Olá, estou com algumas duvidas sobre como exportar uma jTable para Excel. Já li alguns post e cheguei no código abaixo, porem, não esta dando certo, algum poderia apontar o meu erro por favor?
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc = new JFileChooser();
UtilitarioDAO u = new UtilitarioDAO();
int option = fc.showSaveDialog(jtRelatorio);
if (option == JFileChooser.APPROVE_OPTION) {
String filename = fc.getSelectedFile().getName();
String path = fc.getSelectedFile().getParentFile().getPath();
int len = filename.length();
String ext = "";
String file = "";
if (len > 4) {
ext = filename.substring(len - 4, len);
}
if (ext.equals(".xls")) {
file = path + "\\" + filename;
} else {
file = path + "\\" + filename + ".xls";
}
try {
u.toExcel(jtRelatorio, new File(file)); // da erro aqui
} catch (IOException ex) {
// Logger.getLogger(formRdi.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(null, ex);
}
}
}
public void toExcel(JTable table, File file) throws IOException {
TableModel model = table.getModel();
FileWriter excel = new FileWriter(file);
for (int i = 0; i < model.getColumnCount(); i++) {
excel.write(model.getColumnName(i) + "\t");
}
excel.write("\n");
for (int i = 0; i < model.getRowCount(); i++) {
for (int j = 0; j < model.getColumnCount(); j++) {
excel.write(model.getValueAt(i, j).toString() + "\t");
}
excel.write("\n");
}
excel.close();
System.out.println("write out to: " + file);
}