翻譯|使用教程|編輯:胡濤|2022-04-11 13:18:57.330|閱讀 240 次
概述:本篇文章主要介紹了如何使用Spire.Doc 在 C#、VB.NET 中將內容從一個 Word 文檔復制到另一個文檔,歡迎查閱!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在我們的日常工作中,我們經常會遇到將部分或全部內容(不包括頁眉或頁腳)從一個 Word 文檔復制到另一個文檔的要求。如果我們使用復制和粘貼功能,這很容易。
但是,我們如何以編程方式完成此任務?本文旨在介紹如何使用 Spire.Doc for .NET 將整個內容從源文檔傳輸到目標文檔的方法。如果只想轉幾段,請參考這篇文章。 代碼片段:
源文件:
目標文件:
代碼片段:
第 1步:初始化 Document 類的新對象并加載源文檔。
Document sourceDoc = new Document("source.docx");
第 2步:初始化另一個對象以加載目標文檔。
Document destinationDoc = new Document("target.docx");
第 3 步:從源文件中復制內容并將其插入到目標文件中。
foreach (Section sec in sourceDoc.Sections) { foreach (DocumentObject obj in sec.Body.ChildObjects) { destinationDoc.Sections[0].Body.ChildObjects.Add(obj.Clone()); } }
第 4 步:保存更改
destinationDoc.SaveToFile("target.docx", FileFormat.Docx2010);
結果:
完整代碼:
[C#]
using Spire.Doc; namespace CopyContent { class Program { static void Main(string[] args) { Document sourceDoc = new Document("source.docx"); Document destinationDoc = new Document("target.docx"); foreach (Section sec in sourceDoc.Sections) { foreach (DocumentObject obj in sec.Body.ChildObjects) { destinationDoc.Sections[0].Body.ChildObjects.Add(obj.Clone()); } } destinationDoc.SaveToFile("target.docx", FileFormat.Docx2010); System.Diagnostics.Process.Start("target.docx"); } } }
[VB.NET]
Imports Spire.Doc Namespace CopyContent Class Program Private Shared Sub Main(args As String()) Dim sourceDoc As New Document("source.docx") Dim destinationDoc As New Document("target.docx") For Each sec As Section In sourceDoc.Sections For Each obj As DocumentObject In sec.Body.ChildObjects destinationDoc.Sections(0).Body.ChildObjects.Add(obj.Clone()) Next Next destinationDoc.SaveToFile("target.docx", FileFormat.Docx2010) System.Diagnostics.Process.Start("target.docx") End Sub End Class End Namespace
歡迎下載|體驗更多E-iceblue產品
如需獲取更多產品相關信息請咨詢
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn