Bug 56959 - CellStyle doesn't contain border and font color info at HSSF
Summary: CellStyle doesn't contain border and font color info at HSSF
Status: RESOLVED WORKSFORME
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.10-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-09-11 11:40 UTC by pschnappauf
Modified: 2015-06-20 14:45 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description pschnappauf 2014-09-11 11:40:58 UTC
If CellStyle is copied from an existing Cell to a new Cell (like newCell.setCellStyle(oldCell.getCellStyle());  )
same style informations are not copied (like font color or border style).

When using the same code at XSSF (e.g. newCell.setCellStyle(oldCell.getCellStyle());) it works fine and all style informations are present at newCell again.

Here is what I am doing (see also attached example.xls):
I am copying an existing row by the copyRow method (see later on). The original row was row Nr.3 (with blue text and border). Row Nr.2 (which is a copy of row nr.3) contains almost the same information as the original, except font color and border. If I am using the copyRow method at XSSF, it works fine.


    private static void copyRow(Sheet worksheet, int sourceRowNum, int destinationRowNum)
    {
        // Get the source / new row
        Row newRow = worksheet.getRow(destinationRowNum);
        Row sourceRow = worksheet.getRow(sourceRowNum);
        int numCellsPerRow = sourceRow.getLastCellNum();

        // If the row exist in destination, push down all rows by 1 else create a new row
        if (newRow != null)
        {
            worksheet.shiftRows(destinationRowNum, worksheet.getPhysicalNumberOfRows(), 1, true, true);
            newRow = worksheet.createRow(destinationRowNum);
        }
        else
        {
            newRow = worksheet.createRow(destinationRowNum);
        }

        // Loop through source columns to add to new row
        for (int i = 0; i < numCellsPerRow; i++)
        {
            // Grab a copy of the old/new cell
            Cell oldCell = sourceRow.getCell(i, Row.CREATE_NULL_AS_BLANK);
            Cell newCell = newRow.createCell(i);

            // If the old cell is null jump to next cell
            if (oldCell == null)
            {
                newCell = null;
                continue;
            }

            // Use old cell style
            newCell.setCellStyle(oldCell.getCellStyle());
            
            //-Alternative approach:
            //-CellStyle newCellStyle = workbook.createCellStyle();
            //-newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
            //-newCellStyle.setFont(workbook.getFontAt(oldCell.getCellStyle().getFontIndex()));
            //-newCell.setCellStyle(newCellStyle);

            // If there is a cell comment, copy
            if (oldCell.getCellComment() != null)
            {
                newCell.setCellComment(oldCell.getCellComment());
            }

            // If there is a cell hyperlink, copy
            if (oldCell.getHyperlink() != null)
            {
                newCell.setHyperlink(oldCell.getHyperlink());
            }

            // Set the cell data type
            newCell.setCellType(oldCell.getCellType());

            // Set the cell data value
            switch (oldCell.getCellType())
            {
                case Cell.CELL_TYPE_BLANK:
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    newCell.setCellValue(oldCell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_ERROR:
                    newCell.setCellErrorValue(oldCell.getErrorCellValue());
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    newCell.setCellFormula(oldCell.getCellFormula());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    newCell.setCellValue(oldCell.getNumericCellValue());
                    break;
                case Cell.CELL_TYPE_STRING:
                    newCell.setCellValue(oldCell.getRichStringCellValue());
                    break;
            }
        }
    }
Comment 1 Dominik Stadler 2015-06-20 14:45:23 UTC
I could not reproduce this via the following code with latest trunk, also the style is applied when I look at the file in LibreOffice. 


        // Create a new font and alter it.
        Font font = wb.createFont();
        font.setFontHeightInPoints((short)24);
        font.setFontName("Courier New");
        font.setItalic(true);
        font.setStrikeout(true);
        font.setColor(Font.COLOR_RED);
        
        CellStyle style = wb.createCellStyle();
        style.setBorderBottom(CellStyle.BORDER_DOTTED);
        style.setFont(font);
        
        Cell cell = row.createCell(0);
        cell.setCellStyle(style);
        cell.setCellValue("testtext");
        
        Cell newCell = row.createCell(1);
        
        newCell.setCellStyle(style);
        newCell.setCellValue("2testtext2");

        CellStyle newStyle = newCell.getCellStyle();
        assertEquals(CellStyle.BORDER_DOTTED, newStyle.getBorderBottom());
        assertEquals(Font.COLOR_RED, ((HSSFCellStyle)newStyle).getFont(wb).getColor());



If this is still a problem for you, please reopen this bug with a sample file and a self-contained unit test which allows us to reproduce your problem.