ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

使用freemaker+echarts导出word模板

2022-03-31 09:02:55  阅读:153  来源: 互联网

标签:map word String qsdb link new echarts 模板 freemaker


后台获取传来的base64图片,并对其进行处理,然后获取我们定义好的模板,将图片输出到我们定义好的模板中,放在我们的占位符指定的位置

    public ResultBody<?> exportXls(@RequestParam(required = false) Map map, HttpServletResponse response, HttpServletRequest request) throws IOException, TemplateException {

        System.out.println("--------------------------------------------------------------------------------------------");

        String  pie =  (String)map.get("pie");
        String  bar =  (String)map.get("bar");
        String  qsdb =  (String)map.get("qsdb");
        System.out.println(qsdb);


        //利用正则去除前面的无用字段,得到base64图片描述字段
        map.replace("pie",getImgString(pie));
        map.replace("bar",getImgString(bar));
        map.replace("qsdb",getImgString(qsdb));

        Configuration configuration = new Configuration(new Version("2.3.0"));
        configuration.setDefaultEncoding("utf-8");
        Writer outWriter = null;
        Template template = null;

        try {
            System.out.println("--------------------------------------------------------------------------------------------");
            OutputStream out = response.getOutputStream();
//            outWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("D:/2.docx")), "utf-8"), 10240);      // 获取本地模板,如果需要直接写入本地的文件中即可使用这个方法
            outWriter = new BufferedWriter(new OutputStreamWriter(out,"UTF-8"), 10240);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            configuration.setClassForTemplateLoading(this.getClass(),"/templates");
//            configuration.setDirectoryForTemplateLoading(new File("E:/"));     //如果定义好的模板在本地则使用此方法
            template = configuration.getTemplate("/2.ftl","UTF-8");
        }catch (Exception e) {
            e.printStackTrace();
        }
        template.process(map,outWriter);
        outWriter.flush();
//        outWriter.close();如果需要返回到前端的xml就不需要关闭,否则会出现错误

        return ResultBody.ok();
    }

前端代码

//下载报告
      handleExportWord() {
       this.data1['pie'] = this.chart1Data
          this.data1['bar'] = this.chart2Data
          this.data1['qsdb'] = this.chart3Data

        downFile("/statistics/exportXls", this.data1 ).then((data) => {    //获取后台传回的xml模板,然后通过浏览器下载文档
          if (!data) {
            this.$message.warning('文件下载失败')
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            //window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-word' }), "双评价" + '.word')
          } else {
            let url = window.URL.createObjectURL(data)
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', "test" + '.docx')
            document.body.appendChild(link)

            link.click()
            document.body.removeChild(link) //下载完成移除元素
            window.URL.revokeObjectURL(url) //释放掉blob对象
          }
        }).catch(err => {
          console.log(err)
        })

      },

 参考文档:https://www.jianshu.com/p/28e73b41895e

模板建立可以参考:https://blog.csdn.net/xx20cw/article/details/86539759

标签:map,word,String,qsdb,link,new,echarts,模板,freemaker
来源: https://www.cnblogs.com/1gaoyu/p/16080171.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有