I'm working with POI 3.0.2-FINAL, and I met a stressful problem when trying to autosize a column where there's a FONT apply to. I obtain that ugly exception: java.lang.ClassCastException: org.apache.poi.hssf.record.StyleRecord at org.apache.poi.hssf.model.Workbook.getExFormatAt(Workbook.java:665) at org.apache.poi.hssf.usermodel.HSSFCell.getCellStyle(HSSFCell.java:896) at org.apache.poi.hssf.usermodel.HSSFSheet.autoSizeColumn(HSSFSheet.java:1690) Here an abstract of the code producing that stack: private static HSSFCellStyle _BOLD_STYLE = null; private static HSSFFont _BOLD_FONT = null; private HSSFCellStyle getBoldStyle() { if (null == _BOLD_STYLE) { _BOLD_STYLE = _wb.createCellStyle(); _BOLD_STYLE.setFont(getBoldFont()); } return _BOLD_STYLE; } private HSSFFont getBoldFont() { if (null == _BOLD_FONT) { _BOLD_FONT = _wb.createFont(); _BOLD_FONT.setFontHeightInPoints((short)10); _BOLD_FONT.setColor(HSSFFont.COLOR_NORMAL); _BOLD_FONT.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); } return _BOLD_FONT; } Then during usage: HSSFCell c = null; _curCellIndex = -1; r = _sheet.createRow(++_curRowIndex); c = r.createCell(++_curCellIndex); final HSSFRichTextString ref = new HSSFRichTextString("Référence"); c.setCellValue(ref); c.setCellStyle(getBoldStyle()); This is done somthing like 13times for "headers" in my sheet. And once the page is created, I call this: private void layout() { _sheet.setColumnWidth((short)0, (short)2500); _sheet.setColumnWidth((short)1, (short)2500); for (short i = 2; i < 30; ++i) { _sheet.autoSizeColumn(i); } } Then autoSizeColumn fire that strange exception. This example is run under jdk_1.4.2_11, but the dev environnement is jdk1.6.0 (don't think it touch that).
Not related to JDK: same exception with jdk1.4.2 and jdk1.6.0.
Just to precise: autoSizeColumn works perfectly when no Style is applied to a cell scanned by the autoSizeColumn. There's no problem to apply a font *after* the autoSizeColum.
Can you post a fully working example? It would me much easier for me to diagnose the problem. I can't reproduce the problem from the supplied pieces of code. Regards, Yegor
(In reply to comment #3) I tried to write a new project, illustrating the problem, but the sample project works just fine... If I rerun the original project, the problem is still here: that looks like a race condition. Since the code contains confidential information, I'll try to fork it and mess the content to post here a working project containing the problem. I hope I could do it next week. Thank you for your attention.
Since it is not reproducible I'm resolving it. Yegor
Hello, It's seems that this bug occurs when you put one cellStyle to two workbook Just consider the exemple above, the error should take place at line "cell.setCellStyle(cellStyle)" : // Create FileOutputStream os = new FileOutputStream("d:\\tst.xls"); HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet =workbook.createSheet("test"); HSSFCell cell = sheet.createRow(0).createCell((short)0); cell.setCellValue(new HSSFRichTextString("test")); HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); cell.setCellStyle(cellStyle); sheet.autoSizeColumn((short)0); workbook.write(os); os.close(); // re create os = new FileOutputStream("d:\\tst2.xls"); workbook = new HSSFWorkbook(); sheet =workbook.createSheet("test"); cell = sheet.createRow(0).createCell((short)0); cell.setCellValue(new HSSFRichTextString("test")); cell.setCellStyle(cellStyle); sheet.autoSizeColumn((short)0);
You can only use a cell style with the workbook that created it!