...

package com.kingzheng.projects.word;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException; public class CreateWordTest {
// 参考 https://blog.csdn.net/chimaocai1302/article/details/100856467 private Configuration configuration = null; public CreateWordTest() {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
} /**@param dataMap 要填充的数据集合 *
* @param fileName 生成的word文档路劲与名称 * Template
* @throws UnsupportedEncodingException */
public void createDoc(Map<String,Object> dataMap,String fileName) throws UnsupportedEncodingException {
System.out.println(dataMap);
//读取模板 // /sdst4/src/com/jz/dzst/mail/zs_f.ftl
configuration.setClassForTemplateLoading(this.getClass(), "/com/jz/dzst/mail");
/* https://www.cnblogs.com/fangjian0423/p/freemarker-templateloading-question.html
Freemarker提供了3种加载模板目录的方法。 它使用Configuration类加载模板
public void setClassForTemplateLoading(Class clazz, String pathPrefix);
public void setDirectoryForTemplateLoading(File dir) throws IOException;
public void setServletContextForTemplateLoading(Object servletContext, String path);
*/
Template t=null;
try {
t = configuration.getTemplate("zs_f.ftl");
} catch (IOException e) {
e.printStackTrace();
} File outFile = new File(fileName);
Writer out = null;
FileOutputStream fos=null;
try {
fos = new FileOutputStream(outFile);
//注意对流的编码
OutputStreamWriter oWriter = new OutputStreamWriter(fos,"UTF-8");
out = new BufferedWriter(oWriter);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} try {
t.process(dataMap, out);
out.close();
fos.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
CreateWordTest createWordTest =new CreateWordTest();
// 太长 附件会变成 .bin 文件
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("xmbh", "1");
dataMap.put("gcmc", "如何使用freemarker生成word文档");
dataMap.put("jsdw", "我");
dataMap.put("nr", "首先建立ftl模板,引入freemarker的jar包,编写MDoc类读取模板,编写Main测试类,使用map集合填充模板");
dataMap.put("bh", "1");
try {
createWordTest.createDoc(dataMap, "F:/cs.doc");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} }

最新文章

  1. 当我们在谈论kmeans(1)
  2. MAPINFO中利用GridMaker工具创建栅格图层
  3. MySQL Innodb的两种表空间方式
  4. linux运维面试题汇总一
  5. Akka边学边写(2)-- Echo Server
  6. 【Xamarin挖墙脚系列:Xamarin正式发布了IOS的模拟器在Windows下】
  7. OpenCV-Python教程(5、初级滤波内容)
  8. Nagios+pnp4nagios+rrdtool 安装配置为nagios添加自定义插件(三)
  9. typeof做类型判断时容易犯下的错
  10. 初识servlet--未完成
  11. C++标准库之vector(各函数及其使用全)
  12. python运算符优先级问题
  13. 安卓高级EventBus使用详解
  14. 景观指数分析 - 初识FragStats4.2
  15. Java基础static的探究
  16. 7、TypeScript数据类型
  17. 模块化 Sea.js(CMD)规范 RequireJS(AMD)规范 的用法
  18. java设计模式:概述与GoF的23种设计模式
  19. checkbox用图片替换原始样式,并实现同样的功能
  20. :工厂模式1:方法模式--Pizza

热门文章

  1. 阿里:MySQL数据库规范
  2. Ubuntu 20.04上安装MySQL教程,ubuntu安装mysql
  3. Vulnhub实战-JIS-CTF_VulnUpload靶机&#128123;
  4. Dapr + .NET Core实战(十一)单机Dapr集群
  5. 11.5.2 LVS-NAT 实验
  6. C语言日记② 初识 C的内存
  7. AT3950 [AGC022E] Median Replace
  8. NX二次开发-调内部函数UGS::UICOMP_enum::set_width(int)更改BlockUI的枚举控件宽度
  9. Java中的函数式编程(二)函数式接口Functional Interface
  10. python查询对像所有方法