使用freemaker+echarts导出word模板

后台获取传来的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

posted on 2022-03-31 08:48  一往无前!  阅读(101)  评论(0编辑  收藏  举报