C#/VB.NET:How to Merge or Split Table Cells in Word
When we use Word to make a table, because the table is more complicated, simply inserting rows and columns cannot meet our needs. To make a complete table, cells often need to be split or merged to achieve the desired effect. So how to do it? Don't worry, this article will introduce you in detail the ideas and methods of splitting or merging cells in a Word table.
- Merge Table Cells in Word
- Split Table Cells in Word
Programming
Environment
In this test, Free Spire.Doc for .NET is introduced into the program. The Spire.Doc.dll file can be referenced by:
Method 1: Download Free Spire.Doc for .NET locally, unzip it, and install it. After the installation is complete, find Spire.Doc.dll in the BIN folder under the installation path. Then open the "Solution Explorer" in Visual Studio, right-click "References", "Add Reference", and add a reference to the dll file in the BIN folder of the local path to the program.
Method 2: Install via NuGet. It can be installed by the following 2 methods:
(1) You can open the "Solution Explorer" in Visual Studio, right-click "References", "Manage NuGet Packages", then search for "Free Spire.Doc", and click "Install". Wait for the program installation to complete.
(2) Copy the following content to the PM console installation.
Install-Package FreeSpire.Doc
-Version 10.8.0
Merge Table Cells in Word
Merging cells refers to merging two or more cells in the same row or column into one cell. Specific steps are as follows:
- Initialize an instance of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Get a specific section in the document by its index through Document.Sections[int] property.
- Add a table to the section using Section.AddTable() method.
- Specify the number of rows and columns of the table using Table.ResetCells() method.
- Horizontally merge specific cells in the table using Table.ApplyHorizontalMerge() method.
- Vertically merge specific cells in the table using Table.ApplyVerticalMerge() method.
- Add some data to the table.
- Apply a style to the table.
- Save the result document using Document.SaveToFile() method.
using Spire.Doc;
using Spire.Doc.Documents;
namespace MergeTableCells
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new
Document();
//Load a Word document
document.LoadFromFile("Input.docx");
//Get the first section
Section section = document.Sections[0];
//Add a 4 x 4 table to the section
Table table = section.AddTable();
table.ResetCells(4, 4);
//Horizontally merge cells 1, 2, 3, and 4
in the first row
table.ApplyHorizontalMerge(0, 0, 3);
//Vertically merge cells 3 and 4 in the
first column
table.ApplyVerticalMerge(0, 2, 3);
//Add some data to the table
for (int row = 0; row < table.Rows.Count; row++)
{
for (int col = 0; col < table.Rows[row].Cells.Count;
col++)
{
TableCell cell = table[row, col];
cell.CellFormat.VerticalAlignment =
VerticalAlignment.Middle;
Paragraph paragraph = cell.AddParagraph();
paragraph.Format.HorizontalAlignment =
HorizontalAlignment.Center;
paragraph.Text = "Text";
}
}
//Apply a style to the table
table.ApplyStyle(DefaultTableStyle.LightGridAccent1);
//Save the result document
document.SaveToFile("MergeCells.docx", FileFormat.Docx2013);
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace MergeTableCells
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim document As Document = New
Document()
'Load a Word document
document.LoadFromFile("Input.docx")
'Get the first section
Dim section As Section = document.Sections(0)
'Add a 4 x 4 table to the section
Dim table As Table = section.AddTable()
table.ResetCells(4, 4)
'Horizontally merge cells 1, 2, 3, and 4
in the first row
table.ApplyHorizontalMerge(0, 0, 3)
'Vertically merge cells 3 and 4 in the
first column
table.ApplyVerticalMerge(0, 2, 3)
'Add some data to the table
For row As Integer = 0 To
table.Rows.Count - 1
For col As Integer = 0 To
table.Rows(row).Cells.Count - 1
Dim cell As TableCell = table(row, col)
cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
Dim
paragraph As Paragraph = cell.AddParagraph()
paragraph.Format.HorizontalAlignment =
HorizontalAlignment.Center
paragraph.Text = "Text"
Next
Next
'Apply a style to the table
table.ApplyStyle(DefaultTableStyle.LightGridAccent1)
'Save the result document
document.SaveToFile("MergeCells.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace
Effective Shot
Split Table Cells in Word
Splitting a cell into two or more cells is called splitting a cell. Specific steps are as follows:
- Initialize an instance of the Document class.
- Load a Word document using Document.LoadFromFile() method.
- Get a specific section in the document by its index through Document.Sections[int] property.
- Get a specific table in the section by its index through Section.Tables[int] property.
- Get the table cell that you want to split through Table.Rows[int].Cells[int] property.
- Split the cell into specific number of columns and rows using TableCell.SplitCell() method.
- Save the result document using Document.SaveToFile() method.
using Spire.Doc;
namespace SplitTableCells
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new
Document();
//Load a Word Document
document.LoadFromFile("MergeCells.docx");
//Get the first section
Section section = document.Sections[0];
//Get the first table in the section
Table table = section.Tables[0] as Table;
//Get the 4th cell in the 4th row
TableCell cell1 = table.Rows[3].Cells[3];
//Split the cell into 2 columns and 2 rows
cell1.SplitCell(2, 2);
//save the result document
document.SaveToFile("SplitCells.docx", FileFormat.Docx2013);
}
}
}
VB.NET
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace MergeTableCells
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a Document instance
Dim document As Document = New
Document()
'Load a Word document
document.LoadFromFile("Input.docx")
'Get the first section
Dim section As Section = document.Sections(0)
'Add a 4 x 4 table to the section
Dim table As Table = section.AddTable()
table.ResetCells(4, 4)
'Horizontally merge cells 1, 2, 3, and 4
in the first row
table.ApplyHorizontalMerge(0, 0, 3)
'Vertically merge cells 3 and 4 in the
first column
table.ApplyVerticalMerge(0, 2, 3)
'Add some data to the table
For row As Integer = 0 To
table.Rows.Count - 1
For col As Integer = 0 To
table.Rows(row).Cells.Count - 1
Dim cell As TableCell = table(row, col)
cell.CellFormat.VerticalAlignment =
VerticalAlignment.Middle
Dim
paragraph As Paragraph = cell.AddParagraph()
paragraph.Format.HorizontalAlignment =
HorizontalAlignment.Center
paragraph.Text = "Text"
Next
Next
'Apply a style to the table
table.ApplyStyle(DefaultTableStyle.LightGridAccent1)
'Save the result document
document.SaveToFile("MergeCells.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace
Effective Shot
Conclusion:
In this post, you have learned how to merge or split table cells in Word in C#/VB.NET. Not only that, we also have other functions, such as, C#/VB.NET: Create a Table in Word, Add/Get Alternative Text of Table in Word in C# 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 .NET.
Comments
Post a Comment