in poi/src/main/java/org/apache/poi/ss/util/CellUtil.java [400:484]
public static void copyCell(Cell srcCell, Cell destCell, CellCopyPolicy policy, CellCopyContext context) {
// Copy cell value (cell type is updated implicitly)
if (policy.isCopyCellValue()) {
if (srcCell != null) {
CellType copyCellType = srcCell.getCellType();
if (copyCellType == CellType.FORMULA && !policy.isCopyCellFormula()) {
// Copy formula result as value
// FIXME: Cached value may be stale
copyCellType = srcCell.getCachedFormulaResultType();
}
switch (copyCellType) {
case NUMERIC:
// DataFormat is not copied unless policy.isCopyCellStyle is true
if (!policy.isCopyCellStyle() && DateUtil.isCellDateFormatted(srcCell)) {
destCell.setCellValue(srcCell.getDateCellValue());
} else {
destCell.setCellValue(srcCell.getNumericCellValue());
}
break;
case STRING:
destCell.setCellValue(srcCell.getRichStringCellValue());
break;
case FORMULA:
destCell.setCellFormula(srcCell.getCellFormula());
break;
case BLANK:
destCell.setBlank();
break;
case BOOLEAN:
destCell.setCellValue(srcCell.getBooleanCellValue());
break;
case ERROR:
destCell.setCellErrorValue(srcCell.getErrorCellValue());
break;
default:
throw new IllegalArgumentException("Invalid cell type " + srcCell.getCellType());
}
} else { //srcCell is null
destCell.setBlank();
}
}
// Copy CellStyle
if (policy.isCopyCellStyle() && srcCell != null) {
if (srcCell.getSheet() != null && destCell.getSheet() != null &&
destCell.getSheet().getWorkbook() == srcCell.getSheet().getWorkbook()) {
destCell.setCellStyle(srcCell.getCellStyle());
} else {
CellStyle srcStyle = srcCell.getCellStyle();
CellStyle destStyle = context == null ? null : context.getMappedStyle(srcStyle);
if (destStyle == null) {
destStyle = destCell.getSheet().getWorkbook().createCellStyle();
destStyle.cloneStyleFrom(srcStyle);
if (context != null) context.putMappedStyle(srcStyle, destStyle);
}
destCell.setCellStyle(destStyle);
}
}
final Hyperlink srcHyperlink = (srcCell == null) ? null : srcCell.getHyperlink();
if (policy.isMergeHyperlink()) {
// if srcCell doesn't have a hyperlink and destCell has a hyperlink, don't clear destCell's hyperlink
if (srcHyperlink != null) {
if (srcHyperlink instanceof Duplicatable) {
Hyperlink newHyperlink = (Hyperlink) ((Duplicatable) srcHyperlink).copy();
destCell.setHyperlink(newHyperlink);
} else {
throw new IllegalStateException("srcCell hyperlink is not an instance of Duplicatable");
}
}
} else if (policy.isCopyHyperlink()) {
// overwrite the hyperlink at dest cell with srcCell's hyperlink
// if srcCell doesn't have a hyperlink, clear the hyperlink (if one exists) at destCell
if (srcHyperlink == null) {
destCell.setHyperlink(null);
} else if (srcHyperlink instanceof Duplicatable) {
Hyperlink newHyperlink = (Hyperlink) ((Duplicatable) srcHyperlink).copy();
destCell.setHyperlink(newHyperlink);
} else {
throw new IllegalStateException("srcCell hyperlink is not an instance of Duplicatable");
}
}
}