I am trying to generate a .xls file containing almost 13000 lines and 3 columns using the POI library. But it is taking almost 8-10 minutes to generate that complete file. Can anyone suggest how can I reduce the execution time?
public static void generateReconReport(Connection con,String neName,String reportTable) throws SQLException, IOException{
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql = "SELECT * FROM "+reportTable;
ResultSet rsNERecon = stmt.executeQuery(sql);
System.out.println("Resulset obtained, Generating Report");
Date date = new Date();
SimpleDateFormat sid = new SimpleDateFormat("MMddyyyy");
String curDate = sid.format(date);
String fileName = neName.toUpperCase()+"_" + curDate + ".xlsx";
File ob_file = new File(fileName);
if(!ob_file.exists())
ob_file.createNewFile();
HSSFWorkbook hsfWb = new HSSFWorkbook();
HSSFSheet hsfSheet = hsfWb.createSheet(neName.toUpperCase()+" Recon Report");
HSSFRow hsfRow = hsfSheet.createRow(0);
ResultSetMetaData metaRs = rsNERecon.getMetaData();
int colCount = metaRs.getColumnCount();
for (int j = 1; j <= colCount; j++) {
String colName = metaRs.getColumnName(j);
hsfRow.createCell(j).setCellValue(colName);
}
FileOutputStream fileOut = new FileOutputStream(fileName);
int rowNum = 1;
while (rsNERecon.next()) {
hsfRow = hsfSheet.createRow(rowNum);
for (int j = 1; j <= colCount; j++)
hsfRow.createCell(j).setCellValue(rsNERecon.getString(j));
rowNum++;
}
for (int j = 1; j <= colCount; j++)
hsfSheet.autoSizeColumn(j);
rsNERecon.close();
stmt.close();
hsfWb.write(fileOut);
fileOut.close();
System.out.println("Report generated for "+neName.toUpperCase());
}
Generating a character separated value file (csv) using either a tab or a comma as the delimiter will be much faster. Save the file with a .csv extension.
Excel is very quick at reading these files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With