2013-10-29 15 views
10

Làm cách nào để tạo tài liệu .docx bằng Microsoft.Office.Interop.Word từ Danh sách? hoặc cách tốt nhất là thêm docx.dll?Làm cách nào để tạo tài liệu .docx bằng Microsoft.Office.Interop.Word?

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

Update. Có thể câu hỏi đầu tiên của tôi là không đúng. Sự khác nhau giữa Microsoft.Office.Interop.Word và DocX.dll là gì? Tôi có cần Microsft Word để tạo và mở tài liệu .docx trong cả hai trường hợp không?

+0

Interop.Word yêu cầu Office được cài đặt trên máy. DocX không, nó trực tiếp hack nội dung OpenXML của tệp .docx. Lựa chọn thông thường là Open XML SDK. Hỗ trợ lâu dài cho thư viện này thường sẽ là điều gì đó để băn khoăn. Xem lại danh sách Sự cố cho các sự cố đã biết. –

Trả lời

18

Sau khi cài đặt OpenXML SDK bạn sẽ có thể tham khảo DocumentFormat.OpenXml lắp ráp: Add Reference ->Assemblies -> Extensions ->DocumentFormat.OpenXml. Ngoài ra, bạn sẽ cần phải tham khảo WindowsBase.

Hơn bạn sẽ có thể tạo ra tài liệu, ví dụ, như thế này:

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

Ngoài ra bạn có thể sử dụng Năng suất Tool (liên kết giống nhau) để tạo ra mã từ tài liệu. Nó có thể giúp hiểu cách hoạt động với SDK SDK.

Bạn có thể làm tương tự với 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); 
       } 
      } 
     } 
    } 
} 

Nhưng trong trường hợp này, bạn nên tham khảo kiểu COM thư viện Microsoft. Thư viện đối tượng từ.


Dưới đây là những điều rất hữu ích về COM interop: How do I properly clean up Excel interop objects?

+2

Nếu bạn muốn đi theo cách tương tác, bạn cũng nên thoát khỏi đối tượng ứng dụng và giải phóng đối tượng com. Nếu không, bạn sẽ kết thúc với rò rỉ bộ nhớ rất lớn. Đây là lý do tại sao http://alemiralles.blogspot.com.ar/2012/11/how-to-create-word-documents-from.html –

+0

Liên kết đó không tốt. – KFP

0

Nếu bạn không muốn sử dụng văn phòng Microsoft interop sau đó

Tôi thực sự thích

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

      } 

     } 

mất thông tin này C-sharp corner

0

Nếu bạn không biết cách truy cập các đối tượng interop văn phòng năm 2016, liên kết (https://social.msdn.microsoft.com/Forums/vstudio/en-US/55fe7d16-998b-4c43-9746-45ff35310158/office-2016-interop-assemblies?forum=exceldev) có thể giúp bạn.

Sau này, bạn có thể thử ví dụ của @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); 
      } 
     } 
    } 
} 
Các vấn đề liên quan