翻譯|使用教程|編輯:胡濤|2022-04-11 13:18:57.330|閱讀 240 次
概述:本篇文章主要介紹了如何使用Spire.Doc 在 C#、VB.NET 中將內(nèi)容從一個(gè) Word 文檔復(fù)制到另一個(gè)文檔,歡迎查閱!
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
在我們的日常工作中,我們經(jīng)常會(huì)遇到將部分或全部?jī)?nèi)容(不包括頁眉或頁腳)從一個(gè) Word 文檔復(fù)制到另一個(gè)文檔的要求。如果我們使用復(fù)制和粘貼功能,這很容易。
但是,我們?nèi)绾我跃幊谭绞酵瓿纱巳蝿?wù)?本文旨在介紹如何使用 Spire.Doc for .NET 將整個(gè)內(nèi)容從源文檔傳輸?shù)侥繕?biāo)文檔的方法。如果只想轉(zhuǎn)幾段,請(qǐng)參考這篇文章。 代碼片段:
源文件:
目標(biāo)文件:
代碼片段:
第 1步:初始化 Document 類的新對(duì)象并加載源文檔。
Document sourceDoc = new Document("source.docx");
第 2步:初始化另一個(gè)對(duì)象以加載目標(biāo)文檔。
Document destinationDoc = new Document("target.docx");
第 3 步:從源文件中復(fù)制內(nèi)容并將其插入到目標(biāo)文件中。
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);
結(jié)果:
完整代碼:
[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
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
如需獲取更多產(chǎn)品相關(guān)信息請(qǐng)咨詢
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn