java 根据word xml模板生成word
这里用的是poi相关jar包以及freemarker插值技术实现,poi相关jar包这里不再述说
1,编辑word并保存为xml
2,把xml后缀改为ftl文件
3,前端代码
// alert(jsonStr);
var url = "${ctx}//exportWordXTOdsxdlsdbh.htm?jsonStr="+ jsonStr;
window.open(url, 'xxtitle');
4,后台代码
@RequestMapping("exportWordXTOgdlbh")
public void exportWordXTOgdlbh(HttpServletRequest request,HttpServletResponse response) throws Exception{
String str = request.getParameter("jsonStr");
//去掉单引号和双引号
str = str.replace("'", "");
str = str.replace("\"", "");
Map<String,Object> rootMap = new HashMap<String,Object>();
System.out.println(str);
Gson gson = new Gson();
rootMap = gson.fromJson(str, rootMap.getClass());
String name = "xxx--" + df.format(new Date()) + ".doc";
//设置MIMI类型,默认的是text/hmtl
response.setHeader("Content-Type", "application/msword");
//解决中文乱码,以及设置生成的文档名
response.setHeader("Content-Disposition", "attachment;filename="+ new String(name.getBytes("utf-8"), "ISO8859-1"));
//取得当前类所在的目录
String filePath = FileUtil.getClassesPath() + File.separator + "template"+ File.separator +"officeftl"+ File.separator +"zdjs";
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File(filePath));
//这里不设置编码的话,即是成功了有时也打不开文件,因为乱码
Template temp = cfg.getTemplate("exportWordXTOgdlbh.ftl","utf-8");
PrintWriter pw = response.getWriter();
/* 合并数据模型和模版*/
temp.process(rootMap, pw);
pw.flush();
pw.close();
}
5,注意事项
打开的xml要看看是否为途中¥{}的样式,如果被wold分割了,需要自己了解wold xml格式并去修改
最新文章
- jasmine test 页面测试工具
- css 文字溢出隐藏 带省略号
- 运行iis出现:The server has encountered an error while loading an application ……的解决办法【转】
- 集合、拆箱、装箱、自定义集合的foreach
- PHP array_chunk() 函数
- Apache(一)
- asp.net网站后台退出后,点后退按钮仍能进,如何安全退出
- 你应该知道CSS选择器技巧
- hdu 5007 水 弦
- idea中编译项目报错 java: javacTask: 源版本 1.8 需要目标版本 1.8
- iOS 关于UITableView的黑科技
- Android Studio精彩案例(四)《DrawerLayout使用详解仿网易新闻客户端侧边栏 》
- 零基础入门学习javase,应该怎么学?这是我给你的一个建议
- File类中的一些属性 添加删除文件夹
- oracle连表语法
- [Java基础复习] -- x. 正则表达式的使用
- 软件测试工程师这样面试,拿到offer的几率是80%
- 关于vim的折叠
- VideoView 监听视频格式不支持时的错误。
- Pandas 基础(3) - 生成 Dataframe 的几种方式
热门文章
- 【代码审计】XYHCMS V3.5任意文件读取漏洞分析
- Context namespace element &#39;component-scan&#39; and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher
- 2. Mysql数据库的入门知识
- pandas pivot_table 活学活用实例教程
- Prometheus 函数
- 老师的blog整理 .网络编程部分 .网络编程部分 前端部分 django基础部分
- [No000011E]Python教程1/9-Python简介
- Js高设笔记1-2章 defer and async
- idhttp与cookie
- DbGridEh 一个单元格的值改变时另一单元格的值随之改变