如何合并与拆分 Word 表格中的单元格

在编辑Word文档时插入表格是非常实用的功能。有时,合并或者拆分某些单元格能帮助我们更好的显示或者编辑表格数据。在这里我将分享如何通过编程的方法完成此项操作。具体操作步骤后面附有示例代码,供参考使用。

程序

在这篇教程中,我所使用的类库是Free Spire.Doc for .NET,可以通过以下两种方法安装:

方法一

    通过NuGet安装Free Spire.Doc for .NET,具体步骤为:依次选择工具>NuGet包管理器>程序包管理器控制台,然后执行以下命令:

    PM> Install-Package FreeSpire.Doc      

方法二

    在程序中手动引入Spire.doc.dll文件;将Free Spire.Doc for .NET 下载到本地,解压并安装。安装完成后,打开Visual Studio创建新项目,在右边的“解决方案资源管理器”中右键点击“引用”,再依次选择“添加引用”> “浏览”,找到安装路径下BIN文件夹中的dll文件,点击“确定”,将其添加引用至程序中。

 

合并 Word 表格中的元格

  • 初始化 Document 类的实例。
  • 使用 Document.LoadFromFile() 方法加载 Word 文档。
  • 调用 Document.Sections[int] 属性,通过索引获取文档中的特定节。
  • 使用 Section.AddTable() 方法将表添加到该节。
  • 使用 Table.ResetCells() 方法指定表的行数和列的数量。
  • 使用 Table.ApplyHorizontalMerge() 方法水平合并表中的特定单元格。
  • 使用 Table.ApplyVerticalMerge() 方法垂直合并表中的特定单元格。
  • 将数据添加到表中。
  • 将样式应用于表。
  • 使用 Document.SaveToFile() 方法保存结果文档。

C#:

using Spire.Doc;
using Spire.Doc.Documents;

namespace MergeTableCells
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化 Document类的实例
            Document document = new Document();
            //加载Word文档
            document.LoadFromFile("sample.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("result.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())
            '初始化 Document类的实例
            Dim document As Document = New Document()
            '加载Word文档
            document.LoadFromFile("sample.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("result.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() 方法保存结果文档。

 C#:

using Spire.Doc;

namespace SplitTableCells
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化Document类的实例
            Document document = new Document();
            //加载Word文档
            document.LoadFromFile("rusult.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("result2.docx", FileFormat.Docx2013);
        }
    }
}

VB.NET:

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("result.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("result2.docx", FileFormat.Docx2013)
        End Sub
    End Class
End Namespace

posted @ 2023-02-03 10:24  Gia-  阅读(570)  评论(0编辑  收藏  举报