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文件!
spring boot freemarker 导出word 带echarts图形报表的更多相关文章
- freemarker导出word带图片
导出word带图片 如果你需要在word中添加图片,那你就在第一步制作模板时,加入一张图片占位,然后打开xml文档,可以看到如下的一片base64编码后的代码: <w:binData w:nam ...
- 使用Freemarker导出Word文档(包含图片)代码实现及总结
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- freemarker导出word文档——WordXML格式解析
前不久,公司一个项目需要实现导出文档的功能,之前是一个同事在做,做了3个星期,终于完成了,但是在项目上线之后却发现导出的文档有问题,此时,这个同事已经离职,我自然成为接班者,要把导出功能实现,但是我看 ...
- freemarker导出word文档
使用freemarker导出word文档的过程 **************************************************************************** ...
- freemarker导出word档
1.word另存为xml:2.xml文件后缀名改成ftl:3.编写完整json字符串备用:4.修改ftl中动态字段为json中对应字段名:5.编写java代码自动生成word文件:(注意:换行用< ...
- spring boot+freemarker+spring security标签权限判断
spring boot+freemarker+spring security标签权限判断 SpringBoot+SpringSecurity+Freemarker项目中在页面上使用security标签 ...
- Spring boot Freemarker 获取ContextPath的方法
Spring boot Freemarker 获取ContextPath的两种方法: 1.自定义viewResolver,Spring boot中有一个viewResolver,这个和配置文件中的师徒 ...
- freemarker导出word的一些问题
首先,了解下freemarker导出word的流程: 参考https://www.cnblogs.com/llfy/p/9303208.html 异常一: freemarker.core.ParseE ...
- 关于使用freemarker导出word
java使用FreeMarker导出word 一. 先做一个word模板 二. 将该word文件另存为xml格式(注意是另存为,不是直接改扩展名) 三. 打开xml文件把要 ...
随机推荐
- android Camera2 API使用详解
原文:android Camera2 API使用详解 由于最近需要使用相机拍照等功能,鉴于老旧的相机API问题多多,而且新的设备都是基于安卓5.0以上的,于是本人决定研究一下安卓5.0新引入的Came ...
- C++成员函数指针错误用法警示(成员函数指针与高性能的C++委托,三篇),附好多评论
今天做一个成绩管理系统的并发引擎,用Qt做的,仿照QtConcurrent搞了个模板基类.这里为了隐藏细节,隔离变化,把并发的东西全部包含在模板基类中.子类只需注册需要并发执行的入口函数即可在单独线程 ...
- Linux下C语言RPC(远程过程调用)编程实例
在查看libc6-dev软件包提供的工具(用 dpkg -L libc6-dev 命令)的时候,发现此软件包提供了一个有用的工具rpcgen命令.通过rpcgen的man手册看到此工具的作用是把RPC ...
- WinForm TreeView单击,但是获取的是上一次点击选中的节点
/// <summary> /// MouseDown是鼠标按下事件发生在你鼠标单击事件之前,你单击鼠标发生了两个动作,一是鼠标按下二是鼠标抬起.执行之后,就会把SelectedNode转 ...
- 苹果ios用js的Date()出现NaN问题解决办法
原文:苹果ios用js的Date()出现NaN问题解决办法 ios使用如下方法获得NaN,安卓手机则是正常计算,解决方法是换个这个时间的格式 new Date("2017-04-28 23: ...
- 使用PNG实现半透明的窗体(使用GDI+)
Delphi中标准控件是不支持png图片的,据说从Window2000后增加gdiplus.dll库处理更多的gdi图像,其中包括png. 关键的几个api GdipCreateBitma ...
- 配置我的Ubuntu Server记(包括桌面及VNC,SSH,NTP,NFS服务) good
跟老板申请买了一台配置相对较好的计算机回来做GPU计算,当然,不能独享,所以做成服务器让大家都来用. 这篇日志用来记录配置过程中遇到的一些问题,以方便下次不需要到处谷歌度娘. 安装Server版系统 ...
- Azkaban学习之路(二)—— Azkaban 3.x 编译及部署
一.Azkaban 源码编译 1.1 下载并解压 Azkaban 在3.0版本之后就不提供对应的安装包,需要自己下载源码进行编译. 下载所需版本的源码,Azkaban的源码托管在GitHub上,地址为 ...
- JSON对象和JavaScript对象直接量的区别--不同之处
JSON对象和JS对象直接量 在工作当中,我们总是可以听到人说将数据转换为JSON对象,或者说把JSON对象转换为字符串之类的话,下面是关于JSON的具体说明. JSON对象并不是JavaScript ...
- js中新增动态属性
var cc = 'hell' var mm = { [cc](){ alert(33) } } mm.hell() 使用的就是数组形式