EasyPOI 导出图片存在合并单元格只填充一个单元格问题

重写cn.afterturn.easypoi.excel.export.template.ExcelExportOfTemplateUtil
添加 createImageCell 方法

 1 public void createImageCell(Cell cell, double height, String imagePath, byte[] data) throws Exception {
 2         if(height > (double)cell.getRow().getHeight()) {
 3             cell.getRow().setHeight((short)((int)height));
 4         }
 5         //获取当前单元格所在的sheet
 6         Sheet sheet = cell.getRow().getSheet();
 7         //获取当前sheet页中的所有合并单元格信息
 8         List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
 9         //获取当前单元格的开始列号
10         int firstColumn = (short)cell.getColumnIndex();
11         //获取当前单元格的开始行号
12         int firstRow = cell.getRow().getRowNum();
13         //获取当前单元格的结束列号
14         int lastColumn = (short)(cell.getColumnIndex());
15         //获取当前单元格的结束行号
16         int lastRow = cell.getRow().getRowNum();
17         for(CellRangeAddress mergedRegion : mergedRegions){
18             //判断当前单元格是否包含合并行或和并列 当前单元格的所有行号和列号都包含在合并域内 则认为当前单元格存在合并行或和并列
19             if(cell.getColumnIndex()>=mergedRegion.getFirstColumn()
20                     && cell.getColumnIndex()<=mergedRegion.getLastColumn()
21                     && cell.getRow().getRowNum()>=mergedRegion.getFirstRow()
22                     && cell.getRow().getRowNum()<=mergedRegion.getLastRow()){
23                 //获取合并域的开始行号
24                 firstRow = mergedRegion.getFirstRow();
25                 //获取合并域的结束行号
26                 lastRow = mergedRegion.getLastRow();
27                 //获取合并域的开始列号
28                 firstColumn = mergedRegion.getFirstColumn();
29                 //获取合并域的结束列号
30                 lastColumn = mergedRegion.getLastColumn();
31                 break;
32             }
33         }
34 
35         Object anchor;
36         if(this.type.equals(ExcelType.HSSF)) {
37             anchor = new HSSFClientAnchor(0, 0, 0, 0, (short)firstColumn, firstRow, (short)(lastColumn+1), lastRow+1);
38         } else {
39             anchor = new XSSFClientAnchor(0, 0, 0, 0, (short)firstColumn, firstRow, (short)(lastColumn+1), lastRow+1);
40         }
41 
42         if(StringUtils.isNotEmpty(imagePath)) {
43             data = ImageCache.getImage(imagePath);
44         }
45 
46         if(data != null) {
47             PoiExcelGraphDataUtil.getDrawingPatriarch(cell.getSheet()).createPicture((ClientAnchor)anchor, cell.getSheet().getWorkbook().addPicture(data, this.getImageType(data)));
48         }
49 
50     }
51  

 

posted @ 2019-08-20 20:53  我是一名搬运工  阅读(2224)  评论(0编辑  收藏  举报