• springboot整合jett导出数据(2)


    一 操作案例

    1.1 pom文件

    1. <dependency>
    2. <groupId>net.sf.jett</groupId>
    3. <artifactId>jett-core</artifactId>
    4. <version>0.11.0</version>
    5. </dependency>

    1.2 代码

    1. /**
    2. * @author liujianfu
    3. * @description 导出 环保指标查询日,月,年数据
    4. * @date 2022/12/1 16:39
    5. * @param [response]
    6. * @return void
    7. */
    8. @RequestMapping("/export")
    9. public void exportEnvCycleExcel(HttpServletResponse response) {
    10. Map<String, Object> resultMap = new HashMap<String, Object>();
    11. resultMap.put("titleName","环保指标报表");
    12. resultMap.put("reportDate", DateUtils.dateToStr(new Date(),"yyyy-MM-dd"));
    13. //List<Student> studentsList=new ArrayList<>();
    14. List<Map> studentsList=new ArrayList<>();
    15. for(int k=0;k<5;k++){
    16. Map<String,Object> map=new LinkedHashMap<>();
    17. map.put("id",k);
    18. map.put("name","李四"+k);
    19. map.put("age",(k+1)*2);
    20. studentsList.add(map);
    21. }
    22. resultMap.put("dataList",studentsList);
    23. buildExcelReport( resultMap, response);
    24. }
    25. /**
    26. * @author liujianfu
    27. * @description 封装excel
    28. * @date 2022/11/8 10:42
    29. * @param [resultMap]
    30. * @return void
    31. */
    32. public void buildExcelReport(Map<String, Object> resultMap, HttpServletResponse response){
    33. String modelFile="d:/model-test.xlsx";
    34. try (InputStream is = new FileInputStream(new File(modelFile));) {
    35. Workbook workbook = new ExcelTransformer().transform(is, resultMap);
    36. buildExcelDocument("环保_"+System.currentTimeMillis()+".xlsx", workbook, response);
    37. } catch (Exception e) {
    38. e.printStackTrace();
    39. }
    40. }
    41. /**
    42. * @author liujianfu
    43. * @description 数据流的输出
    44. * @date 2022/11/8 10:42
    45. * @param [filename, workbook, response]
    46. * @return void
    47. */
    48. protected static void buildExcelDocument(String filename, Workbook workbook, HttpServletResponse response)
    49. throws Exception {
    50. response.setHeader("Pragma", "no-cache");
    51. response.setHeader("Cache-Control", "no-cache");
    52. response.setContentType("application/vnd.ms-excel");
    53. response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "utf-8"));
    54. OutputStream outputStream = response.getOutputStream();
    55. workbook.write(outputStream);
    56. outputStream.flush();
    57. outputStream.close();
    58. }

    1.3 excel模板

    ${t.id}    ${t.name}   ${t.age}

     

    1.4 导出效果

     

  • 相关阅读:
    9月编程排行榜新鲜出炉霸榜还得是它~
    typescript23-元组
    springboot实现动态定时任务(客户端维持心跳)
    C语言经典面试题目(十六)
    Matlab|基于时间序列预测的粒子群优化混合非线性回归和自回归技术的比较
    为爱出发,与善同行丨纬创软件2023北京善行者圆满收官
    Vue框架实现对前端数据表的增删改查(第六课)
    超级明星们的人物化身 NFT 将来到 The Sandbox 元宇宙
    SpringCloud微服务
    SpringMVC入门
  • 原文地址:https://blog.csdn.net/u011066470/article/details/128136323