Bug 49254 - CellUtil.setFont fails
Summary: CellUtil.setFont fails
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.6-FINAL
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-05-05 11:17 UTC by Thomas Herre
Modified: 2010-05-05 11:25 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Herre 2010-05-05 11:17:56 UTC
Description:
This bug applies to both XSSF and HSSF:
In a workbook, I create a bold font.
Setting this font to a cell using method CellUtil.setFont fails.
Instead, the font at index 0 is used.

Test Code:

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();

        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("TestCell");
        
        Font font = workbook.createFont();
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        font.setFontHeightInPoints((short) 20);
        
        // This fails
        CellUtil.setFont(cell, workbook, font);
        // Workaround
//        short fontIndex = font.getIndex();
//        CellUtil.setCellStyleProperty(cell, workbook, CellUtil.FONT, Short.valueOf(fontIndex));

        workbook.write(new FileOutputStream(new File("test-font.xlsx")));

Evaluation:
CellUtil.setFont calls method setCellStyleProperty and passes-in a Font object as parameter "propertyValue". It fails a find a style that it can reuse and creates a new one. Then it calls setFormatProperties which in turn looks up the font using method getShort. Tha map passed-in contains almost only Short object as values, but a Font object for property "font". Thus it returns the default value 0. 

Workaround:
Avoid method CellUtil.setFont and use the workaround (as commented-out in the  above test code).
Comment 1 Nick Burch 2010-05-05 11:25:38 UTC
Should be fixed in r941342.

Note that things in contrib don't always receive the same level of testing as in the main jar file, as they generally lack unit tests :/