You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by ook? ook! <oo...@gmail.com> on 2008/03/26 16:52:51 UTC

autoSizeColumn mess with fonts

Hi,

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)

Does that problem already known? Someone could help me solving that?

Thank you.

Re: autoSizeColumn mess with fonts

Posted by ook? ook! <oo...@gmail.com>.
Bug 44680 created, hoping this is not too early to create one:
https://issues.apache.org/bugzilla/show_bug.cgi?id=44680

2008/3/26, Yegor Kozlov <ye...@dinom.ru>:
>
> Can you create a bug in bugzilla and attach the problem file?
>
>
> Yegor
>
> > Hi,
>
> > 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)
>
> > Does that problem already known? Someone could help me solving that?
>
> > Thank you.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
>
>

Re: autoSizeColumn mess with fonts

Posted by Yegor Kozlov <ye...@dinom.ru>.
Can you create a bug in bugzilla and attach the problem file?


Yegor
> Hi,

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

> Does that problem already known? Someone could help me solving that?

> Thank you.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Re: autoSizeColumn mess with fonts

Posted by ook? ook! <oo...@gmail.com>.
Thank you again, Mahesh.

I tried to specify the font name, but that didn't change anything.

The only workaround I've found out is to maintain a list of cells to which
apply the Style, but postponed the application *after* the autosizeColum().
The result is not perfect, but acceptable waiting for a fix of that problem.

2008/3/26, Mahesh Ganapathy <mg...@gmail.com>:
>
> Here is my code snippet
>
> [code]
> createFile(String filePath)
> {
>    // Generate the fonts.
>    generateFonts();
>
>    // Generate the styles used in the workbook
>    HSSFCellStyle headerStyle = getHeaderStyle();
>    HSSFCellStyle subHeaderStyle = getSubHeaderStyle();
>    HSSFCellStyle docheaderStyle = getDocHdrLabelStyle();
>    HSSFCellStyle labelStyle = getLabelStyle();
>    HSSFCellStyle valueStyle = getValueStyle();
>    HSSFCellStyle expandedLabelStyle = getExpandedLabelStyle();
>    HSSFCellStyle textAreaStyle = getTextAreaStyle();
>    HSSFCellStyle documentDetailStyle = getDocumentDetailsStyle();
>      :
>      :
>      :
> } // End createFile()
>
> private void generateFonts()
> {
>    // Header Font
>    m_headerFont = m_excelBook.createFont();
>    m_headerFont.setFontHeightInPoints((short) 14);
>    m_headerFont.setFontName("Arial");
>    m_headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
>
>    // Sub Header Font
>    m_subHeaderFont = m_excelBook.createFont();
>    m_subHeaderFont.setFontHeightInPoints((short) 12);
>    m_subHeaderFont.setFontName("Arial");
>    m_subHeaderFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
>       :
>       :
>       :
> } // End generateFonts()
>
> private HSSFCellStyle getHeaderStyle()
> {
>    HSSFCellStyle style = m_excelBook.createCellStyle();
>    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
>    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
>    style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
>    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
>    style.setFont(m_headerFont);
>    return style;
> } // End getHeaderStyle()
>
> private HSSFCellStyle getSubHeaderStyle()
> {
>    HSSFCellStyle style = m_excelBook.createCellStyle();
>    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
>    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
>    style.setFont(m_subHeaderFont);
>    return style;
> }  // End getSubHeaderStyle()
>
> [/code]
>
>
> On 3/26/08, ook? ook! <oo...@gmail.com> wrote:
> >
>
> > Does your Font include Bold?
> >
> > 2008/3/26, Mahesh Ganapathy <mg...@gmail.com>:
> > >
> > > Hello,
> > > just for clarification, I am using *POI 3.0.2-FINAL-20080204 and JDK
> > > compiler compliance level 1.4*
> > > I am using the same code and I don't get the exception.
> > >
> > >
> >
>

Re: autoSizeColumn mess with fonts

Posted by Mahesh Ganapathy <mg...@gmail.com>.
Here is my code snippet

