生成通用的报告模板
一、先制作ftl文件(一定要用word,wps生成会有问题)
- 先在新建的word中写入模板
- 另存为xml文件
- 将xml文件更名为ftl文件
- 更改ftl文件中的内容
全局搜索,将变量替换,图片是base64形式的。
我们设定了三个变量,title,content,image64
二、将ftl文件转成word文件
将ftl文件放在src的com.kaisnm.template目录下。
package com.kaisnm.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class DocumentHandler {
private Configuration configuration = null;
public DocumentHandler() {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
}
/**
*
* @param dataMap 要填入模板的数据文件 HashMap<String,Object>()
* @param templatePath 要填入模板文件路径 "/com/kaisnm/template"
* @param templateName 要填入模板文件名称
* @param outPath 输出文件的路径
*/
public void createDocXml(Map dataMap, String templatePath,String templateName, String outPath) {
configuration.setClassForTemplateLoading(this.getClass(), templatePath);
Template t = null;
try {
t = configuration.getTemplate(templateName);
} catch (IOException e) {
e.printStackTrace();
}
// 输出文档路径及名称
File outFile = new File(outPath);
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
} catch (Exception e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
DocumentHandler documentHandler = new DocumentHandler();
Map<String,String> map = new HashMap<>();
//base64:格式: data:image/png;base64,xxx*** ,我们需要的是xxx***字符串
String image64 = "";
map.put("title","这个是我要的标题");
map.put("content","这个是我要的内容");
map.put("image64",image64);
documentHandler.createDocXml(map, "/com/kaisnm/template", "test.ftl", "bb.doc");
}
}
运行main函数
在指定的目录下生成了bb.doc,生成word完成
三、报告中需要使用echart的图表,我们需要获取echart的base64图片
<script type="text/javascript">
// 路径配置 获取echart的库
require.config({
paths: {
echarts: 'http://***/lib/echart/dist',
}
});
// 使用
require(
[
'echarts',
'echarts/theme/macarons',
'echarts/chart/bar',// 使用柱状图就加载bar模块,按需加载
'echarts/chart/line'// 使用直线
],
function (ec) {
// 基于准备好的dom,初始化echarts实例
var myChart = ec.init(document.getElementById('myChart1'));
var option = {
animation: false,
tooltip: {
trigger: 'axis'
},
calculable: true,
xAxis: [
{
axisLabel: {
rotate: 30,
},
type: 'category',
data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] //初始化echart的数据
}
],
yAxis: [
{
type: 'value'
}
],
series: [
{
name: '销售额',
type: 'bar',
data: [2, 3, 4, 5, 6, 7, 8, 9, 0]
}
]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
document.getElementById('image1Url').value = myChart.getDataURL('png');
}
);
</script>
document.getElementById(‘image1Url’).value = myChart.getDataURL(‘png’);
通过myChart.getDataURL(‘png’) 获取echarts的base64图片,再传入后台生成word文件即可。
注意:
生成word就只是使用freemarker的一个工具方法,如果生成的文件打不开,可能是参数原因。如果你的参数是个list,也可以在ftl文件中遍历出来。