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?
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. –