java生成word

两种方案

一、poi-tl生成word

XML 复制代码
<dependency>
	<groupId>com.deepoove</groupId>
	<artifactId>poi-tl</artifactId>
	<version>1.12.1</version>
</dependency>
java 复制代码
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

XML 复制代码
<properties>
	<docx4j.version>8.3.10</docx4j.version>
</properties>
XML 复制代码
<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>
java 复制代码
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模板:注意模板里面的中文最好全部设置成微软雅黑或者宋体,不然会乱码

相关推荐
怒放吧德德1 分钟前
Netty 4.2 入门指南:从概念到第一个程序
java·后端·netty
雨中飘荡的记忆2 小时前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案
java·redis·后端
心之语歌4 小时前
基于注解+拦截器的API动态路由实现方案
java·后端
华仔啊5 小时前
Stream 代码越写越难看?JDFrame 让 Java 逻辑回归优雅
java·后端
ray_liang5 小时前
用六边形架构与整洁架构对比是伪命题?
java·架构
Ray Liang7 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Java水解7 小时前
Java 中间件:Dubbo 服务降级(Mock 机制)
java·后端
SimonKing11 小时前
OpenCode AI辅助编程,不一样的编程思路,不写一行代码
java·后端·程序员
FastBean11 小时前
Jackson View Extension Spring Boot Starter
java·后端
Seven9712 小时前
剑指offer-79、最⻓不含重复字符的⼦字符串
java