easyexcel动态表头,复杂表头

需要导出的表头有一行是统计时间,需要导出时动态生成.

导出实体类,创建createHead方法,手写表头,动态生成第二行表头统计日期

@Data
public class ExportEntity {

    @ExcelIgnore
    private String id;

    private String projectCode;
    private String areaName;
    private String projectName;
    private String organizationUnit;
    private BigDecimal totalFund;
    private BigDecimal projectProgress;
    private BigDecimal fundProgress;


    public List<List<String>> createHead() {
        List<List<String>> head = new ArrayList<>();
        Date date = new Date();
        String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(date);
        head.add(Arrays.asList("统计表", "统计日期:" + dateStr, "项目基本信息", "项目编号"));
        head.add(Arrays.asList("统计表", "统计日期:" + dateStr, "项目基本信息", "城市"));
        head.add(Arrays.asList("统计表", "统计日期:" + dateStr, "项目基本信息", "项目名称"));
        head.add(Arrays.asList("统计表", "统计日期:" + dateStr, "项目基本信息", "单位"));
        head.add(Arrays.asList("统计表", "统计日期:" + dateStr, "资金信息", "总金额"));
        head.add(Arrays.asList("统计表", "统计日期:" + dateStr, "资金信息", ")"));
        head.add(Arrays.asList("统计表", "统计日期:" + dateStr, "进度", "项目进度"));
        head.add(Arrays.asList("统计表", "统计日期:" + dateStr, "进度", "资金进度"));

        return head;
    }
}

导出方法

public static void writeExcel(HttpServletResponse response, String filename, Class header, List dataList, List<List<String>> head) {
        log.info("开始写excel文件,文件名:{}", filename);
        log.info("header==",header);
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        String fileName;

        try {
            fileName = URLEncoder.encode(filename+".xlsx", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);

            EasyExcel.write(response.getOutputStream(), header)
                    .autoCloseStream(Boolean.FALSE)
                    .head(head)//添加表头
                    .sheet("sheet1").doWrite(dataList);
        } catch (IOException e) {
            log.info("写excel失败,异常栈信息:{}", e);
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<String, String>();
            map.put("status", "failure");
            map.put("message", "导出失败" + e.getMessage());
            try {
                response.getWriter().println(JSON.toJSONString(map));
            } catch (IOException ioException) {
                log.info("写excel失败,异常栈信息:{}", e);
            }
        }
    }
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值