翻譯|使用教程|編輯:胡濤|2023-02-16 10:27:56.750|閱讀 143 次
概述:本文將教您如何使用 Spire.Doc for .NET在 C# 和 VB.NET 中的 Word 文檔中的表格中插入或提取圖像。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫Spire 系列文檔處理組件均由中國本土團隊研發,不依賴第三方軟件,不受其他國家的技術或法律法規限制,同時適配國產操作系統如中科方德、中標麒麟等,兼容國產文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Microsoft Word 中的表格可以包含多種元素,包括文本、圖像等等。有時,您可能希望將圖像插入表格中以顯示一些信息或從表格中提取圖像以用于其他文檔。本文將教您如何使用 Spire.Doc for .NET在 C# 和 VB.NET 中的 Word 文檔中的表格中插入或提取圖像。
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
以上便是如何在C#/VB.NET:在 Word 表格中插入或提取圖像,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn