如何使用 Microsoft.Office.Interop.Word 创建 .docx 文档? [英] How do I create the .docx document with Microsoft.Office.Interop.Word?

查看:39
本文介绍了如何使用 Microsoft.Office.Interop.Word 创建 .docx 文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I create the .docx document with Microsoft.Office.Interop.Word from List? or the best way is to add 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 文档吗?

Update. May be my first question is a litle incorrect. What is the difference between Microsoft.Office.Interop.Word and DocX.dll? Do I need Microsft Word for creating and opening .docx document in both cases?

推荐答案

安装后 OpenXML SDK 你将能够引用 DocumentFormat.OpenXml 程序集:Add Reference -> Assemblies ->扩展 -> DocumentFormat.OpenXml.您还需要参考 WindowsBase.

After installing OpenXML SDK you will able to reference DocumentFormat.OpenXml assembly: Add Reference -> Assemblies -> Extensions -> DocumentFormat.OpenXml. Also you will need to reference WindowsBase.

然后你就可以生成文档了,比如这样:

Than you will be able to generate document, for example, like this:

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.

Also you can use Productivity Tool (the same link) to generate code from document. It can help to understand how work with SDK API.

你可以用 Interop 做同样的事情:

You can do the same with 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);
                }
            }
        }
    }
}

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

But in this case you should reference COM type library Microsoft. Word Object Library.

这里是关于 COM 互操作的非常有用的东西:我如何正确清理 Excel 互操作对象?

Here are very useful things about COM interop: How do I properly clean up Excel interop objects?

这篇关于如何使用 Microsoft.Office.Interop.Word 创建 .docx 文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