Since there doesn't appear to be a HSSFCell copy constructor, I am attempting to create one, as shown below.

It seems to be working reasonably well, except that it is not copying the old CellStyle or Cell Format. Consequently, when I try to print out the new cell, I am getting an exception because it says there is no format defined for the new cell.

BTW: Is there a particular reason that there isn't a public copy constructor for cells, sheets etc.

public void copyCell(HSSFCell newCell, HSSFCell oldCell) {
        newCell.setCellNum(oldCell.getCellNum());
        newCell.setCellStyle(oldCell.getCellStyle());
        newCell.setCellType(oldCell.getCellType());
        switch (oldCell.getCellType()) {
        case HSSFCell.CELL_TYPE_BLANK:
            break;
        case HSSFCell.CELL_TYPE_BOOLEAN:
            newCell.setCellValue(oldCell.getBooleanCellValue());
            break;
        case HSSFCell.CELL_TYPE_ERROR:
            newCell.setCellErrorValue(oldCell.getErrorCellValue());
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            newCell.setCellFormula(oldCell.getCellFormula());
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            if (HSSFDateUtil.isCellDateFormatted(oldCell)) {
                newCell.setCellValue(oldCell.getDateCellValue());
            } else {
                newCell.setCellValue(oldCell.getNumericCellValue());
            }
            break;
        case HSSFCell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getRichStringCellValue());
            break;
        default:
            System.err.println("Unknown Cell Type: " +
                                oldCell.getCellType());
        } // switch
    } // method copyCell

--
A. Rick Anderson


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/

Reply via email to