zoukankan      html  css  js  c++  java
  • Hutool工具包导出Excel文件异常 You need to add dependency of poi-ooxml to your project

    Hutool工具包导出Excel文件异常 You need to add dependency of poi-ooxml to your project

    异常信息:

    cn.hutool.core.exceptions.DependencyException: You need to add dependency of 'poi-ooxml' to your project, and version >= 3.17
    	at cn.hutool.poi.excel.ExcelUtil.getWriter(ExcelUtil.java:376)
    	at cn.pconline.pcloud.admin.controller.train.ExamController.downRankList(ExamController.java:364)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:498) 
    	省略其他。。。。
    
    

    解决方案:

    提示很明确啊,你需要引入poi-ooxml库,版本>=3.17,具体见文档:

    https://www.hutool.cn/docs/#/poi/概述

    使用Hutool工具包导出Excel例子

    贴一个使用Hutool工具导出excel的例子,方便下次复制

    • 1.添加pom依赖
    <!--Hutool工具包-->
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>4.5.10</version>
    </dependency>
    <!--Hutool工具ExcelUtil依赖这个 https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>4.1.0</version>
    </dependency>
    
    • 2.请求代码
     /**
     * 导出
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/student/export.do")
    public void export(HttpServletRequest request, HttpServletResponse response) {
        try {
            List<Student> list = studentService.getlist(Student.class);
    
            String fileName = "student-" + new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("multipart/form-data");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");
    
            List<String> header = CollUtil.newArrayList("名次", "员工名称", "分数", "耗时");
            ExcelWriter excelWriter = ExcelUtil.getWriter(true);
            excelWriter.writeHeadRow(header);
            excelWriter.write(getListRow(list));
            excelWriter.flush(response.getOutputStream());
            excelWriter.close();
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private List<List<String>> getListRow(List<Student> students) {
        List<List<String>> rows = new ArrayList<>();
        students.stream().filter(student -> students != null).forEach(student -> {
            rows.add(new ArrayList<String>() {{
                add(student.getNo());
                add(student.getName());
                add(student.getScore());
                add(student.getTime());
                
            }});
        });
        return rows;
    }
    
    
  • 相关阅读:
    对GDI+绘制圆弧接口的理解
    陈灯可重用代码管理器(插件版最新版本:3.2;桌面版最新版本:2.3)
    Apache OpenJPA 2.1.0 发布
    B3log Solo 0.2.5.1 发布了!
    Apache OpenJPA 2.1.0 发布
    jsoup 1.5.1 发布,超棒的HTML解析器
    程序员阿士顿的故事
    Web 是开源最大的成功
    Web 是开源最大的成功
    Python执行系统命令的方法 os.system(),os.popen(),commands renwofei423的个人空间 开源中国社区
  • 原文地址:https://www.cnblogs.com/cnsyear/p/12724690.html
Copyright © 2011-2022 走看看