Bug 49254

Summary: CellUtil.setFont fails
Product: POI Reporter: Thomas Herre <thomas.herre>
Component: XSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 3.6-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   

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 :/