Bug 44680 - autoSizeColumn dislike HSSFFont
Summary: autoSizeColumn dislike HSSFFont
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.0-FINAL
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL: http://article.gmane.org/gmane.comp.j...
Depends on:
Reported: 2008-03-26 09:16 UTC by Thomas Lecavelier
Modified: 2008-04-25 09:12 UTC (History)
1 user (show)


Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Lecavelier 2008-03-26 09:16:29 UTC
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();
        return _BOLD_STYLE;
    private HSSFFont getBoldFont() {
        if (null == _BOLD_FONT) {
            _BOLD_FONT = _wb.createFont();
        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");
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) {

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).
Comment 1 Thomas Lecavelier 2008-03-26 10:17:56 UTC
Not related to JDK: same exception with jdk1.4.2 and jdk1.6.0.
Comment 2 Thomas Lecavelier 2008-03-27 04:42:44 UTC
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.
Comment 3 Yegor Kozlov 2008-03-27 09:47:38 UTC
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. 

Comment 4 Thomas Lecavelier 2008-03-28 06:41:51 UTC
(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.
Comment 5 Yegor Kozlov 2008-04-02 03:33:12 UTC
Since it is not reproducible I'm resolving it.

Comment 6 benjamalin 2008-04-25 09:09:04 UTC

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();

// 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"));

Comment 7 Nick Burch 2008-04-25 09:12:34 UTC
You can only use a cell style with the workbook that created it!