C#實現(xiàn)拆分合并Word表格中的單元格
我們在使用Word制作表格時,由于表格較為復(fù)雜,只是簡單的插入行、列并不能滿足我們的需要。要做一個完整的表格,很多時候需要將單元格進(jìn)行拆分或者合并,才能達(dá)到我們想要的效果。那么具體要如何操作呢?別擔(dān)心,本文將詳細(xì)為您介紹在Word表格中拆分或合并單元格的思路及方法。
- 在Word表格中合并單元格
- 在Word表格中拆分單元格
程序環(huán)境
本次測試時,在程序中引入Free Spire.Doc for .NET。可通過以下方法引用 Free Spire.Doc.dll文件:
方法1:將 Free Spire.Doc for .NET下載到本地,解壓,安裝。安裝完成后,找到安裝路徑下BIN文件夾中的 Spire.Doc.dll。然后在Visual Studio中打開“解決方案資源管理器”,鼠標(biāo)右鍵點擊“引用”,“添加引用”,將本地路徑BIN文件夾下的dll文件添加引用至程序。
方法2:通過NuGet安裝??赏ㄟ^以下2種方法安裝:
(1)可以在Visual Studio中打開“解決方案資源管理器”,鼠標(biāo)右鍵點擊“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,點擊“安裝”。等待程序安裝完成。
(2)將以下內(nèi)容復(fù)制到PM控制臺安裝。
Install-Package FreeSpire.Doc -Version 10.8.0
在Word表格中合并單元格
合并單元格,指的是將兩個或多個位于同一行或者同一列的單元格合并成一個單元格。具體步驟如下:
- 初始化 Document類的實例。
- 使用Document.LoadFromFile() 方法加載Word文檔。
- 調(diào)用Document.Sections[int] 屬性,通過索引獲取文檔中的特定節(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類的實例 Document document = new Document(); //加載Word文檔 document.LoadFromFile("測試文檔.docx"); //獲取特定節(jié) Section section = document.Sections[0]; //添加一個 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.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") '獲取特定節(jié) Dim section As Section = document.Sections(0) '添加一個 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 Class End Namespace
效果圖
在Word表格中拆分單元格
將一個單元格拆分成兩個或多個單元格,這種方法就叫做拆分單元格。具體步驟如下:
初始化Document類的實例。
使用Document.LoadFromFile() 方法加載Word文檔。
調(diào)用Document.Sections[int] 屬性,通過索引獲取文檔中的特定節(jié)。
通過Section.Tables[int] 屬性,通過索引在該節(jié)獲取特定表格。
通過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類的實例 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.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") '獲取文檔中的特定節(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 Class End Namespace
效果圖
到此這篇關(guān)于C#實現(xiàn)拆分合并Word表格中的單元格的文章就介紹到這了,更多相關(guān)C#拆分合并Word表格單元格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!