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

如何使用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文档?

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

但在这种情况下,您应该引用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 changesList = GetChangesList(_LeadNo, dt.Rows[0]); foreach (KeyValuePair keyValue in changesList) { gDocument.ReplaceText(keyValue.Key.ToString().Trim(), keyValue.Value.ToString().Trim(), false); } //------------------------- End of make changes --------------------- gDocument.SaveAs(_saveAs); } } 

参考C-sharp角落

如果您不知道如何访问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); } } } }