Java删除word合并单元格时的重复值

Spire.Doc提供了Table.applyVerticalMerge()方法来垂直合并word文档里面的表格单元格,Table.applyHorizontalMerge()方法来水平合并表格单元格。默认情况下,如果要合并的单元格包含相同的值,那么合并后的单元格会有重复的值。

程序环境

本次测试时,在程序中引入Spire.Doc for Java。可通过以下方法引用Spire.Doc.jar文件:

 方法1:将Spire.Doc for Java下载到本地,解压,安装。安装完成后,找到安装路径下BIN文件夹中的Spire.Doc.jar。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的jar文件添加引用至程序。

 方法2:使用Maven进行安装,你可以通过在项目的pom.xml文件中添加以下代码,在你的应用程序中轻松导入该JAR文件。

复制代码
 1 <repositories>
 2     <repository>
 3         <id>com.e-iceblue</id>
 4         <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
 5     </repository>
 6 </repositories>
 7 <dependencies>
 8     <dependency>
 9         <groupId>e-iceblue</groupId>
10         <artifactId>spire.doc</artifactId>
11         <version>10.9.0</version>
12     </dependency>
13 </dependencies>
复制代码

 方法3:通过NuGet安装。可通过以下2种方法安装:

 (1)可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“FreeSpire.Doc”,点击“安装”。等待程序安装完成。

 (2)将以下内容复制到PM控制台安装。

 Install-Package FreeSpire.Doc -Version 10.9.0

删除合并单元格时的重复值

  1. 创建一个Document实例,使用Document.loadFromFile()方法加载示例文档。
  2. 使用Document.getSections()方法获得节集合,然后使用SectionCollection.get()方法获得特定的节。
  3. 使用Section.getTables()方法获得表格集合,然后使用TableCollection.get()方法获得想要的表。
  4. 调用mergeCell(Table table,boolean isHorizontalMerge,int index,int start,int end)方法来垂直或水平地合并表格单元格。这个方法将确定要合并的单元格是否有相同的值,并在合并的单元格中只保留一个值。
  5. 使用Document.saveToFile()方法保存文件。

代码示例

复制代码
 1 import com.spire.doc.*;
 2 import com.spire.doc.interfaces.ITable;
 3 
 4     public class MergeCells {
 5     public static void main(String[] args) throws Exception {
 6 
 7         //Create an object of Document class and load the sample document.
 8         Document document = new Document();
 9         document.loadFromFile("Sample.docx");
10 
11         //Get the first section
12         Section section = document.getSections().get(0);
13 
14         //Get the first table
15         Table table = section.getTables().get(0);
16 
17         //Invoike mergeCell()method to merge cells vertically
18         mergeCell(table, false, 0, 1, 3);
19 
20         //Invoike mergeCell()method to merge cell horizontally
21         mergeCell(table, true, 0, 3, 4);
22 
23         //Save the document to file
24         document.saveToFile("MergeTable.docx",FileFormat.Docx_2013);
25 }
26 
27         //Customize a mergeCell() method to remove the duplicate values while merging cells
28         public static void mergeCell(Table table, boolean isHorizontalMerge, int index, int start, int end) {
29         
30         if (isHorizontalMerge) {
31             //Get a cell from table
32             TableCell firstCell = table.get(index, start);
33             //Invoke getCellText() method to get the cell’s text
34             String firstCellText = getCellText(firstCell);
35             for (int i = start + 1; i <= end; i++) {
36                 TableCell cell1 = table.get(index, i);
37                 //Check if the text is the same as the first cell                
38         if (firstCellText.equals(getCellText(cell1))) {
39                     //If yes, clear all the paragraphs in the cell
40                     cell1.getParagraphs().clear();
41                 }
42             }
43             //Merge cells horizontally
44             table.applyHorizontalMerge(index, start, end);
45 
46         } 
47             else {
48             TableCell firstCell = table.get(start, index);
49             String firstCellText = getCellText(firstCell);
50             for (int i = start + 1; i <= end; i++) {
51                 TableCell cell1 = table.get(i, index);
52                 if (firstCellText.equals(getCellText(cell1))) {
53                     cell1.getParagraphs().clear();
54                 }
55             }
56             //Merge cells vertically
57             table.applyVerticalMerge(index, start, end);
58         }
59     }
60         public static String getCellText(TableCell cell) {
61 
62         StringBuilder text = new StringBuilder();
63         //Traverse all the paragraphs of a cell
64         for (int i = 0; i < cell.getParagraphs().getCount(); i++) {
65             //Get every paragraph’s text and append it to StringBuilder
66             text.append(cell.getParagraphs().get(i).getText().trim());
67         }
68         return text.toString();
69     }
70 }
复制代码

效果图

 
注:测试代码中的文件路径为程序Debug路径,文件路径可自定义为其他路径。
 
—THE END—
posted @   UnSoleil  阅读(96)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示