如何使用 Microsoft.Office.Interop.Word 创建.docx文档

本文关键字:创建 docx 文档 Word Interop 何使用 Microsoft Office | 更新日期: 2023-09-27 18:37:12

如何使用 List 中的 Microsoft.Office.Interop.Word 创建.docx文档? 或者最好的方法是添加docx.dll?

http://www.c-sharpcorner.com/UploadFile/scottlysle/using-the-docx-dll-to-programmatically-create-word-documents/

更新。可能是我的第一个问题是一点点不正确。Microsoft.Office.Interop.Word 和 DocX.dll 有什么区别?在这两种情况下,我都需要Microsft Word来创建和打开.docx文档吗?

如何使用 Microsoft.Office.Interop.Word 创建.docx文档

upd:看起来openxml sdk现在正在通过nuget安装,看看他们的github存储库 https://github.com/OfficeDev/Open-XML-SDK

安装 OpenXML SDK 后,您将能够引用DocumentFormat.OpenXml程序集:Add Reference -> Assemblies -> Extensions -> DocumentFormat.OpenXml .您还需要引用WindowsBase

然后您将能够生成文档,例如,如下所示:

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace MyNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var document = WordprocessingDocument.Create(
                "test.docx", WordprocessingDocumentType.Document))
            {
                document.AddMainDocumentPart();
                document.MainDocumentPart.Document = new Document(
                    new Body(new Paragraph(new Run(new Text("some text")))));
            }
        }
    }
}

您也可以使用快捷会议工具(同一链接)从文档生成代码。它有助于了解如何使用 SDK API。

您可以使用互操作执行相同的操作:

using System.Reflection;
using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
namespace Interop1
{
    class Program
    {
        static void Main(string[] args)
        {
            Application application = null;
            try
            {
                application = new Application();
                var document = application.Documents.Add();
                var paragraph = document.Paragraphs.Add();
                paragraph.Range.Text = "some text";
                string filename = GetFullName();
                application.ActiveDocument.SaveAs(filename, WdSaveFormat.wdFormatDocument);
                document.Close();
            
            }
            finally
            {
                if (application != null)
                {
                    application.Quit();
                    Marshal.FinalReleaseComObject(application);
                }
            }
        }
    }
}

但在这种情况下,您应该引用 COM 类型库Microsoft。Word 对象库。

<小时 />

以下是有关 COM 互操作的非常有用的内容:如何正确清理 Excel 互操作对象?

如果您不想使用Microsoft互操作办公室,那么

我真的很喜欢这个

//Add reference DocX.dll
using Novacode;
  // reference to the working document.
        static DocX gDocument;
 public void CreateWithOpenDoc(string _fileName, string _saveAs, int _LeadNo)
        {
            if (File.Exists(_fileName))
            {
                gDocument = DocX.Load(_fileName);

                //--------------------- Make changes -------------------------------
                // Strong-Type
                Dictionary<string, string> changesList = GetChangesList(_LeadNo, dt.Rows[0]);
                foreach (KeyValuePair<string, string> keyValue in changesList)
                {
                    gDocument.ReplaceText(keyValue.Key.ToString().Trim(), keyValue.Value.ToString().Trim(), false);
                }
                //------------------------- End of make changes ---------------------

                gDocument.SaveAs(_saveAs);
            }
        }

参考 C型尖角

如果您不知道如何访问 Office 2016 互操作对象,链接 (https://social.msdn.microsoft.com/Forums/vstudio/en-US/55fe7d16-998b-4c43-9746-45ff35310158/office-2016-interop-assemblies?forum=exceldev) 可以为您提供帮助。

在此之后,您可以尝试@Evgeny铁木辛哥的例子。

class Program
{
    static void Main(string[] args)
    {
        Application application = null;
        try
        {
            application = new Application();
            var document = application.Documents.Add();
            var paragraph = document.Paragraphs.Add();
            paragraph.Range.Text = "some text";
            string filename = GetFullName();
            application.ActiveDocument.SaveAs(filename, WdSaveFormat.wdFormatDocument);
            document.Close();
        }
        finally
        {
            if (application != null)
            {
                application.Quit();
                Marshal.FinalReleaseComObject(application);
            }
        }
    }
}