Use Cases
Openize.OpenXML-SDK for .NET is designed to fit into a wide range of business and development scenarios. Below are common use cases that demonstrate how you can use the SDK in real-world projects.
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." });
// Add a paragraph to the document.
body.AppendChild(para1);
// 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.
- Imports the necessary library
Openize.Cells
. - An instance of the
Workbook
class is initialized. - The
Save
method is invoked to save the Excel spreadsheet.
Create an Empty Workbook / Spreadsheet in C#
using System;
using Openize.Cells;
namespace Example
{
class Program
{
static void Main(string[] args)
{
var workbook = new Openize.Cells.Workbook();
workbook.Save("Z:\\Downloads\\Spreadsheet.xlsx");
Console.WriteLine("Excel spreadsheet created successfully.");
}
}
}
Insert Rows into an Excel Worksheet at a Specified Index
This C# example showcases how to programmatically insert rows into an Excel worksheet at a specified row index using Openize.Cells.
Insert Rows into Excel Worksheet
using System;
using Openize.Cells;
class Program
{
static void Main(string[] args)
{
string filePath = "Z:\\Downloads\\test_spreadsheet.xlsx";
using (var wb = new Openize.Cells.Workbook(filePath))
{
var firstSheet = wb.Worksheets[0];
uint startRowIndex = 5;
uint numberOfRows = 3;
firstSheet.InsertRows(startRowIndex, numberOfRows);
int rowsCount = firstSheet.GetRowCount();
Console.WriteLine("Rows Count=" + rowsCount);
wb.Save(filePath);
Console.WriteLine("Rows inserted and workbook saved successfully.");
}
}
}
Creating a 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
// ...
// Save the PowerPoint file onto the disk.
presentation.Save();
Insert Text into Presentation Programmatically
The following code snippet inserts text into a presentation programmatically.
Insert 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();