Reputation: 1
It will write slow in excel after writing 5000 rows in excel file.
Can anyone suggest that how can i speed up writing excelfile?
I'm scraping data from my website with selenium - java
When i start my code, Writing speed is fast, but it becomes slow after 1 hour. (CPU and RAM consumption are normal.)
This is the code for writing excel file.
public boolean setCellData(String sheetName,String colName,int rowNum, String data){
try{
fis = new FileInputStream(path);
workbook = new XSSFWorkbook(fis);
if(rowNum<=0)
return false;
int index = workbook.getSheetIndex(sheetName);
int colNum=-1;
if(index==-1)
return false;
sheet = workbook.getSheetAt(index);
row=sheet.getRow(0);
for(int i=0;i<row.getLastCellNum();i++){
//System.out.println(row.getCell(i).getStringCellValue().trim());
if(row.getCell(i).getStringCellValue().trim().equals(colName)) {
colNum=i;
break;
}
}
if(colNum==-1)
return false;
sheet.autoSizeColumn(colNum);
row = sheet.getRow(rowNum-1);
if (row == null)
row = sheet.createRow(rowNum-1);
cell = row.getCell(colNum);
if (cell == null)
cell = row.createCell(colNum);
cell.setCellValue(data);
fileOut = new FileOutputStream(path);
workbook.write(fileOut);
fileOut.close();
}
catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
Upvotes: 0
Views: 285
Reputation: 159185
Let's see:
Now why would that be slow?
Because reading and writing files is slow.
You should:
Upvotes: 2
Reputation: 10744
Keep a reference to the sheet and row, instead of looking them up each time you set a value.
Upvotes: 0