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,并从源单元格样式中克隆样式。
CellStyle sourceCellStyle = sourceCell.getCellStyle();
CellStyle targetCellStyle = workbook.createCellStyle();
targetCellStyle.cloneStyleFrom(sourceCellStyle);
targetCell.setCellStyle(targetCellStyle);
  1. 使用Drawing对象获取源单元格的所有图片,并将其添加到目标单元格上。
Drawing<?> drawing = sheet.getDrawingPatriarch();
if (drawing != null) {
    List<XSSFShape> shapes = ((XSSFDrawing) drawing).getShapes();
    for (XSSFShape shape : shapes) {
        if (shape instanceof XSSFPicture) {
            XSSFPicture picture = (XSSFPicture) shape;
            XSSFClientAnchor anchor = picture.getClientAnchor();
            if (anchor.getRow1() == sourceRow.getRowNum()) {
                // 创建新的图片锚点并设置位置信息
                ClientAnchor newAnchor = workbook.getCreationHelper().createClientAnchor();
                newAnchor.setCol1(anchor.getCol1());
                newAnchor.setCol2(anchor.getCol2());
                newAnchor.setDx1(anchor.getDx1());
                newAnchor.setDx2(anchor.getDx2());
                newAnchor.setDy1(anchor.getDy1());
                newAnchor.setDy2(anchor.getDy2());
                newAnchor.setRow1(targetRow.getRowNum());
                newAnchor.setRow2(targetRow.getRowNum());

                // 复制图片并设置新的锚点位置
                byte[] pictureData = picture.getPictureData().getData();
                int pictureType = picture.getPictureType();

                int index = workbook.addPicture(pictureData, pictureType);
                Drawing<?> targetDrawing = sheet.createDrawingPatriarch();
                Picture newPicture = targetDrawing.createPicture(newAnchor, index);

                // 设置图片大小
                newPicture.resize();
            }
        }
    }
}

通过上述修改,您的代码将能够复制行中的图片。请注意,以上修改适用于使用XSSFWorkbookXSSFPicture(即.xlsx格式)进行操作。如果您在处理其他文件格式或使用不同的API,请相应地调整代码。


内容由零声教学AI助手提供,问题来源于学员提问

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?