@amjad.sahi
When trying to insert footer in a xls file giving error “File is corrupted”
Previously it used to work
other xls files are working fine
The aspose-cells-24.5.jar jar is used.
The code snippet used to insert footer is as follows:
System.out.println(fileDir + fname);
workbook = new Workbook(fileDir + File.separator + fname);
workbook.getSettings().setRegion(CountryCode.USA);
System.out.println(workbook.getWorksheets().getCount());
infolog.info(workbook.getWorksheets().getCount());
for (int i = 0; i < workbook.getWorksheets().getCount(); i++) {
// Get the first default sheet
Worksheet sheet = null;
try {
sheet = workbook.getWorksheets().get(i);
}
catch (Exception ex) {
// logger.error("watermarkTextExcelFile load sheet ex:" + ex.getMessage());
ex.printStackTrace();
errlog.error("Exception occurred while reading excel sheets " + ex.getMessage());
}
if(sheet != null) {
PageSetup pageSetup = sheet.getPageSetup();
pageSetup.setFooterMargin(0);
// pageSetup.setFooter(2, watermarkText); // 2 represents the section where you want to add the watermark (Center section in the footer)
// String wtxt = "&KFF0000"+watermarkText;
//01/01/2024 to set font size to 8
// String wtxt = "&KFF0000&8"+footerText+"&D"+footerText1;
//trying to include time also on 05/16/2024
String wtxt = "&KFF0000&8"+footerText+"&D"+ " " + "&T" + footerText1;
// pageSetup.setFooter(2,wtxt );
//to set the footer in the center
pageSetup.setFooter(1,wtxt );
//trying to do right aligned on 05/16/2024 since it is overlapping for PR001715.xls
// pageSetup.setFooter(2,wtxt );
}
else {
// no sheet
break;
}
}
//modified as per Aspose team suggestion
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
if (fname.endsWith(".xls") || fname.endsWith(".XLS")) {
workbook.save(outputStream, com.aspose.cells.SaveFormat.EXCEL_97_TO_2003);
} else {
workbook.save(outputStream, com.aspose.cells.SaveFormat.XLSX);
}
///modified on 07/23/2024 to rename file in util itself
int dotlastpos = fname.lastIndexOf('.');
String ProdSuffixedFileName = fname.substring(0, dotlastpos) + "-" + fileSuffix + fname.substring(dotlastpos);
FileOutputStream fos1 = new FileOutputStream(fileDir + File.separator + ProdSuffixedFileName);
fos1.write(outputStream.toByteArray());
fos1.close();
outputStream.close();
infolog.info("Footer inserted successfully for excel document " + fname);
footerinserted=true;
}
} catch(Exception ex) {
ex.printStackTrace();
errlog.error("Exception occurred during inserting footer for file " + fname + " " + ex.getMessage());
} finally {
if (workbook != null) workbook.dispose();
}
Attaching the xls file having the issue.
PR001715.zip (8.0 KB)
Thanks
Sabarish