嘿,朋友们!在开发过程中,经常会碰到需要生成固定模板 Word 文档的需求,比如合同、报告之类的。Java 里有不少好用的工具能帮咱们搞定这事儿,像 Apache POI 和 FreeMarker 组合就很强大。下面咱就详细唠唠咋用它们来生成固定模板的 Word 文档。
1. 准备工作:引入依赖
咱用 Maven 来管理项目依赖,在 pom.xml
里加上下面这些依赖:
<dependencies>
<!-- Apache POI 处理 Word 文档 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>
<!-- FreeMarker 模板引擎 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
</dependencies>
2. 创建 FreeMarker 模板
先弄一个 Word 模板文件 template.docx
,把需要动态填充的内容用特定标识占位,比如 {name}
、{date}
这种。再把这个模板文件保存成 .ftl
格式,也就是 FreeMarker 模板文件。这里咱假设模板文件内容是这样的:
这是一份测试文档。
姓名:${name}
日期:${date}
3. Java 代码实现
下面是完整的 Java 代码示例:
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class WordGenerator {
public static void main(String[] args) {
// 配置 FreeMarker
Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
try {
// 设置模板文件所在目录
cfg.setDirectoryForTemplateLoading(new File("templates"));
// 获取模板
Template template = cfg.getTemplate("template.ftl");
// 准备数据
Map<String, Object> data = new HashMap<>();
data.put("name", "张三");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
data.put("date", sdf.format(new Date()));
// 生成替换后的内容
StringWriter writer = new StringWriter();
template.process(data, writer);
String content = writer.toString();
// 创建 Word 文档
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText(content);
// 保存 Word 文档
try (FileOutputStream out = new FileOutputStream("output.docx")) {
document.write(out);
System.out.println("Word 文档生成成功!");
}
} catch (IOException | TemplateException e) {
e.printStackTrace();
System.out.println("Word 文档生成失败:" + e.getMessage());
}
}
}
4. 代码解释
配置 FreeMarker
Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
cfg.setDirectoryForTemplateLoading(new File("templates"));
Template template = cfg.getTemplate("template.ftl");
这部分代码是在配置 FreeMarker,指定模板文件所在的目录,然后获取对应的模板。
准备数据
Map<String, Object> data = new HashMap<>();
data.put("name", "张三");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
data.put("date", sdf.format(new Date()));
这里准备了要填充到模板里的数据,像姓名和日期这些。
生成替换后的内容
StringWriter writer = new StringWriter();
template.process(data, writer);
String content = writer.toString();
用 FreeMarker 把数据填充到模板里,得到替换后的内容。
创建并保存 Word 文档
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText(content);
try (FileOutputStream out = new FileOutputStream("output.docx")) {
document.write(out);
System.out.println("Word 文档生成成功!");
}
用 Apache POI 创建一个新的 Word 文档,把替换后的内容添加进去,最后保存成 output.docx
文件。
嘿,朋友们!按照上面的步骤,就能用 Java 轻松生成固定模板的 Word 文档啦。赶紧动手试试,让你的程序也能高效生成文档!