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模板:注意模板里面的中文最好全部设置成微软雅黑或者宋体,不然会乱码

相关推荐
Magnum Lehar26 分钟前
3d游戏引擎EngineTest的系统实现3
java·开发语言·游戏引擎
就叫飞六吧40 分钟前
Spring Security 集成指南:避免 CORS 跨域问题
java·后端·spring
Mcworld85741 分钟前
java集合
java·开发语言·windows
天黑请闭眼1 小时前
IDEA:程序编译报错:java: Compilation failed: internal java compiler error
java·intellij-idea
动感光博2 小时前
Unity序列化字段、单例模式(Singleton Pattern)
unity·单例模式·c#
苍煜2 小时前
Maven构建流程详解:如何正确管理微服务间的依赖关系-当依赖的模块更新后,我应该如何重新构建主项目
java·微服务·maven
冼紫菜2 小时前
[特殊字符]CentOS 7.6 安装 JDK 11(适配国内服务器环境)
java·linux·服务器·后端·centos
isyangli_blog2 小时前
(1-4)Java Object类、Final、注解、设计模式、抽象类、接口、内部类
java·开发语言
qq_29494019102 小时前
WORD个人简历单页326款模版分享下载
word·个人简历·doc模版
JOYUAGV3 小时前
Word压缩解决方案
python·word