要在代码中支持复制行图片,您需要使用POI的HSSFPatriarch和XSSFDrawing类来处理单元格中的图片。以下是修改后的代码示例:
private void mergeFileSheet(File originalFile, List<String> fileUrls) throws Exception {
try (FileInputStream fis = new FileInputStream(originalFile);
Workbook originalWorkbook = new XSSFWorkbook(fis)) {
for (String fileUrl : fileUrls) {
// 下载附件
byte[] fileContent = fastDfsClient.download(fileUrl);
String originalFilename = fastDfsClient.getOriginalFilename(fileUrl);
try (ByteArrayInputStream bais = new ByteArrayInputStream(fileContent);
Workbook attachmentWorkbook = new XSSFWorkbook(bais)) {
// 获取附件中sheet数量
int numberOfSheets = attachmentWorkbook.getNumberOfSheets();
int sheetIndex = 0;
for (int i = 0; i < numberOfSheets; i++) {
// 附件sheet
Sheet attachmentSheet = attachmentWorkbook.getSheetAt(i);
// 判断sheet是否为空
boolean sheetEmpty = ExcelUtil.isSheetEmpty(attachmentSheet);
if(sheetEmpty){
continue;
}
sheetIndex++;
String newSheetName = "附件-" + originalFilename.substring(0, originalFilename.lastIndexOf(".")) + "-" + sheetIndex;
// 创建新的sheet页来放置附件内容
Sheet newSheet = originalWorkbook.createSheet(newSheetName);
Drawing<?> drawing;
if (originalSheet instanceof XSSFSheet) {
drawing= ((XSSFSheet)newSheet).createDrawingPatriarch();
} else if (originalSheet instanceof HSSFSheet) {
drawing= ((HSSFSheet)newSheet).createDrawingPatriarch();
} else {
throw new IllegalArgumentException("Invalid sheet type");
}
for (Row sourceRow : attachmentSheet) {
Row targetRow = newSheet.createRow(sourceRow.getRowNum());
// 复制附件
copyRow(sourceRow, targetRow, originalWorkbook, drawing);
}
}
}
}
// 写入修改后的Workbook到文件
try (FileOutputStream fos = new FileOutputStream(originalFile)) {
originalWorkbook.write(fos);
}
}
}
private void copyRow(Row source, Row target, Workbook workbook, Drawing<?> drawing) {
// 遍历源行的每个单元格
for (int i = source.getFirstCellNum(); i <= source.getLastCellNum(); i++) {
Cell sourceCell = source.getCell(i);
// 如果源单元格为null,则不创建新单元格
if (sourceCell != null) {
Cell targetCell = target.createCell(i, sourceCell.getCellType());
// 复制单元格样式(如果需要)
CellStyle sourceCellStyle = sourceCell.getCellStyle();
CellStyle targetCellStyle = workbook.createCellStyle();
targetCellStyle.cloneStyleFrom(sourceCellStyle);
targetCell.setCellStyle(targetCellStyle);
// 根据单元格类型复制值
switch (sourceCell.getCellTypeEnum()) {
case STRING:
targetCell.setCellValue(sourceCell.getStringCellValue());
break;
case NUMERIC:
targetCell.setCellValue(sourceCell.getNumericCellValue());
break;
case BOOLEAN:
targetCell.setCellValue(sourceCell.getBooleanCellValue());
break;
case FORMULA:
// 如果单元格是公式,则复制公式本身
targetCell.setCellFormula(sourceCell.getCellFormula());
break;
case BLANK:
// 直接设置为空白单元格
targetCell.setBlank();
break;
default:
// 默认处理
}
// 复制单元格中的图片(如果存在)
if (sourceCell.getCellTypeEnum() == CellType.STRING) {
String sourceCellValue = sourceCell.getStringCellValue();
RichTextString richTextString = workbook.getCreationHelper().createRichTextString(sourceCellValue);
for (int j = 0; j < richTextString.numFormattingRuns(); j++) {
int startIndex = richTextString.getIndexOfFormattingRun(j);
int endIndex = startIndex + richTextString.getLengthOfFormattingRun(j);
List<? extends PictureData> pictures = workbook.getAllPictures();
for (PictureData picture : pictures) {
if (startIndex >= picture.getStartIndex() && endIndex <= picture.getEndIndex()) {
int pictureRowNum = target.getRowNum();
ClientAnchor anchor = workbook.getCreationHelper().createClientAnchor();
anchor.setCol1(targetCell.getColumnIndex());
anchor.setRow1(pictureRowNum);
if (drawing instanceof XSSFDrawing) {
XSSFDrawing xssfDrawing = (XSSFDrawing) drawing;
XSSFClientAnchor clientAnchor= xssfDrawing.createAnchor(anchor.getDx1(), anchor.getDy1(), anchor.getDx2(),
anchor.getDy2(), anchor.getCol1(), anchor.getRow1(),
anchor.getCol2(), anchor.getRow2());
xssfDrawing.createPicture(clientAnchor, workbook.addPicture(picture.getData(),
Workbook.PICTURE_TYPE_PNG));
} else if (drawing instanceof HSSFPatriarch) {
HSSFPatriarch hssfPatriarch= (HSSFPatriarch)drawing;
HSSFClientAnchor clientAnchor= hssfPatriarch.createAnchor(anchor.getDx1(), anchor.getDy1(), anchor.getDx2(),
anchor.getDy2(), anchor.getCol1(), anchor.getRow1(),
anchor.getCol2(), anchor.getRow2());
hssfPatriarch.createPicture(clientAnchor, workbook.addPicture(picture.getData(),
Workbook.PICTURE_TYPE_PNG));
} else {
throw new IllegalArgumentException("Invalid drawing type");
}
}
}
}
}
}
}
}
请注意,这只是一个示例代码,并且可能需要根据您的具体需求进行进一步修改和适应。
内容由零声教学AI助手提供,问题来源于学员提问




