有些人使用 Microsoft Office Interop 库来创建 Word 文档,但是他们经常问如何在没有互操作的情况下在 C# 中创建 Word 文档的问题。这就是Aspose.Words for .NET发挥作用的地方,可以帮助轻松地在 C# 中创建 Word 文档。
有些人使用 Microsoft Office Interop 库来创建 Word 文档,但是他们经常问如何在没有互操作的情况下在 C# 中创建 Word 文档的问题。这就是Aspose.Words for .NET发挥作用的地方,可以帮助轻松地在 C# 中创建 Word 文档。
下面的步骤和 C# 代码示例一起回答了这个问题。这表明可以在 .NET 应用程序(无论是 .NET 控制台实用程序、Windows 应用程序还是 ASP.NET Web 项目)中从头开始创建DOCX或DOC文件是多么容易和高效。
Aspose.Words for .NET是高级Word文档处理API,用于执行各种文档管理和操作任务。该API支持生成,修改,转换,呈现和打印文档的能力,而无需在跨平台应用程序中直接利用Microsoft Word。你可以下载《Aspose.Words for .NET开发者指南》获取更多帮助。
在没有自动化的情况下在 C# 中创建 Word 文档的步骤
- 使用Aspose.Words for .NET NuGet 包
- 添加对Aspose.Words 命名空间的引用
- 使用 SetLicense 方法申请 Aspose 许可证
- 创建一个新的文档类实例
- 创建一个DocumentBuilder Class对象并在构造函数中传递 Document 对象
- 设置 DocumentBuilder 对象的必需属性
- 将输出的 Word 文档另存为文件或流
在没有自动化的情况下在 C# 中创建 Word 文档的代码
代码如下:
using System;//Add reference to Aspose.Words for .NET API//Use following namespaces to Word Document generator codeusing 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:asposelicenselicense.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# 代码示例中,我们创建了一个仅包含两行文本的简单 Word 文档,并将粗体属性应用于文本。但是,可以通过添加表格、单元格和Word 文档对象模型 (DOM) 的其他不同对象来执行更多操作并扩展上述代码。
要执行 Aspose.Words for .NET API 的上述 C# 代码,您不需要在您的计算机或将安装应用程序的服务器上安装 Microsoft Word。这使得创建 Word 文档变得更加容易和快捷。
如果您发现任何其他关于在没有互操作的情况下在 c# 中创建 word 文档的问题,请在Aspose.Words 免费支持论坛中提问哦 。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!