2013-10-29 22 views
10

Jak utworzyć dokument .docx za pomocą Microsoft.Office.Interop.Word z listy? lub najlepszym sposobem jest dodanie docx.dll?Jak utworzyć dokument .docx za pomocą Microsoft.Office.Interop.Word?

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

Aktualizacja. Być może moje pierwsze pytanie jest niepoprawne. Jaka jest różnica między Microsoft.Office.Interop.Word i DocX.dll? Czy potrzebuję Microsft Word do tworzenia i otwierania dokumentu .docx w obu przypadkach?

+0

Firma Interop.Word wymaga zainstalowania pakietu Office na komputerze. DocX nie, bezpośrednio hackuje zawartość OpenXML pliku .docx. Zwykłym wyborem jest Open XML SDK. Długoterminowe wsparcie dla tej biblioteki normalnie byłoby czymś, o co można się martwić. Przejrzyj listę problemów pod kątem znanych problemów. –

Odpowiedz

18

Po zainstalowaniu OpenXML SDK będzie mógł odwołać DocumentFormat.OpenXml montaż: Add Reference ->Assemblies -> Extensions ->DocumentFormat.OpenXml. Musisz także podać numer WindowsBase.

niż będzie w stanie wygenerować dokument, na przykład tak:

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

Również można użyć Productivity Tool (ten sam link) do generowania kodu z dokumentu. Może pomóc zrozumieć, jak działa z SDK API.

Można zrobić to samo z 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); 
       } 
      } 
     } 
    } 
} 

Ale w tym przypadku należy odwołać typu COM biblioteki Microsoft. Biblioteka obiektów Word.


Oto bardzo użyteczne rzeczy o modelu COM: How do I properly clean up Excel interop objects?

+2

Jeśli chcesz przejść w ten sposób, powinieneś także zamknąć obiekt aplikacji i zwolnić obiekt com. W przeciwnym razie skończy się to ogromnymi wyciekami pamięci. Właśnie dlatego http://alemiralles.blogspot.com.ar/2012/11/how-to-create-word-documents-from.html –

+0

Ten link jest zły. – KFP

0

Jeśli nie chcesz korzystać z biurem współdziałania Microsoft następnie

bardzo lubiłem ten

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

      } 

     } 

wynos odniesienie C-sharp corner

0

Jeśli nie wiesz, jak uzyskać dostęp do obiektów współdziałania Office 2016, link (https://social.msdn.microsoft.com/Forums/vstudio/en-US/55fe7d16-998b-4c43-9746-45ff35310158/office-2016-interop-assemblies?forum=exceldev) może ci pomóc.

Po tym możesz wypróbować przykład @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); 
      } 
     } 
    } 
}