|
@@ -7,9 +7,12 @@ import com.dayou.annotation.WordTableColumn;
|
|
|
import com.dayou.utils.AsposeWordUtil;
|
|
|
import lombok.Builder;
|
|
|
import lombok.Data;
|
|
|
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
+import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.net.URI;
|
|
@@ -33,7 +36,6 @@ import java.util.List;
|
|
|
public class WordSimpleTests {
|
|
|
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 这里的属性名字要和word设置的域名一致
|
|
|
*/
|
|
@@ -74,6 +76,7 @@ public class WordSimpleTests {
|
|
|
/**
|
|
|
* 测试向word模板中动态填充数据
|
|
|
* 此处案例是设置封面
|
|
|
+ *
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
@Test
|
|
@@ -104,7 +107,7 @@ public class WordSimpleTests {
|
|
|
* 此处案例是拼接目录与声明的内容
|
|
|
*/
|
|
|
@Test
|
|
|
- void testAppendWord(){
|
|
|
+ void testAppendWord() {
|
|
|
try {
|
|
|
//加载源文档
|
|
|
Document doc1 = new Document("E:\\test\\word\\1_cover_output.doc"); // 封面
|
|
@@ -123,6 +126,7 @@ public class WordSimpleTests {
|
|
|
/**
|
|
|
* 测试向word模板中动态填充数据(带集合的情况)
|
|
|
* 此处案例是设置正文内容
|
|
|
+ *
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
@Test
|
|
@@ -130,7 +134,7 @@ public class WordSimpleTests {
|
|
|
// 设置信息
|
|
|
SimpleWordTableData data1 = SimpleWordTableData.builder().id("1").pledger("张三").contractNum("合同阿巴阿巴阿巴阿巴").guarantyStyle("担保").amount("1000万").build();
|
|
|
SimpleWordTableData data2 = SimpleWordTableData.builder().id("2").pledger("李四").contractNum("合同呜呼呜呼呜呼芜湖").guarantyStyle("抵押").amount("200028万").build();
|
|
|
- List<SimpleWordTableData> contractList = Arrays.asList(data1,data2);
|
|
|
+ List<SimpleWordTableData> contractList = Arrays.asList(data1, data2);
|
|
|
SimpleWordData wordData = SimpleWordData.builder()
|
|
|
.name1("大连银行股份有限公司成都分行")
|
|
|
.name2("四川宏淼科技发展有限公司债权")
|
|
@@ -140,7 +144,6 @@ public class WordSimpleTests {
|
|
|
.build();
|
|
|
|
|
|
|
|
|
-
|
|
|
// 读取模板文件
|
|
|
byte[] tmplWordByte = Files.readAllBytes(Paths.get("E:\\test\\word\\5_main.doc"));
|
|
|
|
|
@@ -158,6 +161,7 @@ public class WordSimpleTests {
|
|
|
* 测试向word指定位置插入另一个word
|
|
|
* 实测该方法无法插入页眉页脚
|
|
|
* 此处案例是将正文内容插入到拼接好目录与声明的报告中
|
|
|
+ *
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
@Test
|
|
@@ -185,12 +189,11 @@ public class WordSimpleTests {
|
|
|
void testUpdateCatalogueLink() throws Exception {
|
|
|
Document doc = new Document("E:\\test\\word\\6_success_output.doc");
|
|
|
// 还有一种方式是 doc.updateFields(); 方法,该方法会导致目录样式被更新掉。方便,但是不建议使用
|
|
|
- for (Field field : doc.getRange().getFields()){
|
|
|
- if (field.getType() == FieldType.FIELD_TOC)
|
|
|
- {
|
|
|
- FieldToc toc = (FieldToc)field;
|
|
|
+ for (Field field : doc.getRange().getFields()) {
|
|
|
+ if (field.getType() == FieldType.FIELD_TOC) {
|
|
|
+ FieldToc toc = (FieldToc) field;
|
|
|
toc.updatePageNumbers(); //定位toc只更新页码
|
|
|
- } else{
|
|
|
+ } else {
|
|
|
field.update();
|
|
|
}
|
|
|
}
|
|
@@ -200,6 +203,7 @@ public class WordSimpleTests {
|
|
|
/**
|
|
|
* 测试在指定书签处创建table
|
|
|
* 这里测试的是从无到有的创建一个table,和值填充到模板中已存在的table不一样
|
|
|
+ *
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
@Test
|
|
@@ -208,7 +212,7 @@ public class WordSimpleTests {
|
|
|
// 设置信息
|
|
|
SimpleWordTableData data1 = SimpleWordTableData.builder().id("1").pledger("张三").contractNum("合同阿巴阿巴阿巴阿巴").guarantyStyle("担保").amount("1000万").build();
|
|
|
SimpleWordTableData data2 = SimpleWordTableData.builder().id("2").pledger("李四").contractNum("合同呜呼呜呼呜呼芜湖").guarantyStyle("抵押").amount("200028万").build();
|
|
|
- List<SimpleWordTableData> contractList = Arrays.asList(data1,data2);
|
|
|
+ List<SimpleWordTableData> contractList = Arrays.asList(data1, data2);
|
|
|
|
|
|
AsposeWordUtil.createTable(doc, SimpleWordTableData.class, contractList, "table");
|
|
|
doc.save("E:\\test\\word\\createTable.doc");
|
|
@@ -217,6 +221,7 @@ public class WordSimpleTests {
|
|
|
/**
|
|
|
* 测试将html转成word的文件插入到word中
|
|
|
* 如果插入后发现table样式收到影响,请检查是否是插入位置设置了段落样式等影响到了table
|
|
|
+ *
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
@Test
|
|
@@ -228,7 +233,7 @@ public class WordSimpleTests {
|
|
|
Document mainDoc = new Document("E:\\test\\word\\6_success_output.doc");
|
|
|
|
|
|
// 需要插入的word文件
|
|
|
- Document subDoc = new Document("E:\\test\\word\\1730343284041.docx");
|
|
|
+ Document subDoc = new Document("E:\\test\\word\\1730712433605.docx");
|
|
|
|
|
|
mainDoc = AsposeWordUtil.insertDocumentAfterBookMark(mainDoc, subDoc, bookmark, false);
|
|
|
// 保存文件
|
|
@@ -240,6 +245,7 @@ public class WordSimpleTests {
|
|
|
* 设置Word页眉
|
|
|
* 不能先设置页脚,需要先设置页码后才能设置页脚内容
|
|
|
* 否则提前移动光标到页脚会报错:Cannot insert a node of this type at this location.
|
|
|
+ *
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
// @Test
|
|
@@ -250,11 +256,11 @@ public class WordSimpleTests {
|
|
|
builder.getPageSetup().setOddAndEvenPagesHeaderFooter(false);
|
|
|
|
|
|
/*
|
|
|
- * 设置样式前,先移动光标!
|
|
|
- * 先移动光标!
|
|
|
- * 先移动光标!
|
|
|
- * 先移动光标!
|
|
|
- */
|
|
|
+ * 设置样式前,先移动光标!
|
|
|
+ * 先移动光标!
|
|
|
+ * 先移动光标!
|
|
|
+ * 先移动光标!
|
|
|
+ */
|
|
|
|
|
|
// 移动光标到页眉
|
|
|
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
|
|
@@ -287,11 +293,15 @@ public class WordSimpleTests {
|
|
|
builder.writeln();
|
|
|
builder.write("债权价值分析报告·正文");
|
|
|
|
|
|
+ ParagraphCollection paras = mainDoc.getFirstSection().getBody().getParagraphs();
|
|
|
+ paras.get(0).remove();
|
|
|
+
|
|
|
mainDoc.save("E:\\test\\word\\output.docx");
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置页脚与页码(该方法实测不是很好用,建议先在模板设置好页眉页脚页码等相关设置)
|
|
|
+ *
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
// @Test
|
|
@@ -332,4 +342,22 @@ public class WordSimpleTests {
|
|
|
doc.save("E:\\test\\word\\output.docx");
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ void getRowNo() throws Exception {
|
|
|
+ Document doc = new Document("E:\\test\\word\\6_success_output.doc");
|
|
|
+ DocumentBuilder builder = new DocumentBuilder(doc);
|
|
|
+
|
|
|
+ // 移动光标到文档的某个特定位置,以文档末尾为例
|
|
|
+// builder.moveToDocumentEnd();
|
|
|
+ builder.moveToMergeField("deleteRow");
|
|
|
+
|
|
|
+ // 获取当前光标所在的段落
|
|
|
+ Paragraph currentParagraph = builder.getCurrentParagraph();
|
|
|
+
|
|
|
+ currentParagraph.remove();
|
|
|
+
|
|
|
+ doc.save("E:\\test\\word\\delete_row.doc");
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
}
|