Hi Amjad,
Thanks for the quick response, the steps you have mentioned is exactly what I had tried earlier before the previous comment.
To explain it better, here below is the example code outside the loop , which works fine on first write, the file checks out (refer comments in the code). If you go through the code, it is as per the documentation you had directed me to earlier.
On second write the file goes corrupt. May be I am doing something wrong or missed something. Can you please point me in right direction.
fileName=“output.xlsx”;
file = new File(pathToFile+fileName);
file.createNewFile();
fos = new FileOutputStream(file, true);
Workbook destWorkbook = new Workbook(pathToFile+fileName);
Worksheet destSheet = destWorkbook.getWorksheets().get(0);
Workbook srcWorkbook = new Workbook(pathToFile+“Report1.xlsx”);
Worksheet sourceSheet = srcWorkbook.getWorksheets().get(0);
Range sourceRange = sourceSheet.getCells().getMaxDisplayRange();
//as we need to start from row 0 as this is first write to the disk
int TotalRowCount=0;
Range destRange = destSheet.getCells().createRange(sourceRange.getFirstRow() + TotalRowCount,sourceRange.getFirstColumn(), sourceRange.getRowCount(), sourceRange.getColumnCount());
destRange.copy(sourceRange);
destWorkbook.save(fos, FileFormatType.XLSX);
//adding a break here, may be breakpoint or System.exit(0), the output file is written to disk and checks out with 10 records.
srcWorkbook = new Workbook(pathToFile+“Report2.xlsx”);
sourceSheet = srcWorkbook.getWorksheets().get(0);
sourceRange = sourceSheet.getCells().getMaxDisplayRange();
//as there are 12 rows in first file. need to start from 13th row
TotalRowCount=13;
destRange = destSheet.getCells().createRange(sourceRange.getFirstRow() + TotalRowCount,sourceRange.getFirstColumn(), sourceRange.getRowCount(), sourceRange.getColumnCount());
destRange.copy(sourceRange);
destWorkbook.save(fos, FileFormatType.XLSX);
fos.flush();
fos.close();
//now the file (double the size in KB), complains that it is unreadable.
I am facing this problem only on appending to existing file on disk. If i keep workbook in memory and write the content of both input files in one go it works just fine.