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