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

废话不多说
看正文
开始实战:
pom.xml依赖:
-
-
- <dependency>
- <groupId>cn.afterturngroupId>
- <artifactId>easypoi-baseartifactId>
- <version>3.0.3version>
- dependency>
- <dependency>
- <groupId>cn.afterturngroupId>
- <artifactId>easypoi-webartifactId>
- <version>3.0.3version>
- dependency>
- <dependency>
- <groupId>cn.afterturngroupId>
- <artifactId>easypoi-annotationartifactId>
- <version>3.0.3version>
- dependency>
我们导出数据的实体类 PaymentBillListVO.java
- @Data
- public class PaymentBillListVO {
- /** 主键 */
- private String id;
-
- @JsonProperty("orderId")
- private String orderId;
-
- /** 资源id */
- @JsonProperty("resourceId")
- private String resourceId;
-
- /** 资源名 */
- @JsonProperty("resourceName")
- private String resourceName;
-
- @JsonProperty("feeItemId")
- private String feeItemId;
-
- /** 收费项名 */
- @JsonProperty("feeItemName")
- private String feeItemName;
-
- /** 缴费单对应的周期 */
- // 日期输出格式化
- @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
- @JsonProperty("beginDate")
- private Date beginDate;
-
- /** 缴费单对应的周期 */
- // 日期输出格式化
- @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
- @JsonProperty("endDate")
- private Date endDate;
-
- // 缴费限期
- @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
- @JsonProperty("deadline")
- private Date deadline;
-
- /** 客户 */
- @JsonProperty("feeUser")
- private String feeUser;
-
- /** 起数 */
- @JsonProperty("lastIndex")
- private BigDecimal lastIndex;
-
- /** 止数 */
- @JsonProperty("currentIndex")
- private BigDecimal currentIndex;
-
- /** 倍率 */
- @JsonProperty("multiple")
- private BigDecimal multiple;
-
- /** 损耗 */
- @JsonProperty("loss")
- private BigDecimal loss;
-
- /** 数量 */
- @JsonProperty("num")
- private String num;
-
- /** 单价 */
- @JsonProperty("price")
- private String price;
-
- /** 金额 */
- @JsonProperty("total")
- private String total;
-
- /** 滞纳金 */
- @JsonProperty("lateFee")
- private String lateFee;
-
- /** 折扣 */
- @JsonProperty("discount")
- private String discount;
-
- /** 应收 */
- @JsonProperty("receivable")
- private String receivable;
-
- private Integer refundState;
- private Integer refundTimes;
- private String refundAmount;
-
- /** 流水记录 */
- @JsonProperty("payLogId")
- private String payLogId;
-
- /** 流水单号 */
- @JsonProperty("payLogNo")
- private String payLogNo;
-
- /** 支付状态 */
- @JsonProperty("payState")
- private String payState;
-
- @JsonProperty("payStateName")
- private String payStateName;
-
- /** 支付时间 */
- @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
- @JsonProperty("payTime")
- private Date payTime;
-
- public String getPayStateName() {
- if(StringUtils.isEmpty(this.getPayState())) {
- return "";
- }
- if(this.getPayState().equals(""+ ConstantsUtil.PAY_BILL_PAY_STATE_UNPAIED)) {
- return "待付款";
- }
- if(this.getPayState().equals(""+ConstantsUtil.PAY_BILL_PAY_STATE_PAYING)) {
- return "付款中";
- }
- return "";
- }
- }
然后自定义模板, 注意里面细节:
每一行数据都是一个对象,都在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文件:
- /**
- * 下载bill
- * 缴费通知单.xlsx excel导出 列表 指定模板
- * @param response
- */
- @GetMapping(value = "download")
- public void downloadBill(HttpServletResponse response) throws IOException {
- List<PaymentBillEntity> list= paymentBillService.getUnpaiedAndPayingListByResourceLike();
- List<PaymentBillListVO> listVO = JsonUtil.getJsonToList (list, PaymentBillListVO.class);
- //获取导出模板地址
- ClassPathResource classPathResource = new ClassPathResource("static/zhaoxinpms/fee_notify.xlsx");
- String path = classPathResource.getPath ();
- TemplateExportParams templateExportParams1 = new TemplateExportParams(path);
- Map<String,Object> map = new HashMap<String,Object> (100);
- map.put("indexList",listVO);
- // 2.执行excel导出
- Workbook workbook = ExcelExportUtil.exportExcel (templateExportParams1, map);
- // 3.下载文件
- String fileName="缴费通知单.xlsx";
- try{
- response = ServletUtil.getResponse();
- response.setCharacterEncoding("UTF-8");
- response.setHeader("content-Type", "application/vnd.ms-excel");
- response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
- response.setHeader("download-filename", URLEncoder.encode(fileName, "UTF-8"));
- workbook.write(response.getOutputStream());
- }catch (IOException e){
- e.printStackTrace ();
- }
-
-
- }
调用一下接口,看看效果:

excel文件内容:

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