要支持单元格图片复制,需要进行以下优化:
- 在
copyRow
方法中,判断源单元格是否有图片,并且进行复制。
// 复制单元格样式(如果需要)
CellStyle sourceCellStyle = sourceCell.getCellStyle();
CellStyle targetCellStyle = workbook.createCellStyle();
targetCellStyle.cloneStyleFrom(sourceCellStyle);
targetCell.setCellStyle(targetCellStyle);
// 根据单元格类型复制值
switch (sourceCell.getCellTypeEnum()) {
case STRING:
// ...
case NUMERIC:
// ...
case BOOLEAN:
// ...
case FORMULA:
// ...
case BLANK:
// ...
// 新增代码:检查源单元格是否有图片
if (sourceCell.getCellTypeEnum() == CellType.STRING) {
Drawing<?> sourceDrawing = ((XSSFSheet) sourceSheet).getDrawingPatriarch();
Drawing<?> targetDrawing = ((XSSFSheet) targetSheet).createDrawingPatriarch();
List<XSSFShape> sourceShapes = sourceDrawing.getShapes();
for (XSSFShape shape : sourceShapes) {
if (shape instanceof XSSFPicture) {
XSSFPicture sourcePicture = (XSSFPicture) shape;
XSSFClientAnchor clientAnchor = sourcePicture.getClientAnchor();
byte[] pictureData = sourcePicture.getPictureData().getData(); // 获取原始图片数据
int dx1 = clientAnchor.getDx1(); // 图片左上角相对于列的偏移量
int dy1 = clientAnchor.getDy1(); // 图片左上角相对于行的偏移量
ClientAnchor newClientAnchor =
workbook.getCreationHelper().createClientAnchor();
newClientAnchor.setAnchorType(clientAnchor.getAnchorType());
newClientAnchor.setCol1(targetCell.getColumnIndex() + dx1);
newClientAnchor.setRow1(targetCell.getRowIndex() + dy1);
int pictureIdx = workbook.addPicture(pictureData, Workbook.PICTURE_TYPE_PNG); // 将图片数据添加到工作簿
XSSFPicture newPicture = (XSSFPicture) targetDrawing.createPicture(newClientAnchor, pictureIdx); // 在新单元格创建图片
newPicture.resize(); // 调整图片大小
}
}
}
// ...
}
- 修改
mergeFileSheet
方法,在创建新的sheet页后,需要将原附件中的图片复制到目标单元格。
// 创建新的sheet页来放置附件内容
Sheet newSheet = originalWorkbook.createSheet(newSheetName);
// 新增代码:复制附件中的图片
copyPictures(attachmentWorkbook, attachmentSheet, originalWorkbook, newSheet);
新增的copyPictures
方法如下:
private void copyPictures(Workbook sourceWorkbook, Sheet sourceSheet,
Workbook targetWorkbook, Sheet targetSheet) {
if (sourceSheet instanceof XSSFSheet && targetSheet instanceof XSSFSheet) {
Drawing<?> sourceDrawing = ((XSSFSheet) sourceSheet).getDrawingPatriarch();
Drawing<?> targetDrawing = ((XSSFSheet) targetSheet).createDrawingPatriarch();
if (sourceDrawing != null) {
List<XSSFShape> shapes = sourceDrawing.getShapes();
for (XSSFShape shape : shapes) {
if (shape instanceof XSSFPicture) {
XSSFPicture sourcePicture = (XSSFPicture) shape;
XSSFClientAnchor clientAnchor = sourcePicture.getClientAnchor();
byte[] pictureData = sourcePicture.getPictureData().getData(); // 获取原始图片数据
int dx1 = clientAnchor.getDx1(); // 图片左上角相对于列的偏移量
int dy1 = clientAnchor.getDy1(); // 图片左上角相对于行的偏移量
ClientAnchor newClientAnchor =
targetWorkbook.getCreationHelper().createClientAnchor();
newClientAnchor.setAnchorType(clientAnchor.getAnchorType());
newClientAnchor.setCol1(clientAnchor.getCol1());
newClientAnchor.setDx1(dx1);
newClientAnchor.setRow1(clientAnchor.getRow1());
newClientAnchor.setDy1(dy1);
int pictureIdx = targetWorkbook.addPicture(pictureData, Workbook.PICTURE_TYPE_PNG); // 将图片数据添加到工作簿
XSSFPicture newPicture = (XSSFPicture) targetDrawing.createPicture(newClientAnchor, pictureIdx); // 在新单元格创建图片
newPicture.resize(); // 调整图片大小
}
}
}
}
}
通过以上优化,可以实现在复制单元格时同时复制单元格中的图片。
内容由零声教学AI助手提供,问题来源于学员提问