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!");
}
}
}
Hello anh! Cho e hỏi làm sao để không lỗi font khi xuất file excel
ReplyDelete