ZBLOG

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

要支持单元格图片的复制,你需要做以下优化:

  1. 复制单元格样式时,包括复制图片相关的样式属性。你可以使用CellStyle对象的方法来获取和设置图片样式,例如setFillPattern()setFillForegroundColor()等。

  2. 获取源单元格的图片,并将其复制到目标单元格中。你可以通过调用sourceCell.getDrawingPatriarch()方法获取源单元格上的图形绘图管理器(如果存在),然后通过遍历图形绘图管理器中的所有图形对象来获取图片,并将其插入到目标单元格的绘图管理器中。

下面是修改后的代码片段:

private void copyRow(Row source, Row target, Workbook workbook) {
    // 遍历源行的每个单元格
    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);
            
            // 设置填充模式和背景颜色
            targetCellStyle.setFillPattern(sourceCellStyle.getFillPattern());
            targetCellStyle.setFillForegroundColor(sourceCellStyle.getFillForegroundColor());

            // 复制图片(如果有)
            Drawing<?> drawing = source.getSheet().createDrawingPatriarch();
            if (drawing instanceof XSSFDrawing) {
                XSSFDrawing sourceDrawing = (XSSFDrawing) drawing;
                XSSFDrawing targetDrawing = (XSSFDrawing) target.getSheet().createDrawingPatriarch();
                List<XSSFShape> shapes = sourceDrawing.getShapes();
                for (XSSFShape shape : shapes) {
                    if (shape instanceof XSSFPicture) {
                        XSSFPicture sourcePicture = (XSSFPicture) shape;
                        XSSFClientAnchor sourceAnchor = sourcePicture.getClientAnchor();

                        // 复制图片到目标单元格
                        byte[] pictureData = sourcePicture.getPictureData().getData();
                        int pictureType = sourcePicture.getPictureData().getPictureType();
                        int dx1 = sourceAnchor.getDx1();
                        int dy1 = sourceAnchor.getDy1();
                        int dx2 = sourceAnchor.getDx2();
                        int dy2 = sourceAnchor.getDy2();

                        XSSFClientAnchor targetAnchor = new XSSFClientAnchor(dx1, dy1, dx2, dy2,
                                targetCell.getColumnIndex(), target.getRowNum(),
                                targetCell.getColumnIndex() + 1, target.getRowNum() + 1);
                        
                        // 插入图片到目标单元格的绘图管理器中
                        int index = workbook.addPicture(pictureData, pictureType);
                        XSSFPictureData pictureDataItem = (XSSFPictureData) workbook.getAllPictures().get(index);
                        targetDrawing.createPicture(targetAnchor, index).getCTPicture()
                                .addNewNvPicPr().addNewCnvPr().setId(index + 1);
                    }
                }
            }

            // 设置单元格样式
            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:
                    // target.setCellType(CellType.BLANK);
                    break;
                default:
                    // 默认处理
            }
        }
    }
}

请注意,上述代码假设你使用的是 Apache POI 库,并且处理的是基于 XSSF 的 Excel 文件格式。如果使用其他库或其他文件格式,请相应地调整代码。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?