关于freemark将数据导入Excel步骤

关于freemark将数据导入Excel

1.在Excel中生成你需要的格式,样式,颜色
如图:然后${实体类的缩写.属性名}

在这里插入图片描述
2.另存为.xml文件在这里插入图片描述
3.将生成的excel-template.xml文件放到你的项目中
resource文件夹下建一个template
在这里插入图片描述
4.工具类:

 @RequestMapping("downloadExcel3")
    public void downloadExcel3(HttpServletResponse response)  {
    //获取数据
        List list = clothingService.queryAllListNoPage();
        Map map = new HashMap();
        //把数据存放到Map中,并将所有的信息都存到map中例如:autor等够可以进行显示
        map.put("clothingList", list);
        //map.put("productList", list);
        Configuration configuration = new Configuration();
        //configuration.setDefaultEncoding("utf-8");
        //有两种方式获取你的模板,模板在项目中时用第一个,模板在本地时用第二个。
        //注意:两种方式的路径都只需要写到模板的上一级目录
        configuration.setClassForTemplateLoading(this.getClass(), "/template");
        //  configuration.setDirectoryForTemplateLoading(new File("C:/"));
        //File outFile = new File("D:/outFile/"+Math.random()*10000+".xls");//输出路径
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + UUID.randomUUID().toString() + ".xlsx");
        Template t = null;
        OutputStream out = null;
        OutputStreamWriter writer = null;
        try {
            t = configuration.getTemplate("excel-template.xml", "utf-8"); //文件名,你生成的excel-template.xml模板的名字,必须一样
            out = response.getOutputStream();
            writer = new OutputStreamWriter(out, "utf-8");
            t.process(map, writer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (writer != null) {
                    writer.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

5.编辑excel-template.xml文件
在这里插入图片描述
然后就可以了
执行就可以了

目前对于日期还有空的数据导入还存在问题,后续会补上

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值