• Springboot 自定义模板导出Excel文件


    指定模板(自定义)导出数据,就像:

    废话不多说

    看正文

    开始实战:

    pom.xml依赖:

    1. <dependency>
    2. <groupId>cn.afterturngroupId>
    3. <artifactId>easypoi-baseartifactId>
    4. <version>3.0.3version>
    5. dependency>
    6. <dependency>
    7. <groupId>cn.afterturngroupId>
    8. <artifactId>easypoi-webartifactId>
    9. <version>3.0.3version>
    10. dependency>
    11. <dependency>
    12. <groupId>cn.afterturngroupId>
    13. <artifactId>easypoi-annotationartifactId>
    14. <version>3.0.3version>
    15. dependency>

    我们导出数据的实体类 PaymentBillListVO.java

    1. @Data
    2. public class PaymentBillListVO {
    3. /** 主键 */
    4. private String id;
    5. @JsonProperty("orderId")
    6. private String orderId;
    7. /** 资源id */
    8. @JsonProperty("resourceId")
    9. private String resourceId;
    10. /** 资源名 */
    11. @JsonProperty("resourceName")
    12. private String resourceName;
    13. @JsonProperty("feeItemId")
    14. private String feeItemId;
    15. /** 收费项名 */
    16. @JsonProperty("feeItemName")
    17. private String feeItemName;
    18. /** 缴费单对应的周期 */
    19. // 日期输出格式化
    20. @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    21. @JsonProperty("beginDate")
    22. private Date beginDate;
    23. /** 缴费单对应的周期 */
    24. // 日期输出格式化
    25. @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    26. @JsonProperty("endDate")
    27. private Date endDate;
    28. // 缴费限期
    29. @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    30. @JsonProperty("deadline")
    31. private Date deadline;
    32. /** 客户 */
    33. @JsonProperty("feeUser")
    34. private String feeUser;
    35. /** 起数 */
    36. @JsonProperty("lastIndex")
    37. private BigDecimal lastIndex;
    38. /** 止数 */
    39. @JsonProperty("currentIndex")
    40. private BigDecimal currentIndex;
    41. /** 倍率 */
    42. @JsonProperty("multiple")
    43. private BigDecimal multiple;
    44. /** 损耗 */
    45. @JsonProperty("loss")
    46. private BigDecimal loss;
    47. /** 数量 */
    48. @JsonProperty("num")
    49. private String num;
    50. /** 单价 */
    51. @JsonProperty("price")
    52. private String price;
    53. /** 金额 */
    54. @JsonProperty("total")
    55. private String total;
    56. /** 滞纳金 */
    57. @JsonProperty("lateFee")
    58. private String lateFee;
    59. /** 折扣 */
    60. @JsonProperty("discount")
    61. private String discount;
    62. /** 应收 */
    63. @JsonProperty("receivable")
    64. private String receivable;
    65. private Integer refundState;
    66. private Integer refundTimes;
    67. private String refundAmount;
    68. /** 流水记录 */
    69. @JsonProperty("payLogId")
    70. private String payLogId;
    71. /** 流水单号 */
    72. @JsonProperty("payLogNo")
    73. private String payLogNo;
    74. /** 支付状态 */
    75. @JsonProperty("payState")
    76. private String payState;
    77. @JsonProperty("payStateName")
    78. private String payStateName;
    79. /** 支付时间 */
    80. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    81. @JsonProperty("payTime")
    82. private Date payTime;
    83. public String getPayStateName() {
    84. if(StringUtils.isEmpty(this.getPayState())) {
    85. return "";
    86. }
    87. if(this.getPayState().equals(""+ ConstantsUtil.PAY_BILL_PAY_STATE_UNPAIED)) {
    88. return "待付款";
    89. }
    90. if(this.getPayState().equals(""+ConstantsUtil.PAY_BILL_PAY_STATE_PAYING)) {
    91. return "付款中";
    92. }
    93. return "";
    94. }
    95. }

    然后自定义模板, 注意里面细节:

    每一行数据都是一个对象,都在list 里面,

    所以看到 首个字段 和 末尾最后的字段 是有 括号的   {}:

     

    示例格式:

    {{fe: indexList t.resourceName

    t.feeItemName

    fd:(t.beginDate;yyyy-MM-dd)

    fd:(t.endDate;yyyy-MM-dd)

    fd:(t.deadline;yyyy-MM-dd)

    t.total

    t.lateFee}}

    然后把自定义模板文件丢到 静态资源路径下:

     

    然后是实现使用自定义模板,填充list数据导出excel文件:

    1. /**
    2. * 下载bill
    3. * 缴费通知单.xlsx excel导出 列表 指定模板
    4. * @param response
    5. */
    6. @GetMapping(value = "download")
    7. public void downloadBill(HttpServletResponse response) throws IOException {
    8. List<PaymentBillEntity> list= paymentBillService.getUnpaiedAndPayingListByResourceLike();
    9. List<PaymentBillListVO> listVO = JsonUtil.getJsonToList (list, PaymentBillListVO.class);
    10. //获取导出模板地址
    11. ClassPathResource classPathResource = new ClassPathResource("static/zhaoxinpms/fee_notify.xlsx");
    12. String path = classPathResource.getPath ();
    13. TemplateExportParams templateExportParams1 = new TemplateExportParams(path);
    14. Map<String,Object> map = new HashMap<String,Object> (100);
    15. map.put("indexList",listVO);
    16. // 2.执行excel导出
    17. Workbook workbook = ExcelExportUtil.exportExcel (templateExportParams1, map);
    18. // 3.下载文件
    19. String fileName="缴费通知单.xlsx";
    20. try{
    21. response = ServletUtil.getResponse();
    22. response.setCharacterEncoding("UTF-8");
    23. response.setHeader("content-Type", "application/vnd.ms-excel");
    24. response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
    25. response.setHeader("download-filename", URLEncoder.encode(fileName, "UTF-8"));
    26. workbook.write(response.getOutputStream());
    27. }catch (IOException e){
    28. e.printStackTrace ();
    29. }
    30. }

    调用一下接口,看看效果:

     

    excel文件内容:

     

    好了,该篇就到这,谢谢观看。

  • 相关阅读:
    【每日一题】1094. 拼车
    Android JankStats 发布 Alpha 版本啦
    Lingo代码写下列问题
    国庆作业 day1
    什么是原生IP?原生IP与住宅IP有何区别?
    PySpark数据分析基础:PySpark基础功能及DataFrame操作基础语法详解
    深入Linux——权限的相关概念与操作
    《深入浅出.NET框架设计与实现》笔记6.2——ASP.NET Core应用程序多种运行模式之二——IIS 服务承载
    2023年咸阳市《网络建设与运维》赛题解析------三、无线配置
    【leetcode 力扣刷题】栈和队列的基础知识 + 栈的经典应用—匹配
  • 原文地址:https://blog.csdn.net/qq_34709784/article/details/126377702