[code]
createFile(String filePath)
{
   // Generate the fonts.
   generateFonts();

   // Generate the styles used in the workbook
   HSSFCellStyle headerStyle = getHeaderStyle();
   HSSFCellStyle subHeaderStyle = getSubHeaderStyle();
   HSSFCellStyle docheaderStyle = getDocHdrLabelStyle();
   HSSFCellStyle labelStyle = getLabelStyle();
   HSSFCellStyle valueStyle = getValueStyle();
   HSSFCellStyle expandedLabelStyle = getExpandedLabelStyle();
   HSSFCellStyle textAreaStyle = getTextAreaStyle();
   HSSFCellStyle documentDetailStyle = getDocumentDetailsStyle();
     :
     :
     :
} // End createFile()

private void generateFonts()
{
   // Header Font
   m_headerFont = m_excelBook.createFont();
   m_headerFont.setFontHeightInPoints((short) 14);
   m_headerFont.setFontName("Arial");
   m_headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

   // Sub Header Font
   m_subHeaderFont = m_excelBook.createFont();
   m_subHeaderFont.setFontHeightInPoints((short) 12);
   m_subHeaderFont.setFontName("Arial");
   m_subHeaderFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
      :
      :
      :
} // End generateFonts()

private HSSFCellStyle getHeaderStyle()
{
   HSSFCellStyle style = m_excelBook.createCellStyle();
   style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
   style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
   style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
   style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
   style.setFont(m_headerFont);
   return style;
} // End getHeaderStyle()

private HSSFCellStyle getSubHeaderStyle()
{
   HSSFCellStyle style = m_excelBook.createCellStyle();
   style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
   style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
   style.setFont(m_subHeaderFont);
   return style;
}  // End getSubHeaderStyle()

[/code]

On 3/26/08, ook? ook! <oo...@gmail.com> wrote:
>
> Does your Font include Bold?
>
> 2008/3/26, Mahesh Ganapathy <mg...@gmail.com>:
> >
> > Hello,
> > just for clarification, I am using *POI 3.0.2-FINAL-20080204 and JDK
> > compiler compliance level 1.4*
> > I am using the same code and I don't get the exception.
> >
> >
>

Re: autoSizeColumn mess with fonts

Posted by ook? ook! <oo...@gmail.com>.
Does your Font include Bold?

2008/3/26, Mahesh Ganapathy <mg...@gmail.com>:
>
> Hello,
> just for clarification, I am using *POI 3.0.2-FINAL-20080204 and JDK
> compiler compliance level 1.4*
> I am using the same code and I don't get the exception.
>
>

Re: autoSizeColumn mess with fonts

Posted by Mahesh Ganapathy <mg...@gmail.com>.
Hello,
just for clarification, I am using *POI 3.0.2-FINAL-20080204 and JDK
compiler compliance level 1.4*
I am using the same code and I don't get the exception.

Best Regards,
Mahesh Ganapathy

On 3/26/08, ook? ook! <oo...@gmail.com> wrote:
>
> Mahesh,
>
> The headless mode is already set.
> As expected, using an int casted in short don't differ from using directly
> a
> short.
>
> I'm playing with the styles, but I just gained an headache. Hoping someone
> will see the light ;)
>
> Thank you for your help.
>
> 2008/3/26, Mahesh Ganapathy <mg...@gmail.com>:
> >
> > Hello,
> > I am doing the exact same thing. even the way you call getCellStyle and
> > getFont. The only difference in code is when you do a resize, you
> declare
> > i
> > as short while I declare it as int and cast it to short inside the loop
> > (see
> > code below)
> >
> >
> > for (int i = 0; i < 6; i++)
> > {
> >    if (i != TITLE_COLUMN)
> >    {
> >       transmittalSheet.autoSizeColumn((short) i);
> >    } else
> >    {
> >       transmittalSheet.setColumnWidth(TITLE_COLUMN, TITLE_COLUMN_WIDTH);
> >    }
> > }
> >
> >
> > Also try adding the following line right at the beginning of your
> layout()
> > method
> >
> > System.*setProperty*("java.awt.headless", "true");
> >
> >
> > On 3/26/08, ook? ook! <oo...@gmail.com> wrote:
> >
> >
> > > Thank you for your information, Mahesh.
> > >
> > > Hum... Here some parts of the code:
> > >
> > > 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.
> > >
> > > I think I'm following the same way than you. Can you see a problem in
> my
> > > lines?
> > >
> > > Thank you.
> > >
> > > 2008/3/26, Mahesh Ganapathy <mg...@gmail.com>:
> > > >
> > > > (remeber to see
> > > > http://poi.apache.org/hssf/quick-guide.html#WorkingWithFonts for
> > > > resrictions on number of fonts/styles in a workbook)
> > > >
> > > >
> > >
> >
>

Re: autoSizeColumn mess with fonts

Posted by ook? ook! <oo...@gmail.com>.
Mahesh,

The headless mode is already set.
As expected, using an int casted in short don't differ from using directly a
short.

I'm playing with the styles, but I just gained an headache. Hoping someone
will see the light ;)

Thank you for your help.

2008/3/26, Mahesh Ganapathy <mg...@gmail.com>:
>
> Hello,
> I am doing the exact same thing. even the way you call getCellStyle and
> getFont. The only difference in code is when you do a resize, you declare
> i
> as short while I declare it as int and cast it to short inside the loop
> (see
> code below)
>
>
> for (int i = 0; i < 6; i++)
> {
>    if (i != TITLE_COLUMN)
>    {
>       transmittalSheet.autoSizeColumn((short) i);
>    } else
>    {
>       transmittalSheet.setColumnWidth(TITLE_COLUMN, TITLE_COLUMN_WIDTH);
>    }
> }
>
>
> Also try adding the following line right at the beginning of your layout()
> method
>
> System.*setProperty*("java.awt.headless", "true");
>
>
> On 3/26/08, ook? ook! <oo...@gmail.com> wrote:
>
>
> > Thank you for your information, Mahesh.
> >
> > Hum... Here some parts of the code:
> >
> > 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.
> >
> > I think I'm following the same way than you. Can you see a problem in my
> > lines?
> >
> > Thank you.
> >
> > 2008/3/26, Mahesh Ganapathy <mg...@gmail.com>:
> > >
> > > (remeber to see
> > > http://poi.apache.org/hssf/quick-guide.html#WorkingWithFonts for
> > > resrictions on number of fonts/styles in a workbook)
> > >
> > >
> >
>

Re: autoSizeColumn mess with fonts

Posted by Mahesh Ganapathy <mg...@gmail.com>.
Hello,
I am doing the exact same thing. even the way you call getCellStyle and
getFont. The only difference in code is when you do a resize, you declare i
as short while I declare it as int and cast it to short inside the loop (see
code below)


for (int i = 0; i < 6; i++)
{
   if (i != TITLE_COLUMN)
   {
      transmittalSheet.autoSizeColumn((short) i);
   } else
   {
      transmittalSheet.setColumnWidth(TITLE_COLUMN, TITLE_COLUMN_WIDTH);
   }
}


Also try adding the following line right at the beginning of your layout()
method

System.*setProperty*("java.awt.headless", "true");

On 3/26/08, ook? ook! <oo...@gmail.com> wrote:

> Thank you for your information, Mahesh.
>
> Hum... Here some parts of the code:
>
> 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.
>
> I think I'm following the same way than you. Can you see a problem in my
> lines?
>
> Thank you.
>
> 2008/3/26, Mahesh Ganapathy <mg...@gmail.com>:
> >
> > (remeber to see
> > http://poi.apache.org/hssf/quick-guide.html#WorkingWithFonts for
> > resrictions on number of fonts/styles in a workbook)
> >
> >
>

Re: autoSizeColumn mess with fonts

Posted by ook? ook! <oo...@gmail.com>.
Thank you for your information, Mahesh.

Hum... Here some parts of the code:

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.

I think I'm following the same way than you. Can you see a problem in my
lines?

Thank you.

2008/3/26, Mahesh Ganapathy <mg...@gmail.com>:
>
> (remeber to see
> http://poi.apache.org/hssf/quick-guide.html#WorkingWithFonts for
> resrictions on number of fonts/styles in a workbook)
>
>

Re: autoSizeColumn mess with fonts

Posted by Mahesh Ganapathy <mg...@gmail.com>.
Hello,
I am using the same version and performing the same actions. However, I have
never come accross this issue.
This is how I do it.

create a font
create a style

(remeber to see
http://poi.apache.org/hssf/quick-guide.html#WorkingWithFonts for
resrictions on number of fonts/styles in a workbook)

apply the font to the style
apply the style to the cells I need
autosize the column.

In my case, a column does contain cells with difference styles. How are you
applying the font to the cell?

On 3/26/08, ook? ook! <oo...@gmail.com> wrote:
>
> Hi,
>
> 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)
>
> Does that problem already known? Someone could help me solving that?
>
> Thank you.
>