前言
Spire.Doc提供了Table.applyVerticalMerge()办法来垂直合并word文档外面的表格单元格,Table.applyHorizontalMerge()办法来程度合并表格单元格。默认状况下,如果要合并的单元格蕴含雷同的值,那么合并后的单元格会有反复的值。本文将演示如何应用Spire.Doc for Java来删除合并单元格时的反复值。
程序环境配置
装置Spire.Doc for Java
首先,您须要在您的Java程序中增加Spire.Doc.jar文件作为一个依赖项。该JAR文件能够从这个链接下载。如果您应用Maven,您能够通过在我的项目的pom.xml文件中增加以下代码,在您的应用程序中轻松导入该JAR文件。
代码示例
<repositories> <repository> <id>com.e-iceblue</id> <url>https://repo.e-iceblue.cn/repository/maven-public/</url> </repository></repositories><dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc</artifactId> <version>10.9.8</version> </dependency></dependencies>
留神:下面代码中的版本号要与下载连贯中的版本号保持一致,以体验最新性能或者防止BUG。
删除合并单元格中的反复值
步骤
- 创立一个Document实例,应用Document.loadFromFile() 办法加载示例文档。
- 应用Document.getSections()办法取得节汇合,而后应用SectionCollection.get()办法取得特定的节。
- 应用Section.getTables()办法取得表格汇合,而后应用TableCollection.get()办法取得想要的表。
- mergeCell(Table table, boolean isHorizontalMerge, int index, int start, int end)办法是自定义办法,意为合并单元格,它能够帮忙咱们程度的或是垂直的合并单元格,合并的范畴是从start到end,传入绝对应的值即可实现。
- 调用mergeCell(Table table, boolean isHorizontalMerge, int index, int start, int end)办法来垂直或程度地合并表格单元格。这个办法将确定要合并的单元格是否有雷同的值,并在合并的单元格中只保留一个值。
- 应用Document.saveToFile() 办法保留文件。
代码实现
import com.spire.doc.*;import com.spire.doc.interfaces.ITable; public class MergeCells { public static void main(String[] args) throws Exception { //Create an object of Document class and load the sample document. Document document = new Document(); document.loadFromFile("Sample.docx"); //Get the first section Section section = document.getSections().get(0); //Get the first table Table table = section.getTables().get(0); //Invoike mergeCell()method to merge cells vertically mergeCell(table, false, 0, 1, 3); //Invoike mergeCell()method to merge cell horizontally mergeCell(table, true, 0, 3, 4); //Save the document to file document.saveToFile("MergeTable.docx",FileFormat.Docx_2013);} //Customize a mergeCell() method to remove the duplicate values while merging cells public static void mergeCell(Table table, boolean isHorizontalMerge, int index, int start, int end) { if (isHorizontalMerge) { //Get a cell from table TableCell firstCell = table.get(index, start); //Invoke getCellText() method to get the cell’s text String firstCellText = getCellText(firstCell); for (int i = start + 1; i <= end; i++) { TableCell cell1 = table.get(index, i); //Check if the text is the same as the first cell if (firstCellText.equals(getCellText(cell1))) { //If yes, clear all the paragraphs in the cell cell1.getParagraphs().clear(); } } //Merge cells horizontally table.applyHorizontalMerge(index, start, end); } else { TableCell firstCell = table.get(start, index); String firstCellText = getCellText(firstCell); for (int i = start + 1; i <= end; i++) { TableCell cell1 = table.get(i, index); if (firstCellText.equals(getCellText(cell1))) { cell1.getParagraphs().clear(); } } //Merge cells vertically table.applyVerticalMerge(index, start, end); } } public static String getCellText(TableCell cell) { StringBuilder text = new StringBuilder(); //Traverse all the paragraphs of a cell for (int i = 0; i < cell.getParagraphs().getCount(); i++) { //Get every paragraph’s text and append it to StringBuilder text.append(cell.getParagraphs().get(i).getText().trim()); } return text.toString(); }}
效果图
---THE END---