當(dāng)前熱點(diǎn)-C#/VB.NET 如何在Word表格中拆分或合并單元格???

2022-12-27 10:22:46 來(lái)源:51CTO博客

我們?cè)谑褂肳ord制作表格時(shí),由于表格較為復(fù)雜,只是簡(jiǎn)單的插入行、列并不能滿足我們的需要。要做一個(gè)完整的表格,很多時(shí)候需要將單元格進(jìn)行拆分或者合并,才能達(dá)到我們想要的效果。那么具體要如何操作呢?別擔(dān)心,本文將詳細(xì)為您介紹在Word表格中拆分或合并單元格的思路及方法,并附上C#/VB.NET供大家參考。

在Word表格中合并單元格在Word表格中拆分單元格

程序環(huán)境

本次測(cè)試時(shí),在程序中引入Free Spire.Doc for .NET。可通過(guò)以下方法引用 Free Spire.Doc.dll文件:


(資料圖片僅供參考)

方法1:將 ??Free Spire.Doc for .NET??下載到本地,解壓,安裝。安裝完成后,找到安裝路徑下BIN文件夾中的 Spire.Doc.dll。然后在Visual Studio中打開(kāi)“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“添加引用”,將本地路徑BIN文件夾下的dll文件添加引用至程序。

方法2:通過(guò)??NuGet??安裝。可通過(guò)以下2種方法安裝:

(1)可以在Visual Studio中打開(kāi)“解決方案資源管理器”,鼠標(biāo)右鍵點(diǎn)擊“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,點(diǎn)擊“安裝”。等待程序安裝完成。

(2)將以下內(nèi)容復(fù)制到PM控制臺(tái)安裝。

Install-Package FreeSpire.Doc -Version 10.8.0

在Word表格中合并單元格

合并單元格,指的是將兩個(gè)或多個(gè)位于同一行或者同一列的單元格合并成一個(gè)單元格。具體步驟如下:

初始化 Document類(lèi)的實(shí)例。使用Document.LoadFromFile() 方法加載Word文檔。調(diào)用Document.Sections[int] 屬性,通過(guò)索引獲取文檔中的特定節(jié)。使用Section.AddTable() 方法將表添加到該節(jié)。使用Table.ResetCells() 方法指定表的行數(shù)和列的數(shù)量。使用Table.ApplyHorizontalMerge() 方法水平合并表中的特定單元格。使用 Table.ApplyVerticalMerge() 方法垂直合并表中的特定單元格。將數(shù)據(jù)添加到表中。將樣式應(yīng)用于表。使用Document.SaveToFile() 方法保存結(jié)果文檔。

完整代碼

C#

using Spire.Doc;using Spire.Doc.Documents;namespace MergeTableCells{    class Program    {        static void Main(string[] args)        {            //初始化 Document類(lèi)的實(shí)例            Document document = new Document();            //加載Word文檔            document.LoadFromFile("測(cè)試文檔.docx");            //獲取特定節(jié)            Section section = document.Sections[0];            //添加一個(gè) 4 x 4 表格到該節(jié)            Table table = section.AddTable();            table.ResetCells(4, 4);            //水平合并表中的特定單元格            table.ApplyHorizontalMerge(0, 0, 3);            //垂直合并表中的特定單元格            table.ApplyVerticalMerge(0, 2, 3);            //將數(shù)據(jù)添加到表格中            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 = "文本";                }            }            //將樣式應(yīng)用于表            table.ApplyStyle(DefaultTableStyle.LightGridAccent1);            //保存結(jié)果文檔            document.SaveToFile("合并單元格.docx", FileFormat.Docx2013);        }    }}

VB.NET

Imports Spire.DocImports Spire.Doc.DocumentsNamespace MergeTableCells    Friend Class Program        Private Shared Sub Main(ByVal args As String())            "初始化 Document類(lèi)的實(shí)例            Dim document As Document = New Document()            "加載Word文檔            document.LoadFromFile("測(cè)試文檔.docx")            "獲取特定節(jié)            Dim section As Section = document.Sections(0)            "添加一個(gè) 4 x 4 表格到該節(jié)            Dim table As Table = section.AddTable()            table.ResetCells(4, 4)            "水平合并表中的特定單元格            table.ApplyHorizontalMerge(0, 0, 3)            "垂直合并表中的特定單元格            table.ApplyVerticalMerge(0, 2, 3)            "將數(shù)據(jù)添加到表格中            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            "將樣式應(yīng)用于表            table.ApplyStyle(DefaultTableStyle.LightGridAccent1)            "保存結(jié)果文檔            document.SaveToFile("合并單元格.docx", FileFormat.Docx2013)        End Sub    End ClassEnd Namespace

效果圖

在Word表格中拆分單元格

將一個(gè)單元格拆分成兩個(gè)或多個(gè)單元格,這種方法就叫做拆分單元格。具體步驟如下:

初始化Document類(lèi)的實(shí)例。使用Document.LoadFromFile() 方法加載Word文檔。調(diào)用Document.Sections[int] 屬性,通過(guò)索引獲取文檔中的特定節(jié)。通過(guò)Section.Tables[int] 屬性,通過(guò)索引在該節(jié)獲取特定表格。通過(guò)Table.Rows[int].Cells[int] 屬性獲取要拆分的表格單元格。使用TableCell.SplitCell() 方法將單元格分為特定數(shù)量的列和行。使用 Document.SaveToFile() 方法保存結(jié)果文檔。

完整代碼

C#

using Spire.Doc;namespace SplitTableCells{    class Program    {        static void Main(string[] args)        {            //初始化Document類(lèi)的實(shí)例            Document document = new Document();            //加載Word文檔            document.LoadFromFile("合并單元格.docx");            //獲取文檔中的特定節(jié)            Section section = document.Sections[0];            //在該節(jié)獲取特定表格            Table table = section.Tables[0] as Table;            //獲取要拆分的表格單元格            TableCell cell1 = table.Rows[3].Cells[3];            //將單元格分為特定數(shù)量的列和行            cell1.SplitCell(2, 2);            //保存結(jié)果文檔            document.SaveToFile("拆分單元格.docx", FileFormat.Docx2013);        }    }}

VB.NET

Imports Spire.DocNamespace SplitTableCells    Friend Class Program        Private Shared Sub Main(ByVal args As String())            "初始化Document類(lèi)的實(shí)例            Dim document As Document = New Document()            "加載Word文檔            document.LoadFromFile("合并單元格.docx")            "獲取文檔中的特定節(jié)            Dim section As Section = document.Sections(0)            "在該節(jié)獲取特定表格            Dim table As Table = TryCast(section.Tables(0), Table)            "獲取要拆分的表格單元格            Dim cell1 As TableCell = table.Rows(3).Cells(3)            "將單元格分為特定數(shù)量的列和行            cell1.SplitCell(2, 2)            "保存結(jié)果文檔            document.SaveToFile("拆分單元格.docx", FileFormat.Docx2013)        End Sub    End ClassEnd Namespace

效果圖

—本文完—

標(biāo)簽: 合并單元格 拆分單元格 解決方案

上一篇:QT的QGroupBox
下一篇:環(huán)球觀天下!QT實(shí)現(xiàn)表單