Openize.OpenXML-SDK for .NET
Manipulate Office Documents in C# .NET Apps
Create, load, and modify various contents of Microsoft Office documents, including Word, Excel, and PowerPoint files, with just a few lines of code.
Openize.OpenXML-SDK for .NET is a user-friendly and accessible Open-Source .NET SDK designed for effortless creation and customization of Microsoft Office documents. This intuitive C# library allows for the generation and manipulation of Word, Excel, and PowerPoint documents with minimal lines of code.
The installation of this lightweight solution proceeds seamlessly, offering an array of features to meet various document needs. Openize.OpenXML-SDK harnesses the power of the OpenXML SDK, a technology endorsed by Microsoft. Serving as a convenient wrapper, Openize.OpenXML-SDK simplifies the utilization of advanced capabilities.
Tailored with developers in mind, this Open-Source .NET library provides the means to expand its functionalities by utilizing the OpenXML SDK library. Handling Openize.OpenXML-SDK for .NET is straightforward, owing to its user-friendly design. The library showcases an assortment of intelligent features, encompassing tasks such as adding new paragraphs, implementing text formatting, inserting and resizing images, extracting images, modifying document properties, and much more.
Explore our GitHub repository to contribute, suggest improvements, and enhance this Open Source SDK: https://github.com/openize-com/openize-open-xml-sdk-net
Getting Started with Openize.OpenXML-SDK for .NET
The recommend way to install Openize.OpenXML-SDK for .NET is using NuGet. Please use the following command for a smooth installation.
Install Openize.OpenXML-SDK for .NET via NuGet
NuGet\Install-Package Openize.OpenXML-SDK
You can also download it directly from github.Creating a Word Document Programmatically
The following code snippet creates an empty Word document programmatically.
Create a word document via .NET SDK
// 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");
Add some text to a Word document
The following code snippet adds some text to a document programmatically.
Create a word document paragraph via .NET SDK
// 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");
}
Creating an Empty Excel Spreadsheet/Workbook Programmatically
The following code snippet exemplifies how to create and save a new Microsoft Excel spreadsheet using C# with the FileFormat.Cells library.
- The code begins by importing the necessary library FileFormat.Cells, providing access to functionalities for Excel spreadsheet manipulation.
- Within the Example namespace, the Program class is defined.
- The Main method serves as the entry point of the program, accepting command-line arguments if any (string[] args).
- An instance of the Workbook class is initialized using Workbook workbook = new Workbook();.
- The Save method is invoked on the workbook instance to save the Excel spreadsheet. The file is saved with the name "Spreadsheet.xlsx" in the root directory `("/") of the disk.
Copy and paste the code snippet below into your main file and execute the program.
Create an Empty Workbook / Spreadsheet in C#
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.");
}
}
}
Programmatically insert rows into an Excel worksheet at a specified row index
This C# example showcases how to programmatically insert rows into an Excel worksheet at a specified row index using the Openize.Cells library.
Copy and paste the code snippet below into your main file and execute the program.
Create an Empty Workbook / Spreadsheet in C#
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.");
}
}
}
Creating an PowerPoint presentation Programmatically
The following code snippet creates an empty PowerPoint presentation programmatically.
Create a PowerPoint presentation via .NET API
// 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();
Insert Text into Presentation Programmatically
The following code snippet inserts a text into presentation programmatically.
Insert a Text into Presentation via .NET API
// 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();
More Code Examples and Resources
Explore more detailed code examples at Openize Gists.