Xls file giving File is corrupted error when trying to insert footer

@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

@sabkan,

Thanks for the template Excel file.

I tested your scenario/case using your sample code snippet with latest version/fix:
Aspose.Cells for Java v24.11, it works fine and as expected. The output XLS file (attached) is fine tuned. I used “This is test footer” and “This is test footer1” strings/values for footerText and footerText1 variables in the code. I also used dummy value for fileSuffix variable. Please try using Aspose.Cells for Java v24.11 and in case you still find the issue, kindly provide values for footerText and footerText1 variables/objects which we use to reproduce the error or corrupted (output) file.
PR001715-file.zip (7.2 KB)

@amjad.sahi

Thanks.When tried the same xls file from another location, the issue is not happening.
Earlier when tried the xls file from the earlier location with the latest jar 24.11 also was getting the same issue.
So looks like the xls file in that earlier location was having some issue.
Sabarish

@sabkan,

I’m glad your issue has been resolved. It’s possible you had a different or corrupted XLS file in your previous location. Please don’t hesitate to reach out if you have any other questions or comments.