欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot freemarker 导出word 带echarts图形报表

程序员文章站 2022-06-17 11:29:40
创建word文件内容如下 将word导出为xml格式 将文件后缀名改为 .ftl 打开文件 修改图片的数据内容使用表达式代替 修改后 后查看${username}是否分家了,如果分家了将其多余部分删除 使其团聚 在springboot项目中添加freemarker依赖 将生成的test.ftl放在 ......

创建word文件内容如下

spring boot freemarker 导出word 带echarts图形报表

将word导出为xml格式

spring boot freemarker 导出word 带echarts图形报表

将文件后缀名改为 .ftl

spring boot freemarker 导出word 带echarts图形报表

 

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

spring boot freemarker 导出word 带echarts图形报表

 

修改后

spring boot freemarker 导出word 带echarts图形报表

 

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

 

在springboot项目中添加freemarker依赖

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

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

spring boot freemarker 导出word 带echarts图形报表

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文件!