java生成word

两种方案

一、poi-tl生成word

<dependency>
	<groupId>com.deepoove</groupId>
	<artifactId>poi-tl</artifactId>
	<version>1.12.1</version>
</dependency>
public static void main(String[] args) throws Exception {
	String path = "C:/siefile/bak11/v9/";
	String templateUrl = path + "poitl-template.docx";
	String outUrl = path + "poi-tl测试.docx";
	Map<String, Object> data = new HashMap<>();
	data.put("detail", "用这些语言编写的程序和上面我们讲的命令行客户端");
	data.put("remark", "对mysql-connector-java源码的分析");

	RowRenderData row0 = Rows.of("姓名", "学历").textColor("FFFFFF")
			.bgColor("4472C4").center().create();
	RowRenderData row1 = Rows.create("李四", "博士");
	data.put("testTable", Tables.create(row0, row1));

	XWPFTemplate render = XWPFTemplate.compile(templateUrl).render(data);
	render.writeToFile(outUrl);
	System.out.println("========done========");
}

word模板:注意模板里面的中文最好全部设置成微软雅黑或者宋体,不然会乱码

二、docx4j生成word

<properties>
	<docx4j.version>8.3.10</docx4j.version>
</properties>
<dependency>
	<groupId>org.docx4j</groupId>
	<artifactId>docx4j-JAXB-Internal</artifactId>
	<version>${docx4j.version}</version>
</dependency>
<dependency>
	<groupId>org.docx4j</groupId>
	<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
	<version>${docx4j.version}</version>
</dependency>
<dependency>
	<groupId>org.docx4j</groupId>
	<artifactId>docx4j-export-fo</artifactId>
	<version>${docx4j.version}</version>
</dependency>
public static void main(String[] args) throws Exception {
	String path = "C:/siefile/bak11/v9/";
	String templateFilePath = path + "docx4j-template.docx";
	String outFilePath = path + "docx4j测试.docx";

	// 加载word模板
	WordprocessingMLPackage wordPackage = WordprocessingMLPackage.load(new File(templateFilePath));
	MainDocumentPart documentPart = wordPackage.getMainDocumentPart();

	// 找到表格第二行,然后进行变量替换
	ClassFinder find = new ClassFinder(Tbl.class);
	new TraversalUtil(documentPart.getContent(), find);
	Tbl table = (Tbl) find.results.get(0);
	List<Object> tableContent = table.getContent();
	Tr templateTr = (Tr) tableContent.get(1);
	String templateTrXml = XmlUtils.marshaltoString(templateTr);
	List<Map> list = getTableData();
	for (Map map : list) {
		Tr newTr = (Tr) XmlUtils.unmarshallFromTemplate(templateTrXml, map);
		tableContent.add(newTr);
	}
	tableContent.remove(1);

	// 普通的变量替换
	Map<String, String> data = new HashMap<>();
	data.put("detail", "用这些语言编写的程序和上面我们讲的命令行客户端");
	data.put("remark", "对mysql-connector-java源码的分析");
	VariablePrepare.prepare(wordPackage);
	documentPart.variableReplace(data);

	Docx4J.save(wordPackage, new FileOutputStream(outFilePath));
	System.out.println("=========done========");
}

private static List<Map> getTableData() {
	List list = new ArrayList();
	for (int index = 0; index < 5; index++) {
		HashMap map = new HashMap();
		map.put("name", "name_" + index);
		map.put("age", "age_" + index);
		list.add(map);
	}
	return list;
}

word模板:注意模板里面的中文最好全部设置成微软雅黑或者宋体,不然会乱码

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值