要复制sheet行中的图片,您可以使用Apache POI提供的PictureData
和CreationHelper
类来实现。下面是修改后的代码示例:
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 attachmentSheet = attachmentWorkbook.getSheetAt(i);
boolean sheetEmpty = ExcelUtil.isSheetEmpty(attachmentSheet);
if(sheetEmpty){
continue;
}
sheetIndex++;
String newSheetName = "附件-" + originalFilename.substring(0, originalFilename.lastIndexOf(".")) + "-" + sheetIndex;
// 创建新的sheet页来放置附件内容
Sheet newSheet = originalWorkbook.createSheet(newSheetName);
Drawing<?> drawingPatriarchNew = newSheet.createDrawingPatriarch();
Drawing<?> drawingPatriarchOld;
if (attachmentWorkbook instanceof SXSSFWorkbook) {
SXSSFDrawing xssfDrawingOld =(SXSSFDrawing) ((SXSSFSheet) attachmentSheet).getDrawingPatriarch();
ByteArrayOutputStream outByteStream=new ByteArrayOutputStream();
xssfDrawingOld.write(outByteStream);
ByteArrayInputStream inByteStream=new ByteArrayInputStream(outByteStream.toByteArray());
drawingPatriarchOld = newSheet.createDrawingPatriarch();
((SXSSFDrawing)drawingPatriarchOld).readFrom(inByteStream);
} else {
XSSFNameSpace rDmNS =(XSSFNameSpace) ((XSSFSheet) attachmentSheet).getPackagePart().getParent().getParent();
XSSFRelation rid=(XSSFRelation) XSSFDrawing.RELATIONS.getRelationByType(XSSFDrawing.NS_DRAWING_REL);
ByteArrayOutputStream outByteStream=new ByteArrayOutputStream();
for (POIXMLDocumentPart part :(List<POIXMLDocumentPart>)((POIXMLDocument)attachmentWorkbook).getAllEmbedds()) {
if (part instanceof XSSFDrawing || (rDmNS != null && rDmNS.countDrawingRelationships() > 0)) {
PackageRelationshipCollection pdrc;
if(rDmNS != null){
pdrc = ((XSSFDrawing)drawingPatriarchOld).getPackagePart().getRelationshipsByType(rid.getContentType(), rDmNS.getNamespaceURI());
}else{
pdrc = ((XSSFDrawing)drawingPatriarchOld).getPackagePart().getRelationshipsByType(rid.getContentType());
}
for(PackageRelationship pr : pdrc){
PackagePart dspp=pr.getDocumentPart();
OutputStream dspOut=dspp.getOutputStream();//
xssfDrawingOld.write(dspOut);
dspOut.close();
ByteArrayOutputStream by1eArrayOutputStream=new ByteArrayOutputStream();
dspp.save(by1eArrayOutputStream);//
ByteArrayInputStream byteArrayInputStream2=new ByteArrayInputStream(by1eArrayOutputStream.toByteArray());//
OPCPackage pkg=((POIXMLDocumentFactory)(OPCPackageFactory.newInstance()))._open(byteArrayInputStream2);
if (rDmNS != null) {
// check part relations
int pdrcSize = rDmNS.countDrawingRelationships();
for (int prIndex = 0; prIndex < pdrcSize; prIndex++) {
PackageRelationship drr = rDmNS.getDrawingRelationship(prIndex);
Drawing<?> dr=(Drawing<?>)XSSFDrawing.createDrawingPackagePart(drr, pkg).getDocumentPart();
if (dr instanceof XSSFDrawing){
drawingPatriarchOld =(SXSSFDrawing) dr;
break;
}
}
} else {
drawingPatriarchOld =(SXSSFDrawing) ((XSSFWorkbook)attachmentWorkbook).getAllEmbedds().get(1);
}
}
}
}
}
for (Row sourceRow : attachmentSheet) {
Row targetRow = newSheet.createRow(sourceRow.getRowNum());
// 复制附件
copyRow(sourceRow, targetRow, originalWorkbook);
Drawing<?> sourceDrawingPatriarch = sourceRow.getSheet().createDrawingPatriarch();
for(Drawing<?> dr:sourceDrawingPatriarch){
CreationHelper helper = originalWorkbook.getCreationHelper();
ClientAnchor anchorNew = helper.createClientAnchor();
if(drawingPatriarchOld instanceof XSSFDrawing){
XSSFPicture pictureNew= (XSSFPicture)drawingPatriarchNew.createPicture(anchorNew,
originalWorkbook.addPicture(((XSSFPicture)((XSSFSheet)sourceSheet).getRelationsShips().get(dr)).getPictureData().getData(), PictureType.PNG));
pictureNew.getClientAnchor().setCol1(((XSSFPicture)((XSSFSheet)sourceSheet).getRelationsShips().get(dr)).getClientAnchor().getCol1());
pictureNew.getClientAnchor().setCol2(((XSSFPicture)((XSSFSheet)sourceSheet).getRelationsShips().get(dr)).getClientAnchor().getCol2());
pictureNew.getClientAnchor().setRow1(((XSSFPicture)((XSSFSheet)sourceSheet).getRelationsShips().get(dr)).getClientAnchor().getRow1());
pictureNew.getClientAnchor().setRow2(((XSSFPicture)((XSSFSheet)sourceSheet).getRelationsShips().get(dr)).getClientAnchor().getRow2());
} else if(drawingPatriarchOld instanceof SXSSFClientAnchor){
XSSFDrawing xssfDrawing = (SXSSFDrawing)drawingPatriarchOld;
anchorNew.setDx1(xssfDrawing.getFromDX1(dx));
anchorNew.setDy1(xssfDrawing.getFromDY1(dy));
anchorNew.setDx2(xssfDrawing.getFromDX2(dx));
anchorNew.setDy2(xssfDrawing.getFromDY2(dy));
XSSFPictureData pd=xssfPictureDataList.get(i);
// 添加图片到新的sheet
int pictureIndex = originalWorkbook.addPicture(pd.getData(), pd.getPictureType());
drawingPatriarch.createPicture(anchor, pictureIndex);
}
}
}
}
}
}
// 写入修改后的Workbook到文件
try (FileOutputStream fos = new FileOutputStream(originalFile)) {
originalWorkbook.write(fos);
}
}
}
这段代码会遍历附件中的每个sheet,复制行数据并在目标文件中创建新的sheet,并将原始sheet中的图片复制到目标sheet。请确保已经导入了相关依赖,如Apache POI和FastDFS客户端。同时,请根据您的实际需求进行适当修改和调整。
内容由零声教学AI助手提供,问题来源于学员提问