We are seeing a ClassCastException on a worksheet that is being exported to HTML. The symptoms are:
- The worksheet to be exported as HTML has a named range.
- There is a second worksheet which has an “inter-workbook” hyperlink that points to the named range (in the first worksheet).
- Before export, we make a copy of the worksheet with the named range.
- When we try to save the workbook with HtmlSaveOptions, a ClassCastException is thrown.
try {You should see the following output:
String path = “InterLinks.xlsx”; // adjust to local path
String namedRng = “SampleRange”;<span style="color: rgb(0, 0, 0);">Workbook wb = </span><span style="color: rgb(0, 0, 255);">new </span><span style="color: rgb(0, 0, 0);">Workbook(path);</span> <span style="color: rgb(0, 0, 128);">Name </span><span style="color: rgb(0, 0, 0);">name = wb.getWorksheets().getNames().get(namedRng);</span> <span style="color: rgb(0, 128, 0);">// <b>copy</b> the target worksheet to be exported as HTML</span> <span style="color: rgb(0, 0, 255);">int </span><span style="color: rgb(0, 0, 0);">sheetIdx = name.getRange().getWorksheet().getIndex();</span> <span style="color: rgb(0, 0, 0);">sheetIdx = wb.getWorksheets().addCopy(sheetIdx);</span> <span style="color: rgb(0, 0, 0);">wb.getWorksheets().setActiveSheetIndex(sheetIdx);</span> <span style="color: rgb(0, 128, 0);">// export the active worksheet (i.e. copied worksheet)</span> <span style="color: rgb(0, 0, 0);">HtmlSaveOptions options = </span><span style="color: rgb(0, 0, 255);">new </span><span style="color: rgb(0, 0, 0);">HtmlSaveOptions(SaveFormat.</span><span style="color: rgb(0, 0, 128);">HTML</span><span style="color: rgb(0, 0, 0);">);</span> <span style="color: rgb(0, 0, 0);">options.setEncoding(</span><span style="color: rgb(0, 0, 128);">Encoding</span><span style="color: rgb(0, 0, 0);">.getUTF8());</span> <span style="color: rgb(0, 0, 0);">options.setPresentationPreference(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);</span> <span style="color: rgb(0, 0, 0);">options.setExportActiveWorksheetOnly(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);</span> <span style="color: rgb(0, 0, 0);">options.setExportImagesAsBase64(</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);</span> <span style="color: rgb(0, 0, 0);">wb.save(path.replace(</span><span style="color: rgb(85, 85, 85);">".xlsx"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(85, 85, 85);">".html"</span><span style="color: rgb(0, 0, 0);">), options); </span></font><font face="Courier New"><span style="color: rgb(0, 0, 0);"><font face="Courier New"><span style="color: rgb(0, 128, 0);">// exception happens here</span></font></span>
} catch (Exception ex) {
System.out.println(“Unexpected EXCEPTION: \n\t” + ex.getClass().getName() + " - " +
ex.getMessage());
ex.printStackTrace();
}
Unexpected EXCEPTION:
java.lang.ClassCastException - com.aspose.cells.Name cannot be cast to java.lang.Integer
java.lang.ClassCastException: com.aspose.cells.Name cannot be cast to java.lang.Integer
at com.aspose.cells.NameCollection.a(Unknown Source)
at com.aspose.cells.Hyperlink.a(Unknown Source)
at com.aspose.cells.zafw.a(Unknown Source)
at com.aspose.cells.zaey.a(Unknown Source)
at com.aspose.cells.zaey.(Unknown Source)
at com.aspose.cells.zaet.(Unknown Source)
at com.aspose.cells.Workbook.save(Unknown Source)
at AsposeCellsTest.testHyperlinkBug(AsposeCellsTest.java:1725)
at AsposeCellsTest.main(AsposeCellsTest.java:123)
If you remove the hyperlink in the second worksheet, the export to HTML works as expected. However, we’d prefer not to have to modify the rest of the workbook to workaround this issue.
We’ve reproduced this issue under:
- Aspose Cells Version 8.7.1
- Windows 7 running Java 1.7.0_85 and
- LINUX 5 running Java 1.7.0_85.
Thanks.