翻譯|使用教程|編輯:胡濤|2023-01-09 10:42:37.580|閱讀 229 次
概述:在本文中,您將學習如何使用Spire.Doc for .NET在 C# 和 VB.NET 中合并或拆分 Word 文檔中的表格單元格。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
合并單元格是指將兩個或多個單元格組合成一個較大的單元格,而拆分單元格是指將一個單元格分成兩個或多個較小的單元格。在 Microsoft Word 中創建或編輯表格時,您可能經常需要合并或拆分表格單元格以更好地呈現數據。在本文中,您將學習如何使用Spire.Doc for .NET在 C# 和 VB.NET 中合并或拆分 Word 文檔中的表格單元格。
首先,您需要添加包含在 Spire.Doc for.NET 包中的 DLL 文件作為您的 .NET 項目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.Doc
在 Microsoft Word 中,您可以將兩個或多個相鄰的單元格水平或垂直合并為一個更大的單元格。在 Spire.Doc 中,您可以使用Table.ApplyHorizontalMerge()或Table.ApplyVerticalMerge()方法實現相同的目的。以下是詳細步驟:
【C#】
using Spire.Doc; using Spire.Doc.Documents; namespace MergeTableCells { class Program { static void Main(string[] args) { //Create a Document instance Document document = new Document(); //Load a Word document document.LoadFromFile("Input.docx"); //Get the first section Section section = document.Sections[0]; //Add a 4 x 4 table to the section Table table = section.AddTable(); table.ResetCells(4, 4); //Horizontally merge cells 1, 2, 3, and 4 in the first row table.ApplyHorizontalMerge(0, 0, 3); //Vertically merge cells 3 and 4 in the first column table.ApplyVerticalMerge(0, 2, 3); //Add some data to the table 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 = "Text"; } } //Apply a style to the table table.ApplyStyle(DefaultTableStyle.LightGridAccent1); //Save the result document document.SaveToFile("MergeCells.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()) 'Create a Document instance Dim document As Document = New Document() 'Load a Word document document.LoadFromFile("Input.docx") 'Get the first section Dim section As Section = document.Sections(0) 'Add a 4 x 4 table to the section Dim table As Table = section.AddTable() table.ResetCells(4, 4) 'Horizontally merge cells 1, 2, 3, and 4 in the first row table.ApplyHorizontalMerge(0, 0, 3) 'Vertically merge cells 3 and 4 in the first column table.ApplyVerticalMerge(0, 2, 3) 'Add some data to the table 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 = "Text" Next Next 'Apply a style to the table table.ApplyStyle(DefaultTableStyle.LightGridAccent1) 'Save the result document document.SaveToFile("MergeCells.docx", FileFormat.Docx2013) End Sub End Class End Namespace
Spire.Doc for .NET 提供了TableCell.SplitCell()方法,使您能夠將 Word 表格中的一個單元格拆分為兩個或多個單元格。以下是詳細步驟:
【C#】
using Spire.Doc; namespace SplitTableCells { class Program { static void Main(string[] args) { //Create a Document instance Document document = new Document(); //Load a Word Document document.LoadFromFile("MergeCells.docx"); //Get the first section Section section = document.Sections[0]; //Get the first table in the section Table table = section.Tables[0] as Table; //Get the 4th cell in the 4th row TableCell cell1 = table.Rows[3].Cells[3]; //Split the cell into 2 columns and 2 rows cell1.SplitCell(2, 2); //save the result document document.SaveToFile("SplitCells.docx", FileFormat.Docx2013); } } }
【VB.NET】
Imports Spire.Doc Namespace SplitTableCells Friend Class Program Private Shared Sub Main(ByVal args As String()) 'Create a Document instance Dim document As Document = New Document() 'Load a Word Document document.LoadFromFile("MergeCells.docx") 'Get the first section Dim section As Section = document.Sections(0) 'Get the first table in the section Dim table As Table = TryCast(section.Tables(0), Table) 'Get the 4th cell in the 4th row Dim cell1 As TableCell = table.Rows(3).Cells(3) 'Split the cell into 2 columns and 2 rows cell1.SplitCell(2, 2) 'save the result document document.SaveToFile("SplitCells.docx", FileFormat.Docx2013) End Sub End Class End Namespace
以上便是如何在 Word 中合并或拆分表格單元格,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn