Java: Remove Duplicate Values When Merging Cells in Word

Spire.Doc offers Table.applyVerticalMerge() method to merge table cells vertically and Table.applyHorizontalMerge() method to merge table cells horizontally. By default, the merged cells will have repeated values if the cells to be merged contain the same value. This article will demonstrate how to remove repeated values in the merged cells using a customized method with Spire.Doc for Java.

Install Spire.Doc for Java

First of all, 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>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.11.0</version>
    </dependency>
</dependencies>
    

Remove Duplicate Values When Merging Cells

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.
  • Java
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, 4, 5);

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

Java: Remove Duplicate Values When Merging Cells in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.