一、制作.ftl后缀的word模板文件
1、新建一个word文档模板
2、将word文档另存为xml并改名为.ftl后缀的文件
另存完之后关闭word文档,使用文本编辑器编写表达式替换显示的图片或表格数据,将演示文档.xml的后缀修改为.ftl,然后使用文本编辑器打开demo.ftl文件
3、修改.ftl文件并生成最终的模板文件
① 修改图片的数据内容使用表达式代替
替换之后如下:
② 在数据表格中添加循环标签
二、通过模板文件生成word文档
1.在echarts.jsp添加一个Echarts图形报表
var myChart,option;
//获取图表位置
myChart = echarts.init(document.getElementById("main"),'infographic');
myChart.showLoading({
text : "图表数据正在努力加载..."
});
option = {
animation:false,
title : {
text: '项目阶段分布情况分析',
//subtext: '饼状图',
x:'center'
},
tooltip : {
trigger: 'item',
formatter: "{a} <br/>{b} : {c} ({d}%)"
},
//color:['red', 'green','yellow','blueviolet'],
legend: {
//orient: 'vertical',竖向
//left: 'left',左边
bottom: 10,
left: 'center',
data: ['在谈','签约','开工','投产']
},
toolbox: {
feature: {
mark : {show: true},
//magicType : {show: true, type: ['line', 'bar','stack','tiled']},
//dataView : {show: true, readOnly: false},
/* magicType : {
show: true,
type: ['pie', 'funnel'],
option: {
funnel: {
x: '25%',
width: '50%',
funnelAlign: 'left',
max: 1700
}
}
}, */
restore : {show: true},
saveAsImage : {show: true}
}
},
series : [
{
name: '项目数',
type: 'pie',
radius : '55%',
center: ['50%', '50%'],
data:[
{value:335, name:'在谈'},
{value:310, name:'签约 '},
{value:234, name:'开工'},
{value:135, name:'投产'},
],
itemStyle: {
emphasis: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
},
normal:{
label:{
show: true,
formatter: '{b} : {c}\n ({d}%)' ,
length:5
},
labelLine :{show:true}
}
}
}
]
// color: ['rgb(254,67,101)','rgb(252,157,154)','rgb(249,205,173)','rgb(200,200,169)']
};
setTimeout(function(){
// lineCharts.hideLoading();//取消loading
// lineCharts.setOption({});//载入设置
myChart.hideLoading();
myChart.setOption(option);//将图表内容格式内容放入到myChart位置
//获取Echart图形报表生成的Base64编码格式的数据
var imgData = myChart.getConnectedDataURL();
$.post('xiangMuTongJiAction!generateWord',{'imgData':imgData},function (data) {
alert(data);
},'json');
},3000);
2、后台处理请求并设置模板数据
/**
* 将echarts图表导入word
* @param imgData
* @return
*/
public String generateWord(){
String imgData = request.getParameter("imgData");
//传递过程“+”变为了“”,所以需要替换
String newImageInfo = imgData.replaceAll(" ", "+");
// 数据中:data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABI4AAAEsCAYAAAClh/jbAAA ...
// 在"base64,"之后的才是图片信息
String[] arr = newImageInfo.split("base64,");
//添加模板数据
Map<String,Object> dataMap = new HashMap<String,Object>();
dataMap.put("userName", "张三");
dataMap.put("imgData", arr[1]);
FdipJiLian fdipJiLian1 = new FdipJiLian();
fdipJiLian1.setId("1");
fdipJiLian1.setName("name1");
FdipJiLian fdipJiLian2 = new FdipJiLian();
fdipJiLian2.setId("2");
fdipJiLian2.setName("name2");
FdipJiLian fdipJiLian3 = new FdipJiLian();
fdipJiLian3.setId("3");
fdipJiLian3.setName("name3");
List<FdipJiLian> fdipJiLianList = new ArrayList<FdipJiLian>();
fdipJiLianList.add(fdipJiLian1);
fdipJiLianList.add(fdipJiLian2);
fdipJiLianList.add(fdipJiLian3);
dataMap.put("fdipJiLianList",fdipJiLianList);
//文件生成路径
String wordFilePath = "D:\\ftl";
//文件生成名称(因为是2003版本的xml模板,这里使用.doc后缀,如果使用.docx后缀生成的文件有问题)
String wordFileName = "演示文档.doc";
//模板路径
String templatePath = "D:\\ftl";
//模板文件名称
String templateFileName = "demo.ftl";
//生成word文档
WordUtil.writeWordReport(wordFilePath, wordFileName, templatePath, templateFileName, dataMap,response);
return null;
}
4、生成word文档的工具类方法
/**
* 根据freemarker生成word文档并存到指定目录
* @param wordFilePath word文件生成的目录
* @param wordFileName word文件名
* @param templatePath 模板文件所在的目录
* @param templateFileName 模板文件名
* @param beanParams 生成word文件所需要的模板数据
* @return
*/
public static String writeWordReport(String wordFilePath,String wordFileName,
String templatePath,String templateFileName, Map<String, Object> beanParams,HttpServletResponse response) {
Configuration config = new Configuration(Configuration.getVersion());
// Writer out = null;
try {
config.setDirectoryForTemplateLoading(new File(templatePath));
Template template = config.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);*/
//方式二:实现文件web端下载
//文件名
String fileName =wordFileName;
// 前端响应(HttpServletResponse)
response.setCharacterEncoding("UTF-8");
response.setContentType("application/vnd.ms-word");
response.setHeader("Content-disposition", "attachment;filename="+new String(fileName.getBytes("gb2312"), "ISO8859-1" ));
// response.setContentType("application/octet-stream; charset=UTF-8");
// response.setHeader("content-disposition", "attachment;filename=" + new SimpleDateFormat("yyyyMMddHH:mm:ss").format(new Date()) + ".doc");
Writer out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(),"utf-8"));//重点:一定要注意编码格式,漏写编码格式将导致word无法打开(xml非法字符)
// 写入数据
template.process(beanParams, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
终于完成,参考资料链接如下:
https://blog.csdn.net/zianY/article/details/84982607
https://blog.csdn.net/shen525758203/article/details/48653715