Springboot 自定义模板导出Excel文件

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

 

记得加$ 记得加$ 记得加$ 记得加$ 记得加$ 记得加$ 记得加$ 记得加$ 记得加$ 记得加$ 记得加$

注意:

    模板这里有个小坑,就是当传入的值为数组时,需要遍历输出必须要放到表格里easypoi才能完整的遍历,对于表格以外的位置支持的并不是很好。
    模板遍历时,解析的数组子对象默认为“t”。直接用“t”来引用就可以了。也可以在后面自定义

以下就是模板常用的指令:
空格分割
三目运算 {{test ? obj:obj2}}
n: 表示 这个cell是数值类型 {{n:}}
le: 代表长度{{le:()}} 在if/else 运用{{le:() > 8 ? obj1 : obj2}}
fd: 格式化时间 {{fd:(obj;yyyy-MM-dd)}}
fn: 格式化数字 {{fn:(obj;###.00)}}
fe: 遍历数据,创建row
!fe: 遍历数据不创建row
$fe: 下移插入,把当前行,下面的行全部下移.size()行,然后插入
#fe: 横向遍历
v_fe: 横向遍历值
!if: 删除当前列 {{!if:(test)}}
单引号表示常量值 ‘’ 比如’1’ 那么输出的就是 1
&NULL& 空格
&INDEX& 表示循环中的序号,自动添加
]] 换行符 多行遍历导出
sum: 统计数据
cal: 基础的±X% 计算
dict: 字典
i18n: 国际化
————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

废话不多说

看正文

开始实战:

pom.xml依赖:

 
        <!-- 导入和导出-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.0.3</version>
        </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 ();
        }
 
 
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@百思不得奇解

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值