日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#實現拆分合并Word表格中的單元格_C#教程

作者:Carina-baby ? 更新時間: 2023-01-27 編程語言

我們在使用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

效果圖

原文鏈接:https://www.cnblogs.com/Carina-baby/p/16998042.html

欄目分類
最近更新