[alibaba/easyexcel]图片填充,图片宽高

2024-05-21 47 views
5

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        if(CellDataTypeEnum.IMAGE.equals(cellData.getType())){
            log.info("第{}行,第{}列数据转换后。{}", cell.getRowIndex(), cell.getColumnIndex(),cellData.getType());
            Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
            Sheet sheet = workbook.getSheetAt(0);
            //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
            Drawing patriarch = sheet.createDrawingPatriarch();
            //anchor主要用于设置图片的属性
            XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 255, 255, cell.getColumnIndex(), cell.getRowIndex()+1, cell.getColumnIndex()+2, cell.getRowIndex()+6);
            anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
            //插入图片
            patriarch.createPicture(anchor, workbook.addPicture(cellData.getImageValue(), HSSFWorkbook.PICTURE_TYPE_JPEG));
        }
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        // 这里可以对cell进行任何操作
        //log.info("第{}行,第{}列写入完成。{}", cell.getRowIndex(), cell.getColumnIndex(),cell.getCellType());
    }

建议描述 在afterCellDataConverted中处理,是图片类型时,手动写图片到模板中,并设置大小。有个问题就是,这个不会覆盖easyexcel图片写入,导致图片重复写入。 如果说是在afterCellDispose中设置图片大小,该怎么操作。

回答

7

明白了,在https://www.yuque.com/easyexcel/faq/wpedtd 找到了解决办法。

5

图片之前写进了excel,自定义的拦截器又进去了一遍。图片重复写入,这怎么解决呀?我只想要自定义的拦截器设置过格式的图片。怎办

9

3.0.5版本 怎么解决的呢?