I'm trying to write a large(Total no of rows can be up to 2 million) resultset to an .xlsx file. To over come heap space or memory errors, I decided to fetch some limited set of records from table in each call and append the same to the file. Those limited set of records are all ordered by rownum. AT each call to table I'm fetching 50K records and at last after all the records are being fetched from table appending the same to an .xlsx file. At present I'm putting only 600000 records into each sheet. And my current logic is as below.
But the below logic takes lot of time for creating the .xlsx file. Is there anything which can be done to decrease the time?? And it also fails after creating second sheet.
String query="select count(*) from employee";
String actquery="";
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
conn = DriverManager.getConnection(jdbcURL,user,passwd);
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
stmt.setFetchSize(1000);
int rowcounter;
int increaseCount=50000;
int jdbclimit=50000;//jdbc
int excelrowlimit=600000;
int excellimit=600000;
int rownumStartCnt=0;
int rownumEndCnt=0;
if(rs.next()){
outStream = new FileOutputStream("D:\\Test.xlsx",true);
int count=rs.getInt(1);
System.out.println("Total records"+count);
if(count>0)
{
int sheets= count/excelrowlimit;
if(count%excelrowlimit>0){
sheets = sheets+1;
}
for(int scount=1;scount<=sheets;scount++){
sheet = workbook.createSheet("GapHistory-"+scount);
rowcounter=1;
System.out.println("Sheet:"+scount);
System.out.println("limit is "+jdbclimit);
while(rowcounter<excelrowlimit)
{
rownumStartCnt=rownumEndCnt+1;
rownumEndCnt=jdbclimit;
jdbclimit=jdbclimit+increaseCount;
if(rownumEndCnt>excellimit){
rownumEndCnt=excellimit;
}
if(rownumStartCnt>count){
break;
}
PreparedStatement stmt2 = null;
ResultSet rs2 =null;
ResultSetMetaData rsmd=null;
actquery=actquery+"SELECT * FROM (SELECT t.*, ROW_NUMBER() OVER (ORDER BY employee_number) rnum FROM employee t) WHERE rnum BETWEEN ? and ?";
stmt2 = conn.prepareStatement(actquery);
stmt2.setInt(1,rownumStartCnt);
stmt2.setInt(2,rownumEndCnt);
rs2 = stmt2.executeQuery();
System.out.println("Start row-->"+rownumStartCnt+"End row--->"+rownumEndCnt);
rsmd = rs2.getMetaData();
stmt2.setFetchSize(1000);
rs2.setFetchDirection(ResultSet.FETCH_FORWARD);
while(rs2.next())
{
if(rowcounter==1)
{
row = sheet.createRow(rowcounter);
for(int i=1;i<=68;i++)
{
cell = row.createCell(i);
cell.setCellValue(rsmd.getColumnName(i));
}
}
rowcounter++;
for(int i=1;i<=68;i++)
{
if(i==1)
{
row = sheet.createRow(rowcounter);
}
cell = row.createCell(i);
cell.setCellValue(rs2.getString(i));
}
}
actquery="";
}
excellimit = excellimit+excelrowlimit;// Got rid with exception after adding this line
}
}
}