ZBLOG

private void mergeFileSheet(File originalFile, List fileUrls) throws Exception { try (FileInputStream fis = new FileInputStream(originalFile); Workbook originalWorkbook = new XSSFWorkbook(fis)) { for (Strin...

要支持单元格图片复制,需要进行以下优化:

  1. 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();  // 调整图片大小
            }
        }
    }

    // ...
}
  1. 修改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助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=16323

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?