翻譯|使用教程|編輯:胡濤|2023-01-29 09:33:13.400|閱讀 202 次
概述:本文主要介紹如何通過在 C# 中克隆來插入現有表,歡迎查閱
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發人員無需安裝 Microsoft Word情況下,輕松快捷高效地創建、編輯、轉換和打印 Microsoft Word 文檔。擁有近10年專業開發經驗Spire系列辦公文檔開發工具,專注于創建、編輯、轉換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
在某些情況下,我們需要對現有表進行一些修改,但又不想破壞原始數據,因此我們希望復制現有表,然后在新表中進行一些更改。我們如何獲得復制的表格?最簡單的方法是克隆。將引入一個解決方案來復制表并修改一些數據,然后通過 Spire.Doc 在原始表之后插入新表。
Spire.Doc for .NET是一個獨立的 .NET Word 組件,它提供了一種方法 Table.clone() 以允許用戶復制現有表格。
首先:加載帶有表格的word文檔。
Document doc = new Document(); doc.LoadFromFile(@"CopyTable.doc");
原文檔效果截圖:
其次:提取現有表并調用table.clone()方法復制它。
Section se = doc.Sections[0]; Table original_Table =(Table) se.Tables[0]; Table copied_Table = original_Table.Clone();
再次:提取最后一行然后遍歷其單元格以修改數據。
string[] st = new string[] { "Guyana", "Georgetown", "South America", "214969", "800000" }; //get the last row of copied table TableRow lastRow = copied_Table.Rows[copied_Table.Rows.Count - 1]; //change lastRow data. lastRow.RowFormat.BackColor = Color.Gray; for (int i = 0; i < lastRow.Cells.Count; i++) { lastRow.Cells[i].Paragraphs[0].Text = st[i]; }
最后:調用 Section. tables.add() 方法將復制的表格添加到節中并保存此文檔。
se.Tables.Add(copied_Table); doc.SaveToFile("result.doc", FileFormat.Doc); The result document effect screenshot:
完整代碼:
using Spire.Doc; using System.Drawing; namespace InsertingaAnExistingTable { class Program { static void Main(string[] args) { //load a word document Document doc = new Document(); doc.LoadFromFile(@"CopyTable.doc"); // extract the existing table Section se = doc.Sections[0]; Table original_Table =(Table) se.Tables[0]; // copy the existing table to copied_Table via Table.clone() Table copied_Table = original_Table.Clone(); string[] st = new string[] { "Guyana", "Georgetown", "South America", "214969", "800000" }; //get the last row of table TableRow lastRow = copied_Table.Rows[copied_Table.Rows.Count - 1]; //change last row data. lastRow.RowFormat.BackColor = Color.Gray; for (int i = 0; i < lastRow.Cells.Count; i++) { lastRow.Cells[i].Paragraphs[0].Text = st[i]; } // add copied_Table in section se.Tables.Add(copied_Table); doc.SaveToFile("result.doc", FileFormat.Doc); } } }
以上便是 如何 通過在 C# 中克隆來插入現有表,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn