翻譯|使用教程|編輯:胡濤|2023-02-16 10:27:56.750|閱讀 143 次
概述:本文將教您如何使用 Spire.Doc for .NET在 C# 和 VB.NET 中的 Word 文檔中的表格中插入或提取圖像。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一款專門對(duì) Word 文檔進(jìn)行操作的 .NET 類庫(kù)。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類庫(kù)Spire 系列文檔處理組件均由中國(guó)本土團(tuán)隊(duì)研發(fā),不依賴第三方軟件,不受其他國(guó)家的技術(shù)或法律法規(guī)限制,同時(shí)適配國(guó)產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國(guó)產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Microsoft Word 中的表格可以包含多種元素,包括文本、圖像等等。有時(shí),您可能希望將圖像插入表格中以顯示一些信息或從表格中提取圖像以用于其他文檔。本文將教您如何使用 Spire.Doc for .NET在 C# 和 VB.NET 中的 Word 文檔中的表格中插入或提取圖像。
PM> Install-Package Spire.Doc
在 Microsoft Word 中,您可以將兩個(gè)或多個(gè)相鄰的單元格水平或垂直合并為一個(gè)更大的單元格。在 Spire.Doc 中,您可以使用Table.ApplyHorizontalMerge()或Table.ApplyVerticalMerge()方法實(shí)現(xiàn)相同的目的。以下是詳細(xì)步驟:
[ 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()方法,使您能夠?qū)?Word 表格中的一個(gè)單元格拆分為兩個(gè)或多個(gè)單元格。以下是詳細(xì)步驟:
[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 表格中插入或提取圖像,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn