翻譯|使用教程|編輯:胡濤|2023-03-14 10:51:09.977|閱讀 148 次
概述:本文將向您展示如何訪問特定的書簽并將當前書簽內容替換為表格。
# 界面/圖表報表/文檔/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支持訪問文檔中的書簽,并在書簽位置插入文本、圖片、表格等對象。本文將向您展示如何訪問特定的書簽并將當前書簽內容替換為表格。
第 1 步:加載模板 Word 文檔。
Document doc = new Document(); doc.LoadFromFile(@"C:\Users\Administrator\Desktop\employee.docx");
第 2 步:創建一個表對象。
Table table = new Table(doc,true);
第 3 步:用樣本數據填充表。
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 步:通過名稱獲取特定書簽。
navigator.MoveToBookmark("bookmark_employee");
第 5 步:創建一個 TextBodyPart 實例并將表格添加到其中。
BookmarksNavigator navigator = new BookmarksNavigator(doc); navigator.MoveToBookmark("bookmark_employee");
第 6 步:用 TextBodyPart 對象替換當前書簽內容。
navigator.ReplaceBookmarkContent(part);
第 7 步:保存文件。
doc.SaveToFile("output.docx", FileFormat.Docx2013);
結果:
完整代碼:
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 文檔中的表格替換書簽,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn