Anthony Andrews wrote:
Must admit that I had to think about this one a bit - not something I enjoy 
doing late on a Monday afternoon.

Having done so, and I must emphasise that I am not be at all critical but am 
interested, I must ask why you would want a copy constructor? Typically, a Java 
class will over-ride the clone() method to provide this sort of functionality. 
Many will argue against implementing this method I know, but it is the usual 
way to to provide for a deep copy of an object.
I did an external method because I am not a committer and so any changes that I do to the HSSFCell class will be over-ridden on the next update of the code base.

You can't offend me with your question, because I absolutely agree with you! I would have assumed a copy copy constructor or clone override with be part of the standard API, but there's a whole API that is just beneath the public API that does all this stuff. Unfortunately, that means the public API is missing some intuitive functionality. The ugliness of the original MS format / implementation glares through in many places.

I'm also afraid that if I start refactoring the existing classes and API just to address my own perceived issues with the current usability issues, I'd end up forking the source and that would not make folks happy and leave me with ocean's more work then I can imagine.

It turns out that the following code gave me the functionality I need so far. The solution was to explicitly create a new CellStyle and copy over the old values. For some reason, either the copy of the CellStyle wasn't working (I don't understand that one), or Amol's suggestion of deleting/moving the cell number assignment did the trick:

        newCell.setCellNum(oldCell.getCellNum());
        HSSFCellStyle oldStyle = oldCell.getCellStyle();
        short formatID = oldStyle.getDataFormat();
        HSSFCellStyle newStyle = newWorkbook.createCellStyle();
        newStyle.setDataFormat(formatID);
        newCell.setCellStyle(newStyle);
        newCell.setCellType(oldCell.getCellType());

"A. Rick Anderson" <[EMAIL PROTECTED]> wrote: 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