无法将“样式”从.xlsx文件复制到另一个文件。
这是我正在使用的代码。
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)
请提供一些建议或示例代码来解决此问题。
发布于 2013-08-09 15:01:58
我认为这个问题是由以下声明产生的:
XSSFCellStyle newCellStyle = styleMap.get(stHashCode);使用这个语句,您基本上就是在说newCellStyle = oldCellStyle。但是,在这种情况下,oldCellStyle链接到另一个工作簿,当您打开文件时会出现错误,因为链接已断开。
只需使用您的代码,删除该语句和测试,它应该可以很好地工作:
newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
styleMap.put(stHashCode, newCellStyle); 发布于 2015-10-30 21:44:18
这是cloneStyleFrom中的一个错误
在花了很多时间之后,我得到了这段丑陋的代码:
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());
}发布于 2013-08-09 16:53:00
为什么需要复制CellStyle?至于我所理解的,您希望将相同的样式应用于两个单元格,可能涉及多个工作簿。如果是这样,我会复制单元格值,并将相同的(预定义的)样式应用到这两个单元格。
https://stackoverflow.com/questions/18124581
复制相似问题