翻譯|使用教程|編輯:李顯亮|2021-07-15 10:22:37.453|閱讀 215 次
概述:有些人使用 Microsoft Office Interop 庫來創(chuàng)建 Word 文檔,但是他們經(jīng)常問如何在沒有互操作的情況下在 C# 中創(chuàng)建 Word 文檔的問題。這就是Aspose.Words for .NET發(fā)揮作用的地方,可以幫助輕松地在 C# 中創(chuàng)建 Word 文檔。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
有些人使用 Microsoft Office Interop 庫來創(chuàng)建 Word 文檔,但是他們經(jīng)常問如何在沒有互操作的情況下在 C# 中創(chuàng)建 Word 文檔的問題。這就是Aspose.Words for .NET發(fā)揮作用的地方,可以幫助輕松地在 C# 中創(chuàng)建 Word 文檔。
下面的步驟和 C# 代碼示例一起回答了這個問題。這表明可以在 .NET 應(yīng)用程序(無論是 .NET 控制臺實用程序、Windows 應(yīng)用程序還是 ASP.NET Web 項目)中從頭開始創(chuàng)建DOCX或DOC文件是多么容易和高效。Aspose.Words 無縫地與每種 .NET 平臺技術(shù)配合使用。
Aspose.Words for .NET是高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務(wù)。該API支持生成,修改,轉(zhuǎn)換,呈現(xiàn)和打印文檔的能力,而無需在跨平臺應(yīng)用程序中直接利用Microsoft Word。你可以下載《Aspose.Words for .NET開發(fā)者指南》獲取更多幫助。
代碼如下:
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"); } } }
在此 C# 代碼示例中,我們創(chuàng)建了一個僅包含兩行文本的簡單 Word 文檔,并將粗體屬性應(yīng)用于文本。但是,可以通過添加表格、單元格和Word 文檔對象模型 (DOM) 的其他不同對象來執(zhí)行更多操作并擴(kuò)展上述代碼。
要執(zhí)行 Aspose.Words for .NET API 的上述 C# 代碼,您不需要在您的計算機(jī)或?qū)惭b應(yīng)用程序的服務(wù)器上安裝 Microsoft Word。這使得創(chuàng)建 Word 文檔變得更加容易和快捷。
如果您發(fā)現(xiàn)任何其他關(guān)于在沒有互操作的情況下在 c# 中創(chuàng)建 word 文檔的問題,請在Aspose.Words 免費支持論壇中提問哦 。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn