翻譯|使用教程|編輯:胡濤|2022-04-13 13:33:32.920|閱讀 281 次
概述:本文將解釋我們如何使用 Spire.Doc for .NET 以編程方式通過分節符將 Word 文檔拆分為多個文檔,而不是手動復制和刪除。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在 Word 中,我們可以以最簡單的方式拆分 Word 文檔——打開原始文檔的副本,刪除不需要的部分,然后將剩余部分保存到本地驅動器。但是逐節做這個是相當麻煩和無聊的。本文將解釋我們如何使用 Spire.Doc for .NET 以編程方式通過分節符將 Word 文檔拆分為多個文檔,而不是手動復制和刪除。
詳細步驟和代碼片段:
第 1步:初始化一個新的word文檔對象并加載包含兩個部分的原始word文檔。
Document document = new Document(); document.LoadFromFile("Test.docx");
第 2 步:定義另一個新的 word 文檔對象。
Document newWord;
第 3 步:遍歷原word文檔的所有section,將每個section克隆并作為新的section添加到一個新的word文檔中,然后將文檔保存到指定路徑。
for (int i = 0; i < document.Sections.Count; i++) { newWord = new Document(); newWord.Sections.Add(document.Sections[i].Clone()); newWord.SaveToFile(String.Format(@"test\out_{0}.docx", i)); }
運行項目,我們將得到以下輸出:
完整代碼:
using System; using Spire.Doc; namespace Split_Word_Document { class Program { static void Main(string[] args) { Document document = new Document(); document.LoadFromFile("Test.doc"); Document newWord; for (int i = 0; i < document.Sections.Count; i++) { newWord = new Document(); newWord.Sections.Add(document.Sections[i].Clone()); newWord.SaveToFile(String.Format(@"test\out_{0}.docx", i)); } } } }
歡迎下載|體驗更多E-iceblue產品
如需獲取更多產品相關信息請咨詢
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn