Java merge and split table cells on Word document

This article will demonstrate how to merge and split table cells on word document in Java applications.

Merge Table cells:

import com.spire.doc.*;

public class MergeTableCell {
    public static void main(String[] args) throws Exception {

        String output = "output/MergeTableCells.docx";

        //Create a Document instance
        Document document = new Document();

        Section section = document.addSection();
        Table table = section.addTable(true);
        table.resetCells(4, 4);
        
        //how to merge cells horizontally
        table.applyHorizontalMerge(0, 0, 3);

        //how to merge cells vertically
        table.applyVerticalMerge(0, 2, 3);

        //save the document to file
        document.saveToFile(output, FileFormat.Docx);
    }
}

Java merge and split table cells on Word document

Split table cells:

import com.spire.doc.*;

public class SplitTableCell {
    public static void main(String[] args) throws Exception {

        String output = "output/SplitTableCells.docx";

        //Create a Document instance
        Document document = new Document();

        Section section = document.addSection();
        Table table = section.addTable(true);
        table.resetCells(4, 4);

        //split the cell
        table.getRows().get(3).getCells().get(3).splitCell(2, 2);

        //save the document to file
        document.saveToFile(output, FileFormat.Docx);
    }
}

Java merge and split table cells on Word document