首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSSF无法复制样式(POI)

XSSF无法复制样式(POI)
EN

Stack Overflow用户
提问于 2013-08-08 19:14:16
回答 3查看 7.4K关注 0票数 6

无法将“样式”从.xlsx文件复制到另一个文件。

这是我正在使用的代码。

代码语言:javascript
复制
 public static void copyCell(XSSFCell oldCell, XSSFCell newCell, Map<Integer, XSSFCellStyle> styleMap) {     
    if(styleMap != null) {     
        if(oldCell.getSheet().getWorkbook() .equals( newCell.getSheet().getWorkbook())){     
            newCell.setCellStyle(oldCell.getCellStyle());

        } else{     
            int stHashCode = oldCell.getCellStyle().hashCode();     
            XSSFCellStyle newCellStyle = styleMap.get(stHashCode);

            if(newCellStyle == null){     
                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;     
    }     

}     

同样适用于HSSF,即用于.xls文件,但不适用于XSSF (.xlsx)

请提供一些建议或示例代码来解决此问题。

EN

回答 3

Stack Overflow用户

发布于 2013-08-09 15:01:58

我认为这个问题是由以下声明产生的:

代码语言:javascript
复制
    XSSFCellStyle newCellStyle = styleMap.get(stHashCode);

使用这个语句,您基本上就是在说newCellStyle = oldCellStyle。但是,在这种情况下,oldCellStyle链接到另一个工作簿,当您打开文件时会出现错误,因为链接已断开。

只需使用您的代码,删除该语句和测试,它应该可以很好地工作:

代码语言:javascript
复制
    newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();     
    newCellStyle.cloneStyleFrom(oldCell.getCellStyle());     
    styleMap.put(stHashCode, newCellStyle);     
票数 8
EN

Stack Overflow用户

发布于 2015-10-30 21:44:18

这是cloneStyleFrom中的一个错误

在花了很多时间之后,我得到了这段丑陋的代码:

代码语言:javascript
复制
 private static void copyCellStyle(HSSFCell cell, HSSFCellStyle newCellStyle) {
    newCellStyle.setAlignment(cell.getCellStyle().getAlignment());
    newCellStyle.setBorderBottom(cell.getCellStyle().getBorderBottom());
    newCellStyle.setBorderLeft(cell.getCellStyle().getBorderLeft());
    newCellStyle.setBorderRight(cell.getCellStyle().getBorderRight());
    newCellStyle.setBorderTop(cell.getCellStyle().getBorderTop());
    newCellStyle.setBottomBorderColor(cell.getCellStyle().getBottomBorderColor());
    newCellStyle.setDataFormat(cell.getCellStyle().getDataFormat());
    newCellStyle.setFillBackgroundColor(cell.getCellStyle().getFillBackgroundColor());
    newCellStyle.setFillForegroundColor(cell.getCellStyle().getFillForegroundColor());
    newCellStyle.setFillPattern(cell.getCellStyle().getFillPattern());
    newCellStyle.setFont(cell.getCellStyle().getFont(cell.getSheet().getWorkbook()));
    newCellStyle.setHidden(cell.getCellStyle().getHidden());
    newCellStyle.setIndention(cell.getCellStyle().getIndention());
    newCellStyle.setLeftBorderColor(cell.getCellStyle().getLeftBorderColor());
    newCellStyle.setLocked(cell.getCellStyle().getLocked());
    newCellStyle.setRightBorderColor(cell.getCellStyle().getRightBorderColor());
    newCellStyle.setRotation(cell.getCellStyle().getRotation());
    newCellStyle.setShrinkToFit(cell.getCellStyle().getShrinkToFit());
    newCellStyle.setTopBorderColor(cell.getCellStyle().getTopBorderColor());
    // newCellStyle.setUserStyleName(cell.getCellStyle().getUserStyleName()); -> ignore
    newCellStyle.setVerticalAlignment(cell.getCellStyle().getVerticalAlignment());
    newCellStyle.setWrapText(cell.getCellStyle().getWrapText());
}
票数 1
EN

Stack Overflow用户

发布于 2013-08-09 16:53:00

为什么需要复制CellStyle?至于我所理解的,您希望将相同的样式应用于两个单元格,可能涉及多个工作簿。如果是这样,我会复制单元格值,并将相同的(预定义的)样式应用到这两个单元格。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18124581

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档