Hi,

I am using the below code (see the commented portion of it for error
details) which is working fine for HSSF but fails for XSSF. The
original code can be reached from
http://www.coderanch.com/t/420958/open-source/Copying-sheet-excel-file-another
(for HSSF). I ended up ignoring the style for now and I am able to
merge my workbooks as explained in the coderanch post however styles
are important of course.

Thank you in advance,
-Nestor

public static void copyCell(XSSFCell oldCell, XSSFCell newCell,
Map<Integer, XSSFCellStyle> styleMap) {
        if (styleMap != null) {
            if (oldCell.getSheet().getWorkbook() ==
newCell.getSheet().getWorkbook()) {
                newCell.setCellStyle(oldCell.getCellStyle());
            } else {
                int stHashCode = oldCell.getCellStyle().hashCode();
                XSSFCellStyle newCellStyle = styleMap.get(stHashCode);
                if (newCellStyle == null) {
                    //The below produces:
                    /*
                    java.lang.ArrayIndexOutOfBoundsException: -1
                    at java.util.ArrayList.get(ArrayList.java:324)
                    at
org.apache.poi.xssf.model.StylesTable.getCellStyleXfAt(StylesTable.java:305)
                    at
org.apache.poi.xssf.usermodel.XSSFCellStyle.<init>(XSSFCellStyle.java:79)
                    at
org.apache.poi.xssf.model.StylesTable.createCellStyle(StylesTable.java:521)
                    at
org.apache.poi.xssf.usermodel.XSSFWorkbook.createCellStyle(XSSFWorkbook.java:443)
                    */
                    newCellStyle =
newCell.getSheet().getWorkbook().createCellStyle();
                    newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
                    styleMap.put(stHashCode, newCellStyle);
                }
                newCell.setCellStyle(newCellStyle);
            }
        }
        switch (oldCell.getCellType()) {
            case XSSFCell.CELL_TYPE_STRING:
                newCell.setCellValue(oldCell.getStringCellValue());
                break;
            case XSSFCell.CELL_TYPE_NUMERIC:
                newCell.setCellValue(oldCell.getNumericCellValue());
                break;
            case XSSFCell.CELL_TYPE_BLANK:
                newCell.setCellType(XSSFCell.CELL_TYPE_BLANK);
                break;
            case XSSFCell.CELL_TYPE_BOOLEAN:
                newCell.setCellValue(oldCell.getBooleanCellValue());
                break;
            case XSSFCell.CELL_TYPE_ERROR:
                newCell.setCellErrorValue(oldCell.getErrorCellValue());
                break;
            case XSSFCell.CELL_TYPE_FORMULA:
                newCell.setCellFormula(oldCell.getCellFormula());
                break;
            default:
                break;
        }

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

Reply via email to