

C#/VB.NET 如何在Word表格中拆分或合并单元格?
source link: https://blog.51cto.com/u_15656056/5971350
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C#/VB.NET 如何在Word表格中拆分或合并单元格?
精选 原创我们在使用Word制作表格时,由于表格较为复杂,只是简单的插入行、列并不能满足我们的需要。要做一个完整的表格,很多时候需要将单元格进行拆分或者合并,才能达到我们想要的效果。那么具体要如何操作呢?别担心,本文将详细为您介绍在Word表格中拆分或合并单元格的思路及方法,并附上C#/VB.NET供大家参考。
- 在Word表格中合并单元格
- 在Word表格中拆分单元格
本次测试时,在程序中引入Free Spire.Doc for .NET。可通过以下方法引用 Free Spire.Doc.dll文件:
方法1:将 Free Spire.Doc for .NET下载到本地,解压,安装。安装完成后,找到安装路径下BIN文件夹中的 Spire.Doc.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。
方法2:通过 NuGet安装。可通过以下2种方法安装:
(1)可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,点击“安装”。等待程序安装完成。
(2)将以下内容复制到PM控制台安装。
Install-Package FreeSpire.Doc -Version 10.8.0
在Word表格中合并单元格
合并单元格,指的是将两个或多个位于同一行或者同一列的单元格合并成一个单元格。具体步骤如下:
- 初始化 Document类的实例。
- 使用Document.LoadFromFile() 方法加载Word文档。
- 调用Document.Sections[int] 属性,通过索引获取文档中的特定节。
- 使用Section.AddTable() 方法将表添加到该节。
- 使用Table.ResetCells() 方法指定表的行数和列的数量。
- 使用Table.ApplyHorizontalMerge() 方法水平合并表中的特定单元格。
- 使用 Table.ApplyVerticalMerge() 方法垂直合并表中的特定单元格。
- 将数据添加到表中。
- 将样式应用于表。
- 使用Document.SaveToFile() 方法保存结果文档。
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);
}
}
}
VB.NET
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表格中拆分单元格
将一个单元格拆分成两个或多个单元格,这种方法就叫做拆分单元格。具体步骤如下:
- 初始化Document类的实例。
- 使用Document.LoadFromFile() 方法加载Word文档。
- 调用Document.Sections[int] 属性,通过索引获取文档中的特定节。
- 通过Section.Tables[int] 属性,通过索引在该节获取特定表格。
- 通过Table.Rows[int].Cells[int] 属性获取要拆分的表格单元格。
- 使用TableCell.SplitCell() 方法将单元格分为特定数量的列和行。
- 使用 Document.SaveToFile() 方法保存结果文档。
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);
}
}
}
VB.NET
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

—本文完—
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK