2013-10-29 5 views
10

Wie erstelle ich das DOCX-Dokument mit Microsoft.Office.Interop.Word von List? oder der beste Weg ist docx.dll hinzuzufügen?Wie erstelle ich das DOCX-Dokument mit Microsoft.Office.Interop.Word?

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

aktualisieren. Kann meine erste Frage ist ein wenig falsch. Was ist der Unterschied zwischen Microsoft.Office.Interop.Word und DocX.dll? Benötige ich Microsoft Word zum Erstellen und Öffnen von .docx Dokumenten in beiden Fällen?

+0

Interop.Word erfordert, dass Office auf dem Computer installiert wird. DocX nicht, es hackt den OpenXML-Inhalt einer .docx-Datei direkt. Die übliche Wahl ist das Open XML SDK. Langfristige Unterstützung für diese Bibliothek würde normalerweise etwas beunruhigen. Überprüfen Sie die Problemliste auf bekannte Probleme. –

Antwort

18

Nach OpenXML SDK Installation können Sie DocumentFormat.OpenXml Assembly verweisen: Add Reference ->Assemblies -> Extensions ->DocumentFormat.OpenXml. Außerdem müssen Sie auf WindowsBase verweisen.

als Sie in der Lage sein wird, Dokument zu erzeugen, zum Beispiel wie folgt aus:

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

Sie können auch Productivity Tool (das gleiche Link) verwenden, um Code aus dem Dokument zu erzeugen. Es kann helfen zu verstehen, wie mit der SDK-API gearbeitet wird.

Sie können das gleiche mit Interop tun:

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

Aber in diesem Fall sollten Sie COM-Typbibliothek Microsoft verweisen. Word-Objektbibliothek.


Hier sind sehr nützliche Dinge über COM-Interop: How do I properly clean up Excel interop objects?

+2

Wenn Sie den Interop-Weg gehen wollen, sollten Sie auch das Anwendungsobjekt beenden und das com-Objekt freigeben. Andernfalls werden Sie mit großen Speicherlecks enden. Deshalb http://alemiralles.blogspot.com.ar/2012/11/how-to-create-word-documents-from.html –

+0

Diese Verbindung ist schlecht. – KFP

0

Wenn Sie nicht Microsoft Interop Büro verwenden möchten dann

ich wirklich diesen mochte

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

      } 

     } 

nehmen Referenz C-sharp corner

0

Wenn Sie nicht wissen, wie Sie auf Office 2016-Interop-Objekte zugreifen können, hilft Ihnen der Link (https://social.msdn.microsoft.com/Forums/vstudio/en-US/55fe7d16-998b-4c43-9746-45ff35310158/office-2016-interop-assemblies?forum=exceldev).

Danach können Sie @ Evgeny Timoshenko Beispiel versuchen.

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