java基于模板导出Excel

1.添加easyexcel依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.2.4</version>
</dependency>

2.编写工具类

这边的StoreInfo是我的需求中用到的业务类,改成自己的即可。

public static void exportExcelByTemplate(String filePath, String templatePath, List<StoreInfo> list){
    ExcelWriter excelWriter = EasyExcel.write(filePath).withTemplate(templatePath).build();
    WriteSheet writeSheet = EasyExcel.writerSheet().build();
    FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
    if (list.size() > 0){
        Map<String, Object> map = new HashMap<>(64);
        String rkDate = list.get(0).getInTime().toString();
        String rkDateFormat = rkDate.substring(0, 4) + "年" + rkDate.substring(5, 7) + "月" + rkDate.substring(8, 10) + "日";
        map.put("rkDate", rkDateFormat);
        excelWriter.fill(map, writeSheet);
        excelWriter.fill(new FillWrapper("data", list), fillConfig, writeSheet);
    }
    excelWriter.finish();
}

3.编写接口代码

    @RequestMapping(value="/exportStoreList")
    @ResponseBody
    public Object exportStoreSignList(HttpServletResponse response) throws Exception{
        //随机生成文件名称
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String str = simpleDate.format(date);
        Random rand = new Random();
        int rannum = (int) (rand.nextDouble()*(99999-10000+1)+10000);
        String fileName =str+rannum+".xlsx";
        //服务器
//        String localFilePath = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest().getSession().getServletContext()
//                .getRealPath("//downloadFiles");
        //本地
      String localFilePath = "d:\\";
      //获取模板路径
        String templatePath = this.getClass().getClassLoader().getResource("templates/store_template.xlsx").getPath();
        List<StoreInfo> storeList = new ArrayList<>();
        StoreInfo storeInfo = new StoreInfo();
        storeInfo.setCardNum("321321199905071209");
        storeInfo.setCardType("身份证");
        storeInfo.setInReason("测试");
        storeInfo.setInTime(date);
        storeInfo.setOrgName("百度");
        storeInfo.setUserName("小王");
        storeList.add(storeInfo);
        EasyExportUtil.exportExcelByTemplate(localFilePath+fileName, templatePath, storeList);
        response.setCharacterEncoding("utf-8");
        //返回的数据类型
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        //响应头
        response.setHeader("Content-Disposition", "attachment;fileName="
                + fileName);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(localFilePath+"//"+fileName);
            byte[] data = new byte[fis.available()];
            fis.read(data);
            response.getOutputStream().write(data);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭输入输出流
            if(fis!=null) {
                fis.close();
            }
        }
        return null;
    }

4.模板文件

模板文件放进resource下面

 

 5.如果有不清晰的可以到我的博客下面下载源码参考

  • 1
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Silenchen11

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

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

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

打赏作者

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

抵扣说明:

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

余额充值