zoukankan      html  css  js  c++  java
  • spring boot freemarker 导出word 带echarts图形报表

    创建word文件内容如下

    将word导出为xml格式

    将文件后缀名改为 .ftl

    打开文件 修改图片的数据内容使用表达式代替

    修改后

    后查看${username}是否分家了,如果分家了将其多余部分删除 使其团聚

    在springboot项目中添加freemarker依赖

    <!-- 导出word文档-->
            <dependency>
                <groupId>org.freemarker</groupId>
                <artifactId>freemarker</artifactId>
                <version>2.3.20</version>
            </dependency>

    将生成的test.ftl放在 resources/templates文件夹下

    html中添加echarts

    <div id="container" style="height: 100%;"></div>
    <a onclick="exportImage()">导出</a>
    var dom = document.getElementById("container");
    var myChart = echarts.init(dom);
    var app = {};
    option = null;
    option = {
        xAxis: {
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
            type: 'value'
        },
        series: [{
            data: [820, 932, 901, 934, 1290, 1330, 1320],
            type: 'line'
        }]
    };
    ;
    if (option && typeof option === "object") {
        myChart.setOption(option, true);
    }

    添加导出触发事件方法

    function exportImage(){
                //获取Echart图形报表生成的Base64编码格式的数据
                var imgData = myChart.getConnectedDataURL();
                $.post('/word',{'imgData':imgData},function (data) {
                    alert(data);
                })
    }

    controller 中的方法

    @RequestMapping("/word")
        @ResponseBody
        public String generateWord(String imgData){
            // 传递过程中  "+" 变为了 " " ,所以需要替换
            String newImageInfo = imgData.replaceAll(" ", "+");
            // 数据中:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABI4AAAEsCAYAAAClh/jbAAA ...
            // 在"base64,"之后的才是图片信息
            String[] arr = newImageInfo.split("base64,");
    
            //添加模板数据
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("username","张三");
            dataMap.put("imgData",arr[1]);
    
            //文件生成路径
            String wordFilePath = "D:\ftl";
            //文件生成名称(因为是2003版本的xml模板,这里使用.doc后缀,如果使用.docx后缀生成的文件有问题)
            String wordFileName = "演示文档.doc";
            //模板文件名称
            String templateFileName = "test.ftl";
    
            //生成word文档
            Boolean result = WordUtil.writeWordReport(wordFilePath, wordFileName, templateFileName, dataMap);
            if(result){
                return "success";
            }else {
                return "error";
            }
        }

    创建WordUtil.java

    其中代码如下

    private static final String FTL_FP = "/templates/"; //模板路径
    
    private static Configuration configuration = null;
        static{
            configuration = new Configuration();
            configuration.setDefaultEncoding("utf-8");//设置默认的编码
            //读配置文件
    //        path = PropertiesUtil.get("FILE_PATH")+"/";
    
        }
    
    public static Boolean writeWordReport(String wordFilePath,String wordFileName,String templateFileName, Map<String, Object> beanParams) {
            Writer out = null;
            try {
                configuration.setClassForTemplateLoading(WordUtil.class,FTL_FP);
                Template template = configuration.getTemplate(templateFileName, "UTF-8");
    
                //获取文件目录,如果不存在则创建
                String filePath = "";
                int index = wordFilePath.lastIndexOf(File.separator);
                if(index != wordFilePath.length()-1){
                    filePath = wordFilePath+ File.separator;
                }else {
                    filePath = wordFilePath;
                }
                File file1 = new File(filePath);
                if(!file1.exists()){
                    file1.mkdirs();
                }
    
                //输出文件
                File file = new File(filePath+wordFileName);
                FileOutputStream fos = new FileOutputStream(file);
                out = new OutputStreamWriter(fos, "UTF-8");
                template.process(beanParams, out);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }finally{
                try {
                    if(out != null) {
                        out.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    点击导出可生成word文件!

  • 相关阅读:
    [转]Ubuntu设置Redhat风格的SHELL提示符PS1属性
    [转]Ubuntu Adsl 上网
    [转]Bash中的PS1详解
    Verilog 关于用task仿真应注意的一个问题
    [转]提高编程技能最有效的方法
    [转]ubuntu 终端常用命令
    [转]VMware Workstation 7.1 正式版 For Linux
    [转]Vim基本操作
    [转]Ubuntu Linux下设置IP的配置命令
    xilinxftp.newlocation
  • 原文地址:https://www.cnblogs.com/zmwf/p/11177176.html
Copyright © 2011-2022 走看看