Ebben az oktatóanyagban megtanuljuk, hogyan hozhat létre Word-dokumentumot C# használatával, írhat szöveget különböző betűtípusokkal, illeszthet be egy hiperhivatkozást, és mentheti a dokumentumot DOCX fájlformátumba.
A következő lépések elmagyarázzák, hogyan hoz létre C# Word dokumentumfájlt.
Word-dokumentum létrehozásának lépései C# használatával
- Telepítse a Aspose.Words for .NET csomagot a NuGetből
- Adjon hozzá hivatkozást Aspose.Words és Aspose.Words.Saving névterek
- A Document és a DocumentBuilder osztály példányának létrehozása
- Írjon szöveget, és helyezzen be hiperhivatkozást a dokumentumba
- Mentse a dokumentumot DOCX fájlformátumba
Korábban megvizsgáltuk a Fejléc és lábléc beszúrása DOCX-be C# használatával webhelyet. A következő kódpélda C#-ban Word dokumentumot generál jól ismert DOCX fájlformátumban. Ugyanezt a megközelítést használhatja Word-dokumentumok létrehozásához különböző fájlformátumokban, pl. DOC, RTF stb. a Document.Save módszerrel. Ez a kódpélda ott használható, ahol a .NET telepítve van.
Kód Word dokumentum generálásához C# használatával
using System.Drawing; | |
using Aspose.Words; | |
using Aspose.Words.Saving; | |
namespace KBCodeExamples | |
{ | |
class how_to_create_word_document_using_c_sharp | |
{ | |
public static void Main(string[] args) | |
{ | |
//Set Aspose license before creating blank Word document | |
Aspose.Words.License AsposeWordsLicense = new Aspose.Words.License(); | |
AsposeWordsLicense.SetLicense(@"Aspose.Words.lic"); | |
// Create a blank Word document | |
Document doc = new Document(); | |
// Initialize a new instance of DocumentBuilder class | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
// Insert a string surrounded by a border | |
builder.Font.Border.Color = Color.Green; | |
builder.Font.Border.LineWidth = 2.5d; | |
builder.Font.Border.LineStyle = LineStyle.DashDotStroker; | |
builder.Write("Text surrounded by green border."); | |
// Remove all font formatting specified explicitly | |
builder.Font.ClearFormatting(); | |
builder.InsertBreak(BreakType.ParagraphBreak); | |
builder.Write("For more information, please visit the "); | |
// Insert a hyperlink and emphasize it with custom formatting | |
// The hyperlink will be a clickable piece of text which will take us to the location specified in the URL | |
builder.Font.Color = Color.Blue; | |
builder.Font.Underline = Underline.Single; | |
builder.InsertHyperlink("Aspose Knowledge Base", "https://kb.aspose.com/", false); | |
builder.Font.ClearFormatting(); | |
builder.Writeln("."); | |
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions | |
{ | |
Compliance = OoxmlCompliance.Iso29500_2008_Strict, | |
SaveFormat = SaveFormat.Docx | |
}; | |
// Save the document with strict compliance level | |
doc.Save("create word document using C#.docx", saveOptions); | |
} | |
} | |
} |
A fenti kódpélda a c#-ban Word dokumentum létrehozása a semmiből. A dokumentumosztály egy üres Word dokumentumot jelent. A DocumentBuildert a Dokumentumhoz kell társítania. A DocumentBuilder segítségével különféle típusú tartalmakat illeszthet be a Word-dokumentumba, például táblázatot, képeket, szöveget stb.