I have the following method (createAdditionalSheetsInExcel
) in my code which tries to create additional sheets for a particular scenario (runOutputNumber > 1
). It ends up creating an excel but the problem is when you try to open the excel you end up getting the following errors:
The workBookObj.cloneSheet(index,sheetName)
throws no errors in the java code but when I try to open the excel file I get the following errors:
I tried to remove the formatting for the table in the sheet and then the error disappears. So it must be something to do with the format of the table inside the sheet.
private static void createAdditionalSheetsInExcel(String tempOutputFileName, String outputFileName, int runOutputNumber) throws IOException {
FileInputStream fileIn = new FileInputStream(tempOutputFileName);
XSSFWorkbook workBookObj = new XSSFWorkbook(fileIn);
workBookObj.setWorkbookType(XSSFWorkbookType.XLSM);
runOutputNumber = 2;//Hard coded for clarification
if (runOutputNumber > 1) {
int initialNoOfSheets = workBookObj.getNumberOfSheets();
for (int runIndex = 2; runIndex <= runOutputNumber; runIndex++) {
for (int index = 0; index < initialNoOfSheets; index++) {
XSSFSheet sheet = workBookObj.getSheetAt(index);
String sheetName = sheet.getSheetName().trim()
.substring(0, sheet.getSheetName().length() - 1) + runIndex;
workBookObj.cloneSheet(index, sheetName);
}
}
}
FileOutputStream fileOut = new FileOutputStream(outputFileName);
workBookObj.write(fileOut);
fileOut.close();
workBookObj.close();
deleteTempExcel(tempOutputFileName);
}
Error when the excel tries to open:
We found a problem with some content in 'abc.xlsm'. Do you want to try to recover as much as we can? If you trust the source of this workbook, click Yes.
Error: After opening the excel file:
Repaired Records: Table from /xl/tables/table1.xml part (Table)