翻譯|使用教程|編輯:胡濤|2023-05-18 11:41:19.270|閱讀 185 次
概述:本文介紹如何使用spire.doc,在C#中從word文檔中插入、讀取和刪除表格,歡迎查閱~
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一款專(zhuān)門(mén)對(duì) Word 文檔進(jìn)行操作的 .NET 類(lèi)庫(kù)。在于幫助開(kāi)發(fā)人員無(wú)需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專(zhuān)業(yè)開(kāi)發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開(kāi)發(fā)工具,專(zhuān)注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類(lèi)庫(kù)Spire 系列文檔處理組件均由中國(guó)本土團(tuán)隊(duì)研發(fā),不依賴(lài)第三方軟件,不受其他國(guó)家的技術(shù)或法律法規(guī)限制,同時(shí)適配國(guó)產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國(guó)產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
在 Word 中,文本框可以包含文本、圖像和表格等多種元素。本文演示了如何使用 Spire.Doc 在 word 文本框中插入表格,以及從 word 文本框中讀取和刪除現(xiàn)有表格。
插入表格
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace InsertTable { class Program { static void Main(string[] args) { //Create a Document instance Document document = new Document(); //Add a section Section section = document.AddSection(); //Add a paragraph to the section Paragraph paragraph = section.AddParagraph(); //Add textbox to the paragraph TextBox textbox = paragraph.AppendTextBox(300, 100); //Add text to textbox Paragraph textboxParagraph = textbox.Body.AddParagraph(); TextRange textboxRange = textboxParagraph.AppendText("Table 1"); textboxRange.CharacterFormat.FontName = "Arial"; //Insert table to textbox Table table = textbox.Body.AddTable(true); //Specify the number of rows and columns of the table table.ResetCells(4, 4); string[,] data = new string[,] { {"Name","Age","Gender","ID" }, {"John","28","Male","0023" }, {"Steve","30","Male","0024" }, {"Lucy","26","female","0025" } }; //Add data to table for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { TextRange tableRange = table[i, j].AddParagraph().AppendText(data[i, j]); tableRange.CharacterFormat.FontName = "Arial"; } } //Apply style to table table.ApplyStyle(DefaultTableStyle.LightGridAccent3); //Save the document document.SaveToFile("Output.docx", FileFormat.Docx2013); } } }
讀表
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using System.IO; using System.Text; namespace ReadTable { class Program { static void Main(string[] args) { //Create a Document instance and load the word document Document document = new Document("Output.docx"); //Get the first textbox TextBox textbox = document.TextBoxes[0]; //Get the first table in the textbox Table table = textbox.Body.Tables[0] as Table; StringBuilder sb = new StringBuilder(); //Loop through the paragraphs of the table and extract text to a .txt file foreach (TableRow row in table.Rows) { foreach (TableCell cell in row.Cells) { foreach (Paragraph paragraph in cell.Paragraphs) { sb.AppendLine(paragraph.Text); } } } File.WriteAllText("text.txt", sb.ToString()); } } }
刪除表
using Spire.Doc; using Spire.Doc.Fields; namespace DeleteTable { class Program { static void Main(string[] args) { //Create a Document instance and load the word document Document document = new Document("Output.docx"); //Get the first textbox TextBox textbox = document.TextBoxes[0]; //Remove the first table from the textbox textbox.Body.Tables.RemoveAt(0); //Save the document document.SaveToFile("RemoveTable.docx", FileFormat.Docx2013); } } }
以上便是如何在C#中從word文檔中插入、讀取和刪除表格,如果您有其他問(wèn)題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢(xún) ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn