Java基于XML+FreeMarker导出Word
2013-03-28 14:09
627 查看
使用方法:
1、先用word制作模版,参数使用freemarker的写法 ${xxx} 暂时只支持这一种
2、然后将word另存为xml
3、再将xml拷贝一份,后缀名改为ftl
4、freemarker解析ftl输出word
测试action
1、先用word制作模版,参数使用freemarker的写法 ${xxx} 暂时只支持这一种
2、然后将word另存为xml
3、再将xml拷贝一份,后缀名改为ftl
4、freemarker解析ftl输出word
public class WordExportAction extends AbstractAction { private Configuration configuration = null; private Template template = null;// 模版 private void init(String filename, String templatePath) throws Exception { response.setContentType("application/msword;charset=UTF-8"); response.setHeader("Content-Disposition", "inline;filename=" + java.net.URLEncoder.encode(filename, "UTF-8")); // 客户端不缓存 response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); configuration = new Configuration(); configuration.setDefaultEncoding("utf-8"); String templateName = templatePath.substring(templatePath .lastIndexOf("\\")); String templateRoot = templatePath.substring(0, templatePath .lastIndexOf("\\")); // 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载, configuration.setDirectoryForTemplateLoading(new File(templateRoot)); template = configuration.getTemplate(templateName); template.setEncoding("utf-8"); // 指定模板如何检索数据模型,这是一个高级的主题了… // 但先可以这么来用: configuration.setObjectWrapper(new DefaultObjectWrapper()); } /** * 导出word * @param filename 导出的文件名 * @param dataMap 参数集合 * @param templatePath 模版路径 * @throws Exception */ public void export(String filename, Map dataMap, String templatePath) throws Exception { // 初始化 init(filename, templatePath); //执行导出 template.process(dataMap, response.getWriter()); } }
测试action
public class ExportWordTestAction extends WordExportAction{ public String doExport() throws Exception{ Map dataMap = new HashMap(); // 在根中放入字符串"user" dataMap.put("dept", "研发部"); dataMap.put("code", "r$d"); dataMap.put("date", "2013-3-3"); dataMap.put("addr", "天府软件园"); String templatePath = "jsp/demosys/reports/word/test.ftl"; export("03月27日常州03-⊥-C50停电申请单.doc",dataMap,templatePath); return null; } }
相关文章推荐
- java freemarker 通过ftl模板文件导出word文件发现在有中文地方出现在乱码,打开word文件提示xml错误解决办法
- java使用Freemarker+xml导出word
- java导出word(2003版doc格式) FreeMarker+XML实现
- java基于freemarker导出指定word格式带多图片源码。
- java导出2007版word(docx格式)freemarker + xml 实现
- java导出2007版word(docx格式)freemarker + xml 实现
- java 导出word 利用freemarker指令更改xml 导出word docx文档 (3)
- Java Web项目中利用FreeMarker模板生成的word文档,其它格式文档同理,如pdf,txt,java,xml等等
- Java用freemarker导出word
- JAVA FreeMarker导出word含图片
- Java实现用Freemarker完美导出word文档(带图片)
- Java用freemarker导出word文档
- java操作xml导出word
- FreeMarker + xml 导出word
- Web导出(二)之Freemarker+XML导出Word
- java导出word之freemarker导出(详细教程)
- FreeMarker + xml 导出word
- 【Java】导出word文档之freemarker导出
- Java用freemarker导出word
- java用freemarker导出数据到word(含多图片)