Getting Started
This guide walks you through installing Openize.OpenXML-SDK and creating your first .NET application to work with Word, Excel, or PowerPoint documents.
Installation
Openize.OpenXML-SDK is available as a NuGet package. To install it, run the following command in your terminal or Package Manager Console:
Install-Package FileFormat.Slides
Or, using the NuGet Package Manager in Visual Studio:
- Right-click on your project
- Select Manage NuGet Packages
- Search for Openize.OpenXML-SDK and install the latest version
Requirements
- .NET 6.0 or later (supports both .NET Core and .NET Framework)
- Compatible with Windows, Linux, and macOS environments
Basic Usage Example
Here’s a simple example of how to open and edit a Word document:
// Load the Word Document
var doc = new FileFormat.Words.Document("filename.docx");
var body = new FileFormat.Words.Body(doc);
foreach (var paragraph in body.Paragraphs)
{
foreach (var run in paragraph.Runs)
{
// Prepend 'Modified Heading :' for styled paragraphs
// and 'Modified Run :' for each run within normal paragraphs, preserving the existing format
run.Text = paragraph.Style.Contains("Heading") ? $"Modified Heading: {run.Text}" : $"Modified Run : {run.Text}";
}
// Update the paragraph in the document
doc.Update(paragraph);
}
// Save the modified Word Document
doc.Save($"{documentDirectory}/{filenameModified}");
Console.WriteLine($"Word Document Modified and Saved");
Working with Excel or PowerPoint is just as easy:
using (Workbook wb = new Workbook(filePath)) // Load existing spreadsheet/workbook file.
{
Worksheet firstSheet = wb.Worksheets[0]; // Load first worksheet within workbook.
Cell cellA1 = firstSheet.Cells["A1"]; // Get A1 cell object within cellA1 variable.
Console.WriteLine(cellA1.GetDataType()); // Output cellA1 data type.
string value = cellA1.GetValue(); // Get value within cell A1.
Console.WriteLine(value); // Output the value stored in cell A1
}