10

如何使用列表中的 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 文档?

4

3 回答 3

19

安装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")))));
            }
        }
    }
}

您还可以使用 Productivity Tool(相同的链接)从文档生成代码。它可以帮助理解如何使用 SDK API。

您可以对 Interop 执行相同操作:

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

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


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

于 2013-11-01T07:49:35.703 回答
0

如果您不想使用 Microsoft interop office,那么

我真的很喜欢这个

//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 尖角

于 2016-09-26T08:49:57.340 回答
0

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

在此之后,您可以尝试@Evgeny Timoshenko 的示例。

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);
            }
        }
    }
}
于 2017-04-11T06:39:15.580 回答