19 July 2016

Xuất file excel từ bảng Table trong Java swing gui - How to export Excel data in Java swing gui


How to export Excel data in Java swing gui
Java gui 2016
public void exportExcel(JTable table) {
 JFileChooser chooser = new JFileChooser();
 int i = chooser.showSaveDialog(chooser);
 if (i == JFileChooser.APPROVE_OPTION) {
  File file = chooser.getSelectedFile();
  try {
   FileWriter out = new FileWriter(file + ".xls");
   BufferedWriter bwrite = new BufferedWriter(out);
   DefaultTableModel model = (DefaultTableModel) table.getModel();
   // ten Cot
   for (int j = 0; j < table.getColumnCount(); j++) {
    bwrite.write(model.getColumnName(j) + "\t");
   }
   bwrite.write("\n");
   // Lay du lieu dong
   for (int j = 0; j < table.getRowCount(); j++) {
    for (int k = 0; k < table.getColumnCount(); k++) {
     bwrite.write(model.getValueAt(j, k) + "\t");
    }
    bwrite.write("\n");
   }
   bwrite.close();
   JOptionPane.showMessageDialog(null, "Lưu file thành công!");
  } catch (Exception e2) {
   JOptionPane.showMessageDialog(null, "Lỗi khi lưu file!");
  }
 }
}

1 nhận xét:

  1. Hello anh! Cho e hỏi làm sao để không lỗi font khi xuất file excel

    ReplyDelete

 

BACK TO TOP

Xuống cuối trang