freemarker用模板输出指定格式word文档
2016-08-11 14:35
393 查看
零基础步骤阅读。。。
1、创建一个word文档,以w为例
2、在w文档里输入(姓名:name),并另存为.xml格式
3、在firstobject XMLEditor(一款编辑xml的小软件)中打开x.xml文件
4、找到输入的name,在name加上&{}(freemarker的读取格式)即:${name}
5、创建一个项目,引进freemarker的jar包
6、将x.xml文件加载到项目中,并改格式为.ftl
7、创建ExportTemplateWord类
public class ExportTemplateWord { public void exportWord() { try { Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("name", "zhangsan");//向dateMap中输入数据 Configuration configuration = new Configuration(); // 设置编码 configuration.setDefaultEncoding("UTF-8"); // 设置文件所在目录的路径 configuration.setDirectoryForTemplateLoading(new File( "src/com/export/template"));//模板路径 // 获取模版 Template template = configuration.getTemplate("w.ftl");//模板文件 File outFile = new File("e:\\outword.doc");// 设置输出文件名,和保存路径 // 将模板和数据模型合并生成文件 重点设置编码集 BufferedWriter out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(outFile), "UTF-8")); // 生成文件 template.process(dataMap, out); // 关闭流 out.flush(); out.close(); } catch (Exception e) { throw new RuntimeException(e + "打印word"); } }
8、创建一个测试类test,匿名实例ExportTemplateWord对象调用exportWord()方法
public class Test { public static void main(String[] args) { new ExportTemplateWord().exportWord(); } }
9、现在已经生成word文档了,快去指定路径下查看吧
相关文章推荐
- Java Web项目中利用FreeMarker模板生成的word文档,其它格式文档同理,如pdf,txt,java,xml等等
- Java Web项目中利用FreeMarker模板生成的word文档,其它格式文档同理,如pdf,txt,java,xml等等
- 基于springboot的freemarker创建指定格式的word文档
- java利用freemarker生成简单格式的word文档
- 在java 用xml模板通过freemarker生成word文档
- framemaker 导出word文档,利用list遍历输出换行以及保留段落格式(首行空两格)
- java利用Freemarker模板生成格式友好的doc文档(这种方式不支持docx)
- 利用freemarker 在模板里面写入动态数据,动态表格,图片插入并生成word文档
- 在FreeMarker模板中输出rfc 822标准格式时间
- Xml Publisher 程序控制模板输出格式(word, pdf)
- 利用freemarker 在模板里面写入动态数据,动态表格,图片插入并生成word文档
- java基于freemarker导出指定word格式带多图片源码。
- word文档从服务器导出(用freemarker模板导出)
- 使用freemarker按模板导出带表格word文档
- java利用Freemarker模板生成格式友好的doc或者docx文档
- 移动端无法打开FreeMarker模板生成的Word文档
- java使用freemarker模板导出word(带有合并单元格)文档
- freemarker根据模板生成word文档,换行
- Asp.Net输出指定内容的Word、Excel、TXT、HTM等类型的文档
- 利用ASP.NET输出指定内容的WORD、EXCEL、TXT、HTM等类型的文档