C# 如何将一个word文档的内容复制到另一个word文档中?

C# 如何将一个word文档的内容复制到另一个word文档中?,c#,C#,我有一个word文档,其中包含一些文本和图像。我想使用C#将word文档的内容复制到另一个word文档中 谢谢。试试这个。它应该会起作用。 这将把所有内容从第一个文档复制到第二个文档。确保两个文档都存在 using (WordprocessingDocument firstDocument = WordprocessingDocument.Open(@"E:\firstDocument.docx", false)) using (WordprocessingDocument secondDocu

我有一个word文档,其中包含一些文本和图像。我想使用C#将word文档的内容复制到另一个word文档中


谢谢。

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

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);
    }
}

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

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);
    }
}

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

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();
        }
    }

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

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();
        }
    }

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

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);
        }
    }
}

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

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);
        }
    }
}

将文件复制到新文件?将文件复制到新文件?这是一个更好的答案,因为它使用OpenXML(建议用于无人参与的服务器端操作)。这是一个更好的答案,因为它使用OpenXML(建议用于无人参与的服务器端操作)。