Here, we will introduce the merging and splitting of table cells in a Word document in a Java application.
Merge cells:
Spire.Doc merged cells are divided into two categories: horizontal merge and vertical merge. For horizontal join, specify the applyVerticalMerge (int a, int a, int a) column, start row and end row you want to join. At the time of vertical join, specify the applyHorizontalMerge (int a, int a, int a) row, start column you want to join. , Specify the end column.
import com.spire.doc.*;
public class MergeTableCell {
    public static void main(String[] args) throws Exception {
        String output = "output/MergeTableCells.docx";
        //New word example
        Document document = new Document();
        // 4*Add 4 tables
        Section section = document.addSection();
        Table table = section.addTable(true);
        table.resetCells(4, 4);
        
        //Horizontal connection
        table.applyHorizontalMerge(0, 0, 3);
        //Vertical connection
        table.applyVerticalMerge(0, 2, 3);
        //Save document
        document.saveToFile(output, FileFormat.Docx);
    }
}

Cell split: To split a cell, first get the cell and specify the number of split columns and rows of the cell. import com.spire.doc.*;
public class SplitTableCell {
    public static void main(String[] args) throws Exception {
        String output = "output/SplitTableCells.docx";
        //New word example
        Document document = new Document();
        //4*Add 4 tables
        Section section = document.addSection();
        Table table = section.addTable(true);
        table.resetCells(4, 4);
        //Cell split
        table.getRows().get(3).getCells().get(3).splitCell(2, 2);
        //Save document
        document.saveToFile(output, FileFormat.Docx);
    }
}

Recommended Posts