翻譯|使用教程|編輯:胡濤|2023-03-14 10:51:09.977|閱讀 148 次
概述:本文將向您展示如何訪問(wèn)特定的書(shū)簽并將當(dāng)前書(shū)簽內(nèi)容替換為表格。
# 界面/圖表報(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 等格式
書(shū)簽是指定 Word 文檔中重要位置的好方法。Spire.Doc支持訪問(wèn)文檔中的書(shū)簽,并在書(shū)簽位置插入文本、圖片、表格等對(duì)象。本文將向您展示如何訪問(wèn)特定的書(shū)簽并將當(dāng)前書(shū)簽內(nèi)容替換為表格。
第 1 步:加載模板 Word 文檔。
Document doc = new Document(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\employee.docx");
第 2 步:創(chuàng)建一個(gè)表對(duì)象。
Table table = new Table(doc,true);
第 3 步:用樣本數(shù)據(jù)填充表。
DataTable dt = new DataTable(); dt.Columns.Add("id", typeof(string)); dt.Columns.Add("name", typeof(string)); dt.Columns.Add("job", typeof(string)); dt.Columns.Add("email", typeof(string)); dt.Columns.Add("salary", typeof(string)); dt.Rows.Add(new string[] { "Emp_ID", "Name", "Job", "E-mail", "Salary" }); dt.Rows.Add(new string[] { "0241","Andrews", "Engineer", "andrews@outlook.com" ,"3.8K"}); dt.Rows.Add(new string[] { "0242","White", "Manager", "white@outlook.com","4.2K" }); dt.Rows.Add(new string[] { "0243","Martin", "Secretary", "martin@gmail.com", "3.5K" }); table.ResetCells(dt.Rows.Count, dt.Columns.Count); for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { table.Rows[i].Cells[j].AddParagraph().AppendText(dt.Rows[i][j].ToString()); } }
第 4 步:通過(guò)名稱(chēng)獲取特定書(shū)簽。
navigator.MoveToBookmark("bookmark_employee");
第 5 步:創(chuàng)建一個(gè) TextBodyPart 實(shí)例并將表格添加到其中。
BookmarksNavigator navigator = new BookmarksNavigator(doc); navigator.MoveToBookmark("bookmark_employee");
第 6 步:用 TextBodyPart 對(duì)象替換當(dāng)前書(shū)簽內(nèi)容。
navigator.ReplaceBookmarkContent(part);
第 7 步:保存文件。
doc.SaveToFile("output.docx", FileFormat.Docx2013);
結(jié)果:
完整代碼:
using Spire.Doc; using Spire.Doc.Documents; using System.Data; namespace ReplaceBookmark { class Program { static void Main(string[] args) { Document doc = new Document(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\employee.docx"); Table table = new Table(doc, true); DataTable dt = new DataTable(); dt.Columns.Add("id", typeof(string)); dt.Columns.Add("name", typeof(string)); dt.Columns.Add("job", typeof(string)); dt.Columns.Add("email", typeof(string)); dt.Columns.Add("salary", typeof(string)); dt.Rows.Add(new string[] { "Emp_ID", "Name", "Job", "E-mail", "Salary" }); dt.Rows.Add(new string[] { "0241", "Andrews", "Engineer", "andrews@outlook.com", "3.8K" }); dt.Rows.Add(new string[] { "0242", "White", "Manager", "white@outlook.com", "4.2K" }); dt.Rows.Add(new string[] { "0243", "Martin", "Secretary", "martin@gmail.com", "3.5K" }); table.ResetCells(dt.Rows.Count, dt.Columns.Count); for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { table.Rows[i].Cells[j].AddParagraph().AppendText(dt.Rows[i][j].ToString()); } } BookmarksNavigator navigator = new BookmarksNavigator(doc); navigator.MoveToBookmark("bookmark_employee"); TextBodyPart part = new TextBodyPart(doc); part.BodyItems.Add(table); navigator.ReplaceBookmarkContent(part); doc.SaveToFile("output.docx", FileFormat.Docx2013); System.Diagnostics.Process.Start("output.docx"); } } }
以上便是用 C#、VB.NET 中的 Word 文檔中的表格替換書(shū)簽,如果您有其他問(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