How to merge four cells in two rows and two columns in word table

How to merge four cells in two rows and two columns in word table。The first cell and second cell in the first row, the first cell and second cell in the second row, I want merge these four cells, How can I do?

Hi Aostarit,

Thanks for your inquiry. Please use the following code snippet to achieve your requirements.

Document doc = new Document(MyDir + "in.docx");

Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
// We want to merge the range of cells found inbetween these two cells.
Cell cellStartRange = table.Rows[0].Cells[0];
Cell cellEndRange = table.Rows[0].Cells[1];
HorizontallyMergeCells(cellStartRange, cellEndRange);
doc.Save(MyDir + "out.docx");
private static void HorizontallyMergeCells(Cell c1, Cell c2)
{
    c1.CellFormat.HorizontalMerge = CellMerge.First;
    // Move all content from next cell to previous
    foreach(Node child in c2.ChildNodes)
        c1.AppendChild(child);
    c2.CellFormat.HorizontalMerge = CellMerge.Previous;
}

private static void VerticallyMergeCells(Cell c1, Cell c2)
{
    c1.CellFormat.VerticalMerge = CellMerge.First;
    // Move all content from bottom cell to top
    foreach(Node child in c2.ChildNodes)
        c1.AppendChild(child);
    c2.CellFormat.VerticalMerge = CellMerge.Previous;
}

You can also achieve the same thing by using vertical and horizontal merge of a table. Please check “MergeCells” method from the following article:
https://docs.aspose.com/words/net/working-with-columns-and-rows/