I\'ve recently started working with java and I\'m facing some issues with the apache poi library when I need to create an excel file starting from a jTable.
I\'ve re
I know that probably this is late for the post, but maybe for others that want to have the code, here is something that works.
TableModel model = raportTable.getModel();
SXSSFWorkbook wb = new SXSSFWorkbook(-1);
SXSSFSheet sh = wb.createSheet("Report");
Row row = sh.createRow(0);
for (int i = 0; i < model.getColumnCount(); i++) {
Cell cell = row.createCell(i);
cell.setCellValue(model.getColumnName(i));
}
for(int i = 0; i < model.getRowCount(); i++) {
row = sh.createRow(i + 1);
for(int j = 0; j < model.getColumnCount(); j++){
Cell cell = row.createCell(j);
cell.setCellValue(model.getValueAt(i,j).toString());
}
}
try {
FileOutputStream excel = new FileOutputStream(filename + ".xlsx");
wb.write(excel);
excel.flush();
excel.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
}
Hope it helps.