翻譯|使用教程|編輯:胡濤|2023-01-29 09:33:13.400|閱讀 202 次
概述:本文主要介紹如何通過在 C# 中克隆來插入現(xiàn)有表,歡迎查閱
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Spire.Doc for .NET是一款專門對 Word 文檔進行操作的 .NET 類庫。在于幫助開發(fā)人員無需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專業(yè)開發(fā)經(jīng)驗Spire系列辦公文檔開發(fā)工具,專注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
在某些情況下,我們需要對現(xiàn)有表進行一些修改,但又不想破壞原始數(shù)據(jù),因此我們希望復(fù)制現(xiàn)有表,然后在新表中進行一些更改。我們?nèi)绾潍@得復(fù)制的表格?最簡單的方法是克隆。將引入一個解決方案來復(fù)制表并修改一些數(shù)據(jù),然后通過 Spire.Doc 在原始表之后插入新表。
Spire.Doc for .NET是一個獨立的 .NET Word 組件,它提供了一種方法 Table.clone() 以允許用戶復(fù)制現(xiàn)有表格。
首先:加載帶有表格的word文檔。
Document doc = new Document(); doc.LoadFromFile(@"CopyTable.doc");
原文檔效果截圖:
其次:提取現(xiàn)有表并調(diào)用table.clone()方法復(fù)制它。
Section se = doc.Sections[0]; Table original_Table =(Table) se.Tables[0]; Table copied_Table = original_Table.Clone();
再次:提取最后一行然后遍歷其單元格以修改數(shù)據(jù)。
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]; }
最后:調(diào)用 Section. tables.add() 方法將復(fù)制的表格添加到節(jié)中并保存此文檔。
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# 中克隆來插入現(xiàn)有表,如果您有其他問題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗更多E-iceblue產(chǎn)品
獲取更多信息請咨詢 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn