Java: Remove Duplicate Values When Merging Cells in Word

     Tables can express content systematically, concisely, and centrally, and have strong logic and contrast. A simple and clear table can make complex data files clear at a glance, so the repeated values in the table are unnecessary, we can delete the repeated values and merge the cells to achieve a clear and concise effect. So today I will show you how to delete duplicate values when merging cells in a Word document through code in a Java program. The following are the steps I have organized, and the Java code is attached for your reference.

Programming Environment

First, you’re required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project’s pom.xml file.

<repositories>

        <repository>

          <id>com.e-iceblue</id>

         <url>https://repo.e-iceblue.com/repository/maven-public/</url>

        </repository>

    </repositories>

<dependencies>

    <dependency>

        <groupId>e-iceblue</groupId>

        <artifactId>spire.doc.free</artifactId>

        <version>5.2.0</version>

    </dependency>

</dependencies>

Specific Steps:

The following are the steps to remove the duplicate values in the merged cells in a Word table.

  • Create an object of Document class and load the sample document using Document.loadFromFile() method.
  • Use Document.getSections() method to get the section collection, and then get the specific section using SectionCollection.get() method.
  • Use Section.getTables() method to get the table collection, and then get the desired table using TableCollection.get() method
  • Invoke mergeCell(Table table, boolean isHorizontalMerge, int index, int start, int end) method to merge table cells vertically or horizontally. This method will determine whether the cells to be merged have the same value, and will preserve only one value in the merged cell.
  • Save the document to file using Document.saveToFile() method.

FULL CODE:

import com.spire.doc.*;


public class RemoveDuplicateValues {
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("table2.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, 3, 5);

//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();
}
}

Effective Shot:



conclusion

In this post, you have learned how to remove Duplicate Values When Merging Cells in Word in Java. Not only that, we also have other functions, such as,Java: Create a Table in Word,Java merge and split table cells on Word document and so on. Apart from that, if you'd like to learn more, you can visit this link to explore more about for Spire.Doc for Java.



Comments

Popular posts from this blog

How to Change Font Color in Word via Java

How to Convert OpenDocument Presentation (.odp) to PDF via Java Application

Java: How to encrypt or decrypt PDF documents?