ZBLOG

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

要支持复制图片类型,您可以在copyRow()方法中添加对图片类型的处理。以下是修改后的代码示例:

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);
            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:
                    // 处理空白单元格
                    
                    
                     if (sourceCell.getHyperlink() != null) { 
                         CreationHelper createHelper=workbook.getCreationHelper(); 
                         Hyperlink hyperlink=createHelper.createHyperlink(HyperlinkType.URL); 
                          hyperlink.setAddress(((XSSFHyperlink)sourceLink).getURL()); 
                         newTargetcell.setHyperlink(hyperlink); 
                        }
                    
                    break;
                default:
                    // 默认处理
                    
                    if (sourceCell.getCellTypeEnum() == CellType.PICTURE) {
                        // 获取源单元格的图片
                        List<XSSFShape> sourcePictures = ((XSSFDrawing) source.getSheet().getDrawingPatriarch()).getShapes();
                        XSSFPicture sourcePicture = null;
                        
                        for (XSSFShape shape : sourcePictures) {
                            if (shape instanceof XSSFPicture) {
                                XSSFPicture pic = (XSSFPicture) shape;
                                XSSFClientAnchor anchor = pic.getClientAnchor();
                                
                                if (anchor.getRow1() == sourceCell.getRowIndex()
                                        && anchor.getCol1() == sourceCell.getColumnIndex()) {
                                    sourcePicture = pic;
                                    break;
                                }
                            }
                        }
                        
                        if (sourcePicture != null) {
                            // 在目标工作簿中创建新的图片
                            Drawing<?> targetDrawing = target.getSheet().createDrawingPatriarch();
                            CreationHelper createHelper = workbook.getCreationHelper();
                            
                            ClientAnchor targetAnchor = createHelper.createClientAnchor();
                            targetAnchor.setRow1(target.getRowNum());
                            targetAnchor.setCol1(i);
                            
                            Picture targetPicture = targetDrawing.createPicture(targetAnchor,
                                    sourcePicture.getPictureData().getPictureType());
                            
                            byte[] pictureDataBytes = sourcePicture.getPictureData().getData();
                            int pictureIndex = workbook.addPicture(pictureDataBytes,
                                    Workbook.PICTURE_TYPE_PNG);  // 根据实际情况修改图片类型
                            
                            targetPicture.resize();  // 调整大小(可选)
                            
                            targetPicture.setWorkbook(workbook);
                            targetPicture.setFileName(sourcePicture.getFileName());
                            targetPicture.setPreserveAspectRatio(sourcePicture.isPreserveAspectRatio());
                            
                        }
                    }
            }
        }
    }
}

这样,您可以在复制行时处理图片类型的单元格,并将其复制到目标工作簿中。请注意,在这段代码中,默认将图片保存为PNG格式,如有需要,请根据实际情况修改为其他图片格式。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?