You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Dale Monti <dm...@linoma.com> on 2009/11/06 18:47:35 UTC

Possible Font creation bug

In the below code (Code begin:) no cells are italic. This is because 
when createFont() is called for the second time a new XSSFFont object is 
created but not added to the styles table because it thinks it's already 
there. Maybe if the font is in the styles table createFont() should 
return that object instead of creating a new one?

It's misleading because if the font is not in the styles table then 
cellStyle.setFont doesn't do anything. I don't know if it's really a bug 
since there is no point in calling createFont() the first time. But if 
you create a Font and don't change any of the Font attributes then 
create another Font you may run into this problem. Also, I don't think 
HSSF behaves the same way.

A workaround I have found is to add the font manually to the styles table.

font2.putFont(book.getStylesSource()); //use after setting font attributes

Code begin:
    try{
            File f = new File("C:\\test\\FontBug.xlsx");
            XSSFWorkbook book = new XSSFWorkbook();
            XSSFSheet sheet = book.createSheet("sheet1");
            XSSFRow row = sheet.createRow(0);
            XSSFCellStyle cellStyle = book.createCellStyle();
            XSSFFont font = book.createFont();
            cellStyle.setFont(font);
           
          //font2
            XSSFCellStyle cellStyle2 = book.createCellStyle();
            XSSFFont font2 = book.createFont();
            font2.setItalic(true);
            cellStyle2.setFont(font2);
           
            int colNum = 3;
            for(int i = 0; i < colNum; i++){
                XSSFCell cell = row.createCell(i);
                cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                if(i==1){
                    cell.setCellStyle(cellStyle2);
                }else{
                    cell.setCellStyle(cellStyle);
                }
                cell.setCellValue("test"+i);
            }

            book.write(new BufferedOutputStream(new FileOutputStream(f)));
            System.out.println("done");
        }catch (Exception e){
            e.printStackTrace();
        }

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