C#实现拆分合并Word表格中的单元格

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C#实现拆分合并Word表格中的单元格

Carina-baby   2022-12-23 我要评论

我们在使用Word制作表格时,由于表格较为复杂,只是简单的插入行、列并不能满足我们的需要。要做一个完整的表格,很多时候需要将单元格进行拆分或者合并,才能达到我们想要的效果。那么具体要如何操作呢?别担心,本文将详细为您介绍在Word表格中拆分或合并单元格的思路及方法。

  • 在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() 方法保存结果文档。

完整代码

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("测试文档.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
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() 方法保存结果文档。

完整代码

C#

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);
        }
    }
}

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("合并单元格.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

效果图

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们