c# – 如何使用Microsoft.Office.Interop.Word创建.docx文档?
发布时间:2020-12-15 17:47:05 所属栏目:百科 来源:网络整理
导读:如何从列表中的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文档?或者最好的方法是添加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文档? 解决方法
安装
OpenXML SDK后,您可以参考DocumentFormat.OpenXml程序集:Add Reference – >装配 – >
扩展 – > 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. 你可以在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对象库. 这是关于COM互操作:How do I properly clean up Excel interop objects?的非常有用的东西 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |