if (tableModel == null) throw new IllegalArgumentException("Invalid tabla data model!");
int columnCount = tableModel.getColumnCount();
int rowCount = tableModel.getRowCount() + 1; //Include header row
int row = 0;
SXSSFWorkbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk
Map<String, CellStyle> styles = createStyles(wb);
Sheet sh = wb.createSheet("Sheet 1");
// General setup
sh.setDisplayGridlines(true);
sh.setPrintGridlines(false);
sh.setFitToPage(true);
sh.setHorizontallyCenter(true);
PrintSetup printSetup = sh.getPrintSetup();
printSetup.setLandscape(true);
// Create header
Row header = sh.createRow(row++);
header.setHeightInPoints(20f);
for (int i = 0; i < columnCount; i++) {
Cell cell = header.createCell(i);
cell.setCellStyle(styles.get("header"));
cell.setCellValue(tableModel.getColumnName(i));
}
// Create data rows
for(; row < rowCount; row++){
Row _row = sh.createRow(row);
for(int cellnum = 0; cellnum < columnCount; cellnum++){
Cell cell = _row.createCell(cellnum);
Object value = tableModel.getValueAt(row-1, cellnum);
if (value instanceof Short || value instanceof Long || value instanceof Integer || value instanceof BigInteger) {
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellStyle(styles.get("integer_number_cell"));
if (value!= null) cell.setCellValue( ((Number)value).doubleValue());
} else if (value instanceof Float || value instanceof Double || value instanceof BigDecimal) {
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellStyle(styles.get("decimal_number_cell"));
if (value!= null) cell.setCellValue( ((Number)value).doubleValue());
} else if (value instanceof Date) {
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellStyle(styles.get("date_cell"));
if (value!= null) cell.setCellValue( (Date)value );
} else if (value instanceof Interval) {
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellStyle(styles.get("text_cell"));
if (value!= null) cell.setCellValue( ((Interval)value).getDescription(LocaleManager.currentLocale()) );
} else {
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellStyle(styles.get("text_cell"));
if (value!= null) cell.setCellValue( value.toString() );
}
}
}
// Adjust column size
for (int i = 0; i < columnCount; i++) {
sh.autoSizeColumn(i);
}
// Put border around table data
CellRangeAddress range = new CellRangeAddress(0, rowCount-1, 0, columnCount-1);
RegionUtil.setBorderRight(BorderFormatting.BORDER_THIN, range, sh, wb);
RegionUtil.setRightBorderColor(IndexedColors.BLACK.getIndex(), range, sh, wb);
RegionUtil.setBorderLeft(BorderFormatting.BORDER_THIN, range, sh, wb);
RegionUtil.setLeftBorderColor(IndexedColors.BLACK.getIndex(), range, sh, wb);
RegionUtil.setBorderTop(BorderFormatting.BORDER_THIN, range, sh, wb);
RegionUtil.setTopBorderColor(IndexedColors.BLACK.getIndex(), range, sh, wb);
RegionUtil.setBorderBottom(BorderFormatting.BORDER_THIN, range, sh, wb);
RegionUtil.setBottomBorderColor(IndexedColors.BLACK.getIndex(), range, sh, wb);
ByteArrayInputStream bis = null;
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
wb.write(bos);
bis = new ByteArrayInputStream(bos.toByteArray());
bos.close();
} catch (IOException e) {
log.error("Data export error: ", e);
}
// Dispose of temporary files backing this workbook on disk
if (!wb.dispose()) log.warn("Could not dispose of temporary file associated to data export!");
return bis;
}