I need to write 600-700k records into xlsx file using Apache POI. the code I am presently using is :
public void writeRecords(ResultSet rs) {
try{
SXSSFWorkbook wb = new SXSSFWorkbook();
wb.setCompressTempFiles(true);
SXSSFSheet sh = (SXSSFSheet)wb.createSheet("Sheet 1");
Row row = null;
int numColumns = rs.getMetaData().getColumnCount();
// Workbook wb = ExcelFileUtil.createExcelWorkBook(true, 5);
sh.setRandomAccessWindowSize(100);// keep 100 rows in memory, exceeding rows will be flushed to disk
Row heading = sh.createRow(1);
ResultSetMetaData rsmd = rs.getMetaData();
for(int x = 0; x < numColumns; x++) {
Cell cell = heading.createCell(x+1);
cell.setCellValue(rsmd.getColumnLabel(x+1));
}
int rowNumber = 2;
int sheetNumber = 0;
while(rs.next()) {
row = sh.createRow(rowNumber);
for(int y = 0; y < numColumns; y++) {
row.createCell(y+1).setCellValue(rs.getString(y+1));
// wb.write(bos);
}
rowNumber++;
}
FileOutputStream out = new FileOutputStream("C:/Users/test1.xlsx");
wb.write(out);
out.close();
}
catch (Exception e){
e.printStackTrace();
}
It is working fine but it is taking ~50 minutes to write ~65k records. Resultset of 65k records was fetched in 5-6 minutes.
Is there any way we can write 600,000-700,000 records in about 10-15 minutes using POI. We wont be able to export data into CSV format, as the endusers have setups to import xlsx files only. regards, Tushar