Niektórzy używają biblioteki Microsoft Office Interop do tworzenia dokumentów Word, jednak często zadają pytanie, jak stworzyć dokument Word w C# bez interopu. W tym miejscu do gry wkracza Aspose.Words for .NET, aby łatwo utworzyć dokument Worda w języku C#.
Odpowiedź na to pytanie znajduje się w poniższych krokach wraz z próbką kodu C#. Pokazuje to, jak łatwo i wydajnie można utworzyć plik DOCX lub DOC od podstaw w aplikacjach .NET, niezależnie od tego, czy jest to narzędzie konsoli .NET, aplikacja Windows, czy projekt sieciowy ASP.NET. Aspose.Words bezproblemowo współpracuje z każdą technologią platformy .NET.
Kroki tworzenia dokumentu programu Word w języku C# bez automatyzacji
- Użyj Aspose.Words for .NET pakietu NuGet
- Dodaj odwołanie do Aspose.Words namespace
- Użyj metody SetLicense, aby zastosować licencję Aspose
- Utwórz nową instancję Document Class
- Utwórz obiekt DocumentBuilder Class i przekaż obiekt Document do konstruktora
- Ustaw wymagane właściwości obiektu DocumentBuilder
- Zapisz wyjściowy dokument programu Word jako plik lub strumień
Kod do tworzenia dokumentów programu Word w języku C# bez automatyzacji
using System; | |
//Add reference to Aspose.Words for .NET API | |
//Use following namespaces to Word Document generator code | |
using Aspose.Words; | |
namespace CreateWordDocumentWithoutInterop | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Set Aspose license before creating Word document without Interop | |
//using Aspose.Words for .NET | |
Aspose.Words.License AsposeWordsLicense = new Aspose.Words.License(); | |
AsposeWordsLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//Create an instance of Document class of Aspose.Words for .NET | |
//This initiates a blank Word document | |
Document WordDocumentWithoutInterop = new Document(); | |
//An instance of DocumentBuilder class is used to add | |
//content to the Word Document | |
DocumentBuilder WordDocumentBuilder = new DocumentBuilder(WordDocumentWithoutInterop); | |
//Add some text with bold formatting | |
WordDocumentBuilder.Bold = true; | |
WordDocumentBuilder.Writeln("We're adding this line of text in the word document using DocumentBuilder Class"); | |
WordDocumentBuilder.Writeln("This does not require Office Interop or Office Automation"); | |
//Finally, save the generated word document to docx format | |
WordDocumentWithoutInterop.Save("Word_Document_Created_Without_Interop_using_CSharp.docx"); | |
} | |
} | |
} |
W tym przykładzie kodu C# utworzyliśmy proste Word document z zaledwie dwoma wierszami tekstu i zastosowaliśmy pogrubiony atrybut do tekstu. Możesz jednak zrobić o wiele więcej i rozszerzyć powyższy kod, dodając tabele, komórki i różne inne obiekty Word Document Object Model (DOM).
Aby wykonać powyższy kod C# Aspose.Words for .NET API, nie musisz instalować Microsoft Word na swoim komputerze ani serwerze, na którym aplikacja zostanie zainstalowana. Dzięki temu tworzenie dokumentów Word jest o wiele łatwiejsze i szybsze.