2013-10-29 31 views
10

Listeden Microsoft.Office.Interop.Word ile .docx belgesini nasıl oluştururum? veya en iyi yolu docx.dll eklemektir?Microsoft.Office.Interop.Word ile .docx belgesini nasıl oluştururum?

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

Güncelleme. İlk sorum olacak bir soru yanlış olabilir. Microsoft.Office.Interop.Word ve DocX.dll arasındaki fark nedir? Her iki durumda da .docx belgesi oluşturmak ve açmak için Microsft Word'e ihtiyacım var mı? >Assemblies - -> Extensions ->DocumentFormat.OpenXmlAdd Reference:

+0

Interop.Word, Office'in makineye yüklenmesini gerektirir. DocX, doğrudan .docx dosyasının OpenXML içeriğini kesmez. Normal seçim Açık XML SDK'sıdır. Bu kütüphane için uzun vadeli destek normalde endişelenecek bir şey olurdu. Bilinen sorunlar için Sorunlar listesini gözden geçirin. –

cevap

18

OpenXML SDK yükledikten sonra mümkün DocumentFormat.OpenXml derleme başvurusu için olacaktır. Ayrıca WindowsBase referansına da ihtiyacınız olacak. Böyle, örneğin, belgeyi oluşturmak mümkün olacak daha

:

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

Ayrıca belgeden kodu oluşturmak için Verimlilik Aracı (aynı bağlantı) kullanabilirsiniz

. SDK API'sı ile çalışmanın nasıl yapıldığını anlamaya yardımcı olabilir.

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

Ancak bu durumda COM tür kitaplığı Microsoft başvuruda bulunmalıdır:

Sen Birlikte Çalışma aynısını yapabilirsiniz. Kelime Nesne Kitaplığı. Microsoft birlikte çalışma ofisi kullanmak istemiyorsanız How do I properly clean up Excel interop objects?

+2

Birlikte çalışma yöntemine geçmek istiyorsanız, uygulama nesnesini de çıkıp com nesnesini serbest bırakmalısınız. Aksi halde büyük bellek sızıntıları ile sonuçlanacaksınız. Bu yüzden http://alemiralles.blogspot.com.ar/2012/11/how-to-create-word-documents-from.html –

+0

Bu link bozuk. – KFP

0

sonra

Gerçekten bu

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

      } 

     } 

almak referansı sevdim: Burada


COM birlikte hakkında çok yararlı şeylerdir C-sharp corner

0

Office 2016 interop nesnelerine nasıl erişeceğinizi bilmiyorsanız, bağlantı (https://social.msdn.microsoft.com/Forums/vstudio/en-US/55fe7d16-998b-4c43-9746-45ff35310158/office-2016-interop-assemblies?forum=exceldev) size yardımcı olabilir.

Bundan sonra, @Evgeny Timoshenko örneğini deneyebilirsiniz.

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); 
      } 
     } 
    } 
} 
İlgili konular