1.在pom.xml添加依赖
<dependency>
<groupId>aspose.words</groupId> <!--自定义-->
<artifactId>aspose-words</artifactId> <!--自定义-->
<version>18.8</version> <!--自定义-->
<scope>system</scope> <!--system,类似provided,需要显式提供依赖的jar以后,Maven就不会在Repository中查找它-->
<systemPath>${basedir}/lib/aspose-words-18.8-jdk16-*****.jar</systemPath> <!--项目根目录下的lib文件夹下-->
</dependency>
2.创建一个测试的Word表格
3.具体的实现方法
package net.longjin.configCenter.caseConfig.controller;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.Node;
import com.aspose.words.NodeType;
import com.aspose.words.Range;
import com.aspose.words.Table;
/**
* 描述:AsposeUtils
*
* @author 何志鹏
* @ClassName:AsposeUtils
* @create 2019-07-08 14:16 Version 1.0
*/
public class AsposeUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(AsposeUtils.class);
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = AsposeUtils.class.getClassLoader().getResourceAsStream("com.aspose.words.lic.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 保存doc path保存目录 doc原文档
*
* @param
* @param
* @throws Exception
*/
public static void main(String[] agrs) throws Exception {
if (!getLicense()) {// 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
long old = System.currentTimeMillis();
String path = "C:/Users/longjin/Desktop/新建 DOC 文档 (2).doc";
Document doc = new Document(path);
// 获取word中的第一个表格,index指定表格位置
Table table = (Table) doc.getChild(NodeType.TABLE, 0, true);
List<Staff> staffList = new ArrayList<>();
Staff staff = new Staff();
staff.setName("何志鹏");
staff.setSex("男");
staff.setiDNum("421123199108146035");
staff.setPhoneNum("11");
staff.setPost("java");
staff.setDictName("测试1");
Staff staff2 = new Staff();
staff2.setName("何志鹏2");
staff2.setSex("男2");
staff2.setiDNum("421123199108146035+2");
staff2.setPhoneNum("112");
staff2.setPost("java2");
staff2.setDictName("测试2");
Staff staff3 = new Staff();
staff3.setName("何志鹏3");
staff3.setSex("男3");
staff3.setiDNum("421123199108146035+3");
staff3.setPhoneNum("113");
staff3.setPost("java3");
staff3.setDictName("测试3");
Staff staff4 = new Staff();
staff4.setName("何志鹏4");
staff4.setSex("男4");
staff4.setiDNum("421123199108146035+3");
staff4.setPhoneNum("114");
staff4.setPost("java4");
staff4.setDictName("测试4");
staffList.add(staff);
staffList.add(staff2);
staffList.add(staff3);
staffList.add(staff4);
for (Staff staff1 : staffList) { // 替换变量
Node deepClone = table.getLastRow().deepClone(true);
Range range = table.getLastRow().getRange();
range.replace("name", staff1.getName(), true, true);
range.replace("sex", staff1.getSex(), true, true);
range.replace("post", staff1.getiDNum(), true, true);
range.replace("idNum", staff1.getPhoneNum(), true, true);
range.replace("phoneNum", staff1.getPost(), true, true);
range.replace("dicName", staff1.getDictName(), true, true);
range.replace("7", staff1.getDictName(), true, true);
table.getRows().add(deepClone);
}
table.getLastRow().remove();
doc.save(path);
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
}
}
4.效果如下: