首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将一个词文档的内容复制到另一个词文档中?

如何将一个词文档的内容复制到另一个词文档中?
EN

Stack Overflow用户
提问于 2016-09-08 03:22:13
回答 3查看 7.4K关注 0票数 0

我有一个文字文档和一些图片。我希望使用C#将word文档的内容复制到另一个word文档中。

谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-09-08 03:54:03

尝尝这个。它应该能起作用。这将把所有内容从第一份文件复制到第二份文件。确保这两个文档都存在。

代码语言:javascript
运行
复制
using (WordprocessingDocument firstDocument = WordprocessingDocument.Open(@"E:\firstDocument.docx", false))
using (WordprocessingDocument secondDocument = WordprocessingDocument.Create(@"E:\secondDocument.docx", WordprocessingDocumentType.Document))
{
    foreach (var part in firstDocument.Parts)
    {
        secondDocument.AddPart(part.OpenXmlPart, part.RelationshipId);
    }
}
票数 2
EN

Stack Overflow用户

发布于 2016-09-08 04:04:35

下面的函数将向您展示如何打开、关闭和复制word文档.

代码语言:javascript
运行
复制
using MsWord = Microsoft.Office.Interop.Word;
private static void MsWordCopy()
    {
        var wordApp = new MsWord.Application();
        MsWord.Document documentFrom = null, documentTo = null;

        try
        {
            var fileNameFrom = @"C:\MyDocFile.docx";               

            wordApp.Visible = true;

            documentFrom = wordApp.Documents.Open(fileNameFrom, Type.Missing, true);
            MsWord.Range oRange = documentFrom.Content;
            oRange.Copy();

            var fileNameTo = @"C:\MyDocFile-Copy.docx";
            documentTo = wordApp.Documents.Add();
            documentTo.Content.PasteSpecial(DataType: MsWord.WdPasteOptions.wdKeepSourceFormatting);
            documentTo.SaveAs(fileNameTo);              
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        finally
        {
            if (documentFrom != null)
                documentFrom.Close(false);

            if (documentTo != null)
                documentTo.Close();

            if (wordApp != null)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);

            wordApp = null;
            documentFrom = null;
            documentTo = null;

            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }
票数 2
EN

Stack Overflow用户

发布于 2016-09-08 07:10:04

试试下面的代码。这可能对你有帮助。

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var app = new Microsoft.Office.Interop.Word.Application();
            var sourceDoc =    app.Documents.Open(@"D:\test.docx");

            sourceDoc.ActiveWindow.Selection.WholeStory();
            sourceDoc.ActiveWindow.Selection.Copy();

            var newDocument = new Microsoft.Office.Interop.Word.Document();
            newDocument.ActiveWindow.Selection.Paste();
            newDocument.SaveAs(@"D:\test1.docx");

            sourceDoc.Close(false);
            newDocument.Close();

            app.Quit();

            Marshal.ReleaseComObject(app);
            Marshal.ReleaseComObject(sourceDoc);
            Marshal.ReleaseComObject(newDocument);
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39382093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档