Sending files in blocks
I have a problem with sending an Excel file in blocks. I have this code that is supposed to send the Excel file in blocks, and it works fine up to a certain point. However, the issue is that it only writes the first 100 records. After that, the file size remains the same as if it had all 10,000 records, but when opened, it only has 100 records.
@Override
public void generateAndSendExcelChunks(ServletOutputStream outputStream) throws IOException {
Workbook workbook = new SXSSFWorkbook();
Sheet sheet = workbook.createSheet("Datos");
int totalRows = 10000;
int rowsPerChunk = 100;
int currentRow = 0;
while (currentRow < totalRows) {
int endRow = currentRow + rowsPerChunk;
endRow = Math.min(endRow, totalRows);
generateChunkData(sheet, currentRow, endRow);
workbook.write(outputStream);
outputStream.flush();
sheet = workbook.createSheet("Datos");
currentRow = endRow;
}
workbook.close();
}
private void generateChunkData(Sheet sheet, int startRow, int endRow) {
for (int i = startRow; i < endRow; i++) {
Row row = sheet.createRow(i);
Cell cell = row.createCell(0);
cell.setCellValue("Valor " + (i + 1));
}
}
The line sheet = workbook.createSheet("Datos"); doesn't work because it tries to create a sheet with a name that already exists. If I try to get a reference to that sheet, it gives a "stream closed" error on this line: workbook.write(outputStream);. The intended functionality is to store all the information in the same sheet without overwriting anything.
0 comments:
Post a Comment
Thanks