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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM