https://issues.apache.org/bugzilla/show_bug.cgi?id=56959

            Bug ID: 56959
           Summary: CellStyle doesn't contain border and font color info
                    at HSSF
           Product: POI
           Version: 3.10-FINAL
          Hardware: PC
            Status: NEW
          Severity: normal
          Priority: P2
         Component: HSSF
          Assignee: [email protected]
          Reporter: [email protected]

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;
            }
        }
    }

-- 
You are receiving this mail because:
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to