easypoi 使用模板导出图片遇到的问题

问题描述

在使用easypoi 模板方式导出图片excel 遇到了一个很奇怪的问题我参考教程去写了一个demo 发现设置宽高方式我怎么都看不到图片,而设置图片占用多少行就能够显示图片 ?

问题出现的环境背景及自己尝试过哪些方法

相关代码

参考代码

参考文档

@Test
    public void one() throws Exception {
        TemplateExportParams params = new TemplateExportParams(
                "doc/exportTemp_image.xls", true);
        Map<String, Object> map = new HashMap<String, Object>();
        // sheet 2
        map.put("month", 10);
        Map<String, Object> temp;
        for (int i = 1; i < 8; i++) {
            temp = new HashMap<String, Object>();
            temp.put("per", i * 10);
            temp.put("mon", i * 1000);
            temp.put("summon", i * 10000);
            ImageEntity image = new ImageEntity();
            image.setHeight(200);
            image.setWidth(500);
            image.setUrl("imgs/company/baidu.png");
            temp.put("image", image);
            map.put("i" + i, temp);
        }
        Workbook book = ExcelExportUtil.exportExcel(params, map);
        File savefile = new File("D:/excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("D:/excel/exportTemp_image.xls");
        book.write(fos);
        fos.close();

    }

我的尝试

  @Test
    public void test() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("template/excel/test1.xls");
        TemplateExportParams params = new TemplateExportParams(classPathResource.getPath(), true);
        Map<String, Object> map = new HashMap<>();
        
        /*生成二维码图片byte[]*/
        byte[] bytes = QrCodeUtil.generatePng("测试内容", 50, 50);
        byte[] bytes1 = QrCodeUtil.generatePng("测试内容2", 50, 50);
        /*设置宽高方式*/
        ImageEntity image = new ImageEntity();
        image.setWidth(100);
        image.setHeight(100);
        image.setData(bytes1);
        
        
        /*设置占有多少行列方式*/
        ImageEntity imageEntity = new ImageEntity();
        imageEntity.setData(bytes);
        imageEntity.setRowspan(10);
        imageEntity.setColspan(4);
                
        map.put("phone", imageEntity);
        map.put("content", "测试内容");
        map.put("picture", image);
        map.put("title", "测试2");
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        File file = new File("C:\\Users\\x1522\\Desktop\\download\\test11.xls");
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream out = new FileOutputStream(file);
        workbook.write(out);
    }

模板
image.png

效果

image.png

这是为啥,是我图片本身的大小设置问题吗?但是同样是50*50 的大小相同的图片为啥一个能显示出来而另一个无法显示,还有一个问题就是 ImageEntity 中的设置 width,height 是控制图片的什么参数呢?
由于之前没有使用过poi 和 easypoi 我对他们都不了解请帮我解答一下谢谢

阅读 6.7k
2 个回答

应该是你的URL有问题,width 是图片宽度,height是图片高度

新手上路,请多包涵

题主提供的参考文档在“1.3 使用”中介绍使用的easypoi-base版本号为3.2.0,所以该教程也是在那个版本的基础上说明的。我下载了3.2.0的jar包看了一下,的确是没有问题,这在后面会解释。

现在的问题是,题主使用的肯定是更高版本的easypoi,比如我目前使用的是4.4.0,查看了一下源码,我觉得问题的确就在于colspan和rowspan上。

ImageEntity在构造时,默认colspan与rowspan都为1。而在BaseExportService.createImageCell构造ClientAnchor时,也就是找到这个图片的锚点时出现问题

这里直接导致XSSFClientAnchor在构造时第一和第二单元格时为同一单元格,也就是相当于将图片画到了一个点上。而在3.2.0版本中这里是没有使用colspan、rowspan变量的

说明在版本迭代过程中,作者虽然支持了图片占用多个单元格的方式,但是这里附加的-1会导致ImageEntity默认值被减掉后变成点,要想正常excel导出图片就必须给colspan、rowspan赋值大于2。

我又看了各个版本的jar,发现4.0.0还是与3.2.0一样只支持1个单元格图片,从4.2.0开始支持colspan、rowspan变量,而且是没有-1,也就是说4.2.0是完美支持的。但是不知道为啥,从4.3.0版本开始增加了-1,导致图片缩为点了...具体原因我想肯定是有别的考量在里面吧,我就没有继续深入研究了...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进