合并单元格意味着将两个或多个单元格合并成一个较大的单元格,而拆分单元格意味着把一个单元格拆分成两个或多个较小的单元格。在 Microsoft Word 中创建或编辑表格时,您可能经常需要合并或拆分单元格,以便更好地显示数据。在本文中,您将学习如何使用 Spire.Doc for .NET 在 C# 和 VB.NET 中合并与拆分 Word 表格中的单元格。
安装 Spire.Doc for .NET
首先,您需要添加 Spire.Doc for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过 NuGet 安装。
PM> Install-Package Spire.Doc
合并 Word 表格中的单元格
在 Microsoft Word 中,您可以水平或垂直地将两个或更多相邻的单元格合并为较大的单元格。在 Spire.Doc 中,您可以使用 Table.ApplyHorizontalMerge() 或 Table.ApplyVerticalMerge() 方法实现相同的操作。具体步骤如下:
- 初始化 Document 类的实例。
- 使用 Document.LoadFromFile() 方法加载 Word 文档。
- 调用 Document.Sections[int] 属性,通过索引获取文档中的特定节。
- 使用 Section.AddTable() 方法将表添加到该节。
- 使用 Table.ResetCells() 方法指定表的行数和列的数量。
- 使用 Table.ApplyHorizontalMerge() 方法水平合并表中的特定单元格。
- 使用 Table.ApplyVerticalMerge() 方法垂直合并表中的特定单元格。
- 将数据添加到表中。
- 将样式应用于表。
- 使用 Document.SaveToFile() 方法保存结果文档。
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
namespace MergeTableCells
{
class Program
{
static void Main(string[] args)
{
//初始化 Document类的实例
Document document = new Document();
//加载Word文档
document.LoadFromFile("测试文档.docx");
//获取特定节
Section section = document.Sections[0];
//添加一个 4 x 4 表格到该节
Table table = section.AddTable();
table.ResetCells(4, 4);
//水平合并表中的特定单元格
table.ApplyHorizontalMerge(0, 0, 3);
//垂直合并表中的特定单元格
table.ApplyVerticalMerge(0, 2, 3);
//将数据添加到表格中
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 = "文本";
}
}
//将样式应用于表
table.ApplyStyle(DefaultTableStyle.LightGridAccent1);
//保存结果文档
document.SaveToFile("合并单元格.docx", FileFormat.Docx2013);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace MergeTableCells
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'初始化 Document类的实例 Dim document As Document = New Document() '加载Word文档
document.LoadFromFile("测试文档.docx")
'获取特定节 Dim section As Section = document.Sections(0) '添加一个 4 x 4 表格到该节
Dim table As Table = section.AddTable()
table.ResetCells(4, 4)
'水平合并表中的特定单元格 table.ApplyHorizontalMerge(0,0,3) '垂直合并表中的特定单元格
table.ApplyVerticalMerge(0, 2, 3)
'将数据添加到表格中 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 = "文本" Next Next '将样式应用于表
table.ApplyStyle(DefaultTableStyle.LightGridAccent1)
'保存结果文档 document.SaveToFile("合并单元格.docx",FileFormat.Docx2013) End Sub End Class End Namespace
拆分 Word 表格中的单元格
Spire.Doc for .NET 还提供 TableCell.SplitCell()方法,以便您可以将 Word 表格中的单元格拆分为两个或多个单元格。详细步骤如下:
- 初始化 Document类的实例。
- 使用 Document.LoadFromFile()方法加载 Word 文档。
- 调用 Document.Sections[int]属性,通过索引获取文档中的特定节。
- 通过 Section.Tables[int] 属性,通过索引在该节获取特定表格。
- 通过 Table.Rows[int].Cells[int]属性获取要拆分的表格单元格。
- 使用 TableCell.SplitCell()方法将单元格分为特定数量的列和行。
- 使用 Document.SaveToFile()方法保存结果文档。
- C#
- VB.NET
using Spire.Doc;namespace SplitTableCells{class Program{static void Main(string[] args){//初始化Document类的实例 Document document = new Document();//加载Word文档 document.LoadFromFile("合并单元格.docx");//获取文档中的特定节 Section section = document.Sections[0];//在该节获取特定表格 Table table = section.Tables[0] as Table;//获取要拆分的表格单元格 TableCell cell1 = table.Rows[3].Cells[3];//将单元格分为特定数量的列和行 cell1.SplitCell(2,2);//保存结果文档 document.SaveToFile("拆分单元格.docx",FileFormat.Docx2013);}}}
Imports Spire.Doc Namespace SplitTableCells Friend Class Program Private Shared Sub Main(ByVal args As String()) '初始化Document类的实例
Dim document As Document = New Document()
'加载Word文档 document.LoadFromFile("合并单元格.docx") '获取文档中的特定节
Dim section As Section = document.Sections(0)
'在该节获取特定表格 Dim table As Table = TryCast(section.Tables(0),Table) '获取要拆分的表格单元格
Dim cell1 As TableCell = table.Rows(3).Cells(3)
'将单元格分为特定数量的列和行 cell1.SplitCell(2,2) '保存结果文档
document.SaveToFile("拆分单元格.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace
申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。