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?
Nguồn
2013-11-01 07:49:35
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. –