Openize.OpenXML-SDK for .NET
在 C# .NET 应用程序中处理 Office 文档
使用少量代码即可创建、加载和修改 Microsoft Office 文档,包括 Word、Excel 和 PowerPoint 文件。
Openize.OpenXML-SDK for .NET 是一个易于使用的开源 .NET SDK,旨在帮助开发者轻松创建和自定义 Microsoft Office 文档。这款直观的 C# 库允许开发者使用最少的代码行来生成和修改 Word、Excel 和 PowerPoint 文档。
该轻量级解决方案易于安装,并提供多种功能以满足不同的文档需求。Openize.OpenXML-SDK 利用 Microsoft 支持的 OpenXML SDK 技术,作为一个便捷的封装工具,使开发者能够轻松使用 OpenXML 的高级功能。
此开源 .NET 库专为开发者设计,可通过 OpenXML SDK 扩展功能。其用户友好的设计使得开发者能够轻松执行诸如添加新段落、文本格式化、插入和调整图片大小、提取图片、修改文档属性等智能操作。
欢迎访问我们的 GitHub 仓库,为这个开源 SDK 贡献代码、提供改进建议,并参与开发:https://github.com/openize-com/openize-open-xml-sdk-net
开始使用 Openize.OpenXML-SDK for .NET
推荐使用 NuGet 安装 Openize.OpenXML-SDK for .NET。请使用以下命令进行顺利安装。
通过 NuGet 安装 Openize.OpenXML-SDK for .NET
NuGet\Install-Package Openize.OpenXML-SDK
您也可以直接从 GitHub 下载。使用代码创建 Word 文档
以下代码示例展示了如何使用代码创建一个空的 Word 文档。
通过 .NET SDK 创建 Word 文档
// Create an instance of the Document class.
Document doc = new Document();
// Invoke the Save method to save the Word document onto the disk.
doc.Save("/Docs.docx");
向 Word 文档添加文本
以下代码示例展示了如何使用代码向 Word 文档添加文本。
使用 .NET SDK 创建 Word 文档段落
// Create an instance of the Document class.
using (Document doc = new Document())
{
//Initialize the constructor with the Document class object.
Body body = new Body(doc);
// Instantiate an instance of the Paragraph class.
Paragraph para1 = new Paragraph();
// Set the text of the paragraph.
para1.AddRun(new Run { Text = "This is a Paragraph." });
// Invoke AppendChild method of the body class to add a paragraph to the document.
body.AppendChild(para1);
// Call the Save method to save the Word document onto the disk.
doc.Save("/Docs.docx");
}
使用代码创建空的 Excel 电子表格/工作簿
以下代码示例展示了如何使用 C# 和 FileFormat.Cells 库创建并保存新的 Microsoft Excel 电子表格。
- 代码首先导入必要的 FileFormat.Cells 库,以提供 Excel 电子表格操作功能。
- 在 Example 命名空间中,定义 Program 类。
- Main 方法是程序的入口点,接收命令行参数(如果有)。
- 使用 `Workbook workbook = new Workbook();` 初始化 Workbook 类的实例。
- 调用 Save 方法保存 Excel 电子表格,文件默认保存在磁盘根目录(“/”)下,命名为 "Spreadsheet.xlsx"。
复制并粘贴以下代码到您的主文件中并运行程序。
使用 C# 创建空的 Excel 电子表格/工作簿
using System;
using Openize.Cells;
namespace Example
{
class Program
{
static void Main(string[] args)
{
// Initialize an instance of the Workbook class.
var workbook = new Openize.Cells.Workbook();
// Call the Save method to save the MS Excel Spreadsheet/Workbook onto the disk.
workbook.Save("Z:\\Downloads\\Spreadsheet.xlsx");
Console.WriteLine("Excel spreadsheet created successfully.");
}
}
}
在 Excel 工作表中插入行
此 C# 示例展示了如何使用 Openize.Cells 库,在指定的行索引处插入行。
复制并粘贴以下代码到您的主文件中并运行程序。
使用 C# 在 Excel 电子表格中插入行
using System;
using Openize.Cells;
class Program
{
static void Main(string[] args)
{
string filePath = "Z:\\Downloads\\test_spreadsheet.xlsx";
// Load the workbook from the specified file path
using (var wb = new Openize.Cells.Workbook(filePath))
{
// Access the first worksheet in the workbook
var firstSheet = wb.Worksheets[0];
// Define the starting row index and the number of rows to insert
uint startRowIndex = 5;
uint numberOfRows = 3;
// Insert the rows into the worksheet
firstSheet.InsertRows(startRowIndex, numberOfRows);
// Get the total row count after insertion
int rowsCount = firstSheet.GetRowCount();
// Output the updated row count to the console
Console.WriteLine("Rows Count=" + rowsCount);
// Save the workbook to reflect the changes made
wb.Save(filePath);
Console.WriteLine("Rows inserted and workbook saved successfully.");
}
}
}
使用代码创建 PowerPoint 演示文稿
以下代码示例展示了如何使用代码创建一个空的 PowerPoint 演示文稿。
通过 .NET API 创建 PowerPoint 演示文稿
// Create an object of the Presentation class.
Presentation presentation = Presentation.Create("presentation.pptx");
//Perform necessary operations.
//...
// Call the Save method to save the PowerPoint file onto the disk.
presentation.Save();
在 PowerPoint 演示文稿中插入文本
以下代码示例展示了如何使用代码在 PowerPoint 演示文稿中插入文本。
通过 .NET API 在 PowerPoint 演示文稿中插入文本
// Create a new PowerPoint presentation at the specified file path
Presentation presentation = Presentation.Create("D:\\AsposeSampleResults\\test2.pptx");
// Create a text shape for the title and set its properties
TextShape shape = new TextShape();
shape.Text = "Title: Here is my first title From FF";
shape.TextColor = "980078";
shape.FontFamily = "Baguet Script";
// Create the slide and add the text shape to it
Slide slide = new Slide();
slide.AddTextShapes(shape);
// Append the slide to the presentation
presentation.AppendSlide(slide);
// Save the modified presentation
presentation.Save();
更多代码示例和资源
访问 Openize Gists 以获取更多详细的代码示例。