I'm trying to export tableView
to excel using Apache POI
Every thing is well but I need export all my table not just items, I mean with columns names when I use this code:
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet spreadsheet = workbook.createSheet("sample");
HSSFRow row = null;
for (int i = 0; i < TousEmpSusp.getItems().size(); i++) {
row = spreadsheet.createRow(i);
for (int j = 0; j < TousEmpSusp.getColumns().size(); j++) {
row.createCell(j).setCellValue(TousEmpSusp.getColumns().get(j).getCellData(i).toString());
}
}
it exports only items, I tried to modify it like this:
for (int j = 0; j < TousEmpView.getColumns().size(); j++) {
row.createCell(j).setCellValue(TousEmpView.getColumns().get(j).getText());
}
for (int i = 0; i < TousEmpView.getItems().size(); i++) {
row = spreadsheet.createRow(i+1);
for (int j = 0; j < TousEmpView.getColumns().size(); j++) {
row.createCell(j).setCellValue(TousEmpView.getColumns().get(j).getCellData(i).toString());
}
}
but it invokes IndexOutOfBoundsException
.
So how can I export tableView
with column names? What should I modify?
Here is a working example that will create a
workbook.xls
when you run it with the column headers exported. It is pretty much what you wrote in your question so I don't get where it doesn't work.Just pass the TableView to the export method of this class and it will take care of everything