使用EasyPOI模板导出Execl表格(含有图片)

         这是生成的文件效果

一、导入依赖
        <!--easypoi-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>4.2.0</version>
        </dependency>
com.alibaba.excel.exception.ExcelGenerateException: java.lang.NoSuchMethodError: org.apache.poi.ss.usermodel.Cell.getCellType()Lorg/apache/poi/ss/usermodel/CellType;

        我总是会遇到这个问题,原因在于公司的框架引入了hutoolExecl、POI和EasyPOI三种jar包,导致版本冲突,如果要使用EasyPOI,最好把其他的包都删掉,因为EasyPOI会自动引入原生的poi不需要单独引入。

二、创建模板文件

        这是我的xls模板文件,单个参数直接用 {{参数名}} 就可以识别到,如果是列表数据,就需要fe语法

{{$fe: maplist t.id 。。。。。。t.projectCategory}}  双花括号从头包括到尾部 maplist是后端传来的map一个键值对

三、Java代码

        1、 引入模板文件

TemplateExportParams params = new TemplateExportParams("ftl/supervisionProject.xls");

        2、生成模板的数据

        模板的数据都是由map集合来接受的

Map<String, Object> map = new HashMap<String, Object>();
        map.put("year", "2024");
        map.put("mouth", "01");
        File img=new File("D:\\HuaweiMoveData\\Users\\AK47\\Desktop\\各种图片\\640 (2).jpg");
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < 4; i++) {
            Map<String, Object> lm = new HashMap<String, Object>();
            lm.put("id", i + 1 + "");
            lm.put("regionName", i * 10000 + "");
            lm.put("projectName", "A001");
            lm.put("projectCategory", "设计");
            // 插入图片
            BufferedImage bufferImg = ImageIO.read(img);
            ImageIO.write(bufferImg, "jpg", byteArrayOut);
            ImageEntity imageEntity = new ImageEntity(byteArrayOut.toByteArray(), 200, 1000);
            lm.put("img", imageEntity);
            listMap.add(lm);
        }
        map.put("maplist", listMap);

        这一步是将图片放入到集合里,execl中的图片需要转为二进制数组存放

        ImageIO.write(bufferImg, "jpg", byteArrayOut);将文件流转换到byte数组中

        ImageEntity 是easypoi封装好的实体里可以设置图片大小

 // 插入图片
            BufferedImage bufferImg = ImageIO.read(img);
            ImageIO.write(bufferImg, "jpg", byteArrayOut);
            ImageEntity imageEntity = new ImageEntity(byteArrayOut.toByteArray(), 200, 1000);

        3、写入到文件中,并关闭流

 Workbook workbook = ExcelExportUtil.exportExcel(params, map);
 FileOutputStream fos = new FileOutputStream("D:/AAAAA/test.xls");
 workbook.write(fos);
 fos.close();

    这是demo例子全部代码 

 TemplateExportParams params = new TemplateExportParams(
                "ftl/supervisionProject.xls");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("year", "2024");
        map.put("mouth", "01");
        File img = new File("D:\\HuaweiMoveData\\Users\\AK47\\Desktop\\各种图片\\640 (2).jpg");
        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
        List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < 4; i++) {
            Map<String, Object> lm = new HashMap<String, Object>();
            lm.put("id", i + 1 + "");
            lm.put("regionName", i * 10000 + "");
            lm.put("projectName", "A001");
            lm.put("projectCategory", "设计");
            // 插入图片
            BufferedImage bufferImg = ImageIO.read(img);
            ImageIO.write(bufferImg, "jpg", byteArrayOut);
            ImageEntity imageEntity = new ImageEntity(byteArrayOut.toByteArray(), 200, 1000);
            lm.put("img", imageEntity);
            listMap.add(lm);
        }
        map.put("maplist", listMap);
        System.out.println(listMap);
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        FileOutputStream fos = new FileOutputStream("D:/AAAAA/test.xls");
        workbook.write(fos);
        fos.close();
  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1.添加依赖 在pom.xml文件中添加以下依赖: ``` <!-- Excel导出依赖 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.0</version> </dependency> ``` 2.编写控制器 在控制器中编写导出Excel的方法: ``` @GetMapping("/export") public void export(HttpServletResponse response) throws IOException { // 1.创建工作簿 XSSFWorkbook workbook = new XSSFWorkbook(); // 2.创建工作表 XSSFSheet sheet = workbook.createSheet("用户信息"); // 3.创建行 XSSFRow row = sheet.createRow(0); // 4.创建单元格 XSSFCell cell = row.createCell(0); cell.setCellValue("用户名"); cell = row.createCell(1); cell.setCellValue("年龄"); cell = row.createCell(2); cell.setCellValue("性别"); // 5.写入数据 List<User> userList = userService.getUserList(); for (int i = 0; i < userList.size(); i++) { row = sheet.createRow(i + 1); User user = userList.get(i); row.createCell(0).setCellValue(user.getName()); row.createCell(1).setCellValue(user.getAge()); row.createCell(2).setCellValue(user.getGender()); } // 6.设置响应头 response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=userInfo.xlsx"); // 7.输出Excel OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); } ``` 3.测试 启动SpringBoot应用,访问导出Excel的接口,即可下载Excel文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值