Convert Figma logo to code with AI

dotnet logoOpen-XML-SDK

Open XML SDK by Microsoft

3,989
544
3,989
156

Top Related Projects

Open XML SDK by Microsoft

2,090

JAXB-based Java library for Word docx, Powerpoint pptx, and Excel xlsx files

7,241

A pure PHP library for reading and writing word processing documents

1,915

Mirror of Apache POI

1,763

Fast and easy to use .NET library that creates or modifies Microsoft Word files without installing Word.

Quick Overview

The Open XML SDK is a .NET library that provides tools for working with Open XML documents, including Word, Excel, and PowerPoint files. It allows developers to create, read, and modify Office documents programmatically without requiring Microsoft Office to be installed.

Pros

  • Provides a high-level, object-oriented API for working with Office documents
  • Supports both reading and writing of Open XML files
  • Compatible with .NET Framework and .NET Core
  • Actively maintained and supported by Microsoft

Cons

  • Learning curve can be steep for developers unfamiliar with the Open XML format
  • Performance can be slower compared to native Office applications for complex operations
  • Limited support for some advanced Office features

Code Examples

  1. Creating a new Word document:
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

using (WordprocessingDocument doc = WordprocessingDocument.Create("example.docx", WordprocessingDocumentType.Document))
{
    MainDocumentPart mainPart = doc.AddMainDocumentPart();
    mainPart.Document = new Document(new Body(new Paragraph(new Run(new Text("Hello, World!")))));
}
  1. Reading text from an existing Word document:
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

using (WordprocessingDocument doc = WordprocessingDocument.Open("example.docx", false))
{
    Body body = doc.MainDocumentPart.Document.Body;
    string text = body.InnerText;
    Console.WriteLine(text);
}
  1. Adding a new worksheet to an Excel workbook:
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

using (SpreadsheetDocument doc = SpreadsheetDocument.Open("example.xlsx", true))
{
    WorkbookPart workbookPart = doc.WorkbookPart;
    WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
    worksheetPart.Worksheet = new Worksheet(new SheetData());

    Sheets sheets = workbookPart.Workbook.GetFirstChild<Sheets>();
    string relationshipId = workbookPart.GetIdOfPart(worksheetPart);
    uint sheetId = 1;
    if (sheets.Elements<Sheet>().Any())
    {
        sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
    }
    Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = "New Sheet" };
    sheets.Append(sheet);
}

Getting Started

  1. Install the NuGet package:
dotnet add package DocumentFormat.OpenXml
  1. Add the necessary using statements:
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing; // For Word documents
using DocumentFormat.OpenXml.Spreadsheet; // For Excel documents
using DocumentFormat.OpenXml.Presentation; // For PowerPoint documents
  1. Start working with Open XML documents using the examples provided above or refer to the official documentation for more detailed guides and API references.

Competitor Comparisons

Open XML SDK by Microsoft

Pros of Open-XML-SDK

  • Widely adopted and well-established library for working with Open XML documents
  • Extensive documentation and community support
  • Comprehensive API covering various aspects of Open XML manipulation

Cons of Open-XML-SDK

  • Larger codebase and potentially steeper learning curve
  • May include features not needed for simpler use cases
  • Slower release cycle for updates and new features

Code Comparison

Open-XML-SDK:

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

using (WordprocessingDocument doc = WordprocessingDocument.Create("example.docx", WordprocessingDocumentType.Document))
{
    MainDocumentPart mainPart = doc.AddMainDocumentPart();
    mainPart.Document = new Document(new Body(new Paragraph(new Run(new Text("Hello, World!")))));
}

Both repositories are actually the same project, with Open-XML-SDK being the official .NET library for working with Open XML documents. The comparison above highlights some general characteristics of the library. As there is no alternative repository to compare against, the code example demonstrates a basic usage scenario of creating a simple Word document using the Open-XML-SDK.

2,090

JAXB-based Java library for Word docx, Powerpoint pptx, and Excel xlsx files

Pros of docx4j

  • Written in Java, offering platform independence and integration with Java ecosystems
  • Supports a wider range of document formats, including DOCX, PPTX, XLSX, and PDF
  • More comprehensive documentation and examples available

Cons of docx4j

  • Generally slower performance compared to Open-XML-SDK
  • Less active community and fewer contributors
  • Steeper learning curve due to more complex API

Code Comparison

Open-XML-SDK (C#):

using (WordprocessingDocument doc = WordprocessingDocument.Create(path, WordprocessingDocumentType.Document))
{
    MainDocumentPart mainPart = doc.AddMainDocumentPart();
    mainPart.Document = new Document(new Body(new Paragraph(new Run(new Text("Hello, World!")))));
}

docx4j (Java):

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
mainDocumentPart.addParagraphOfText("Hello, World!");
wordMLPackage.save(new File("HelloWorld.docx"));

Both libraries provide similar functionality for creating and manipulating Office Open XML documents. Open-XML-SDK is more focused on Microsoft Office formats and offers better performance, while docx4j provides broader format support and platform independence. The choice between them often depends on the specific project requirements and the development environment.

7,241

A pure PHP library for reading and writing word processing documents

Pros of PHPWord

  • Written in PHP, making it more accessible for PHP developers
  • Simpler API for basic document creation and manipulation
  • Lighter weight and easier to integrate into PHP projects

Cons of PHPWord

  • Less comprehensive feature set compared to Open-XML-SDK
  • May not handle complex document structures as efficiently
  • Limited support for advanced Office Open XML features

Code Comparison

PHPWord:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello World!');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

Open-XML-SDK:

using (WordprocessingDocument doc = WordprocessingDocument.Create("test.docx", WordprocessingDocumentType.Document))
{
    MainDocumentPart mainPart = doc.AddMainDocumentPart();
    mainPart.Document = new Document(new Body(new Paragraph(new Run(new Text("Hello World!")))));
}

Both libraries provide ways to create and manipulate Word documents, but Open-XML-SDK offers more granular control over document structure and elements. PHPWord's API is more straightforward for simple tasks, while Open-XML-SDK provides a more comprehensive approach to working with Office Open XML formats.

1,915

Mirror of Apache POI

Pros of POI

  • Cross-platform compatibility (Java-based)
  • Supports a wider range of file formats (Excel, Word, PowerPoint, Visio, etc.)
  • Larger and more active community with frequent updates

Cons of POI

  • Generally slower performance compared to Open XML SDK
  • Higher memory consumption, especially for large files
  • Steeper learning curve due to more complex API

Code Comparison

Open XML SDK (C#):

using (SpreadsheetDocument spreadsheet = SpreadsheetDocument.Open(filePath, false))
{
    WorkbookPart workbookPart = spreadsheet.WorkbookPart;
    WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
    SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
}

POI (Java):

try (FileInputStream fis = new FileInputStream(filePath);
     XSSFWorkbook workbook = new XSSFWorkbook(fis)) {
    XSSFSheet sheet = workbook.getSheetAt(0);
    Iterator<Row> rowIterator = sheet.iterator();
}

Both libraries provide functionality for working with Office documents, but Open XML SDK is specifically designed for .NET environments and offers better performance for large files. POI, on the other hand, provides broader file format support and cross-platform compatibility at the cost of increased complexity and resource usage.

1,763

Fast and easy to use .NET library that creates or modifies Microsoft Word files without installing Word.

Pros of DocX

  • Simpler API, easier to use for basic document operations
  • Faster performance for certain tasks
  • Smaller library size, lighter dependency

Cons of DocX

  • Limited support for complex document features
  • Less comprehensive documentation
  • Smaller community and fewer updates

Code Comparison

Open-XML-SDK:

using (WordprocessingDocument doc = WordprocessingDocument.Create(path, WordprocessingDocumentType.Document))
{
    MainDocumentPart mainPart = doc.AddMainDocumentPart();
    mainPart.Document = new Document(new Body(new Paragraph(new Run(new Text("Hello, World!")))));
}

DocX:

using (var doc = DocX.Create(path))
{
    doc.InsertParagraph("Hello, World!");
    doc.Save();
}

Both Open-XML-SDK and DocX are libraries for working with Microsoft Word documents in .NET. Open-XML-SDK is the official Microsoft library, offering comprehensive support for all Office Open XML features. It provides fine-grained control over document structure but has a steeper learning curve.

DocX, on the other hand, offers a more user-friendly API for common document operations. It's easier to get started with and performs well for basic tasks. However, it may not support all advanced features found in complex Word documents.

Choose Open-XML-SDK for full Office Open XML compliance and advanced document manipulation. Opt for DocX if you need a simpler API for basic document creation and editing tasks.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Open XML SDK

[!NOTE]

v3.0.0 refactors and addresses some technical debt while retaining source compatibility as much as possible. You should be able to update your package and recompile with limited changes. However, binary compatibility was not a goal and will break that for some changes which are documented. PRs that introduced such changes are marked with a breaking-change label and were added to a list to help migrating to v3.0.0.

Please see the v3.0.0 milestone for issues and PRs that are included. For discussions, please join us at this issue.

[!IMPORTANT] The CI feed URL has changed as of 2 April, 2024. Please update to the new URL if using CI builds.

Downloads Build Status Backend Status

The Open XML SDK provides tools for working with Office Word, Excel, and PowerPoint documents. It supports scenarios such as:

  • High-performance generation of word-processing documents, spreadsheets, and presentations.
  • Document modification, such as adding, updating, and removing content and metadata.
  • Search and replace content using regular expressions.
  • Splitting up (shredding) a file into multiple files, and combining multiple files into a single file.
  • Updating cached data and embedded spreadsheets for charts in Word/PowerPoint.

Table of Contents

Packages

The official release NuGet packages for Open XML SDK are on NuGet.org:

PackageDownloadPrerelease
DocumentFormat.OpenXml.FrameworkNuGetNuGet
DocumentFormat.OpenXmlNuGetNuGet
DocumentFormat.OpenXml.LinqNuGetNuGet
DocumentFormat.OpenXml.FeaturesNuGetNuGet

Daily Builds

The NuGet package for the latest builds of the Open XML SDK is available as a custom feed on an Azure blob. Stable releases here will be mirrored onto NuGet and will be identical. You must set up a NuGet.config file that looks similar to this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="OpenXmlCI" value="https://ooxml.blob.core.windows.net/feed/index.json" />
  </packageSources>
</configuration>

For latests changes, please see the changelog

Framework Support

The package currently supports the following targets:

  • .NET Framework 3.5, 4.0, 4.6
  • .NET Standard 2.0
  • .NET 6.0

For details on platform support, including other runtimes such as Mono and Unity, please see the docs at https://docs.microsoft.com/en-us/dotnet/standard/net-standard.

Known Issues

  • On .NET Core and .NET 5 and following, ZIP packages do not have a way to stream data. Thus, the working set can explode in certain situations. This is a known issue.
  • On .NET Framework, an IsolatedStorageException may be thrown under certain circumstances. This generally occurs when manipulating a large document in an environment with an AppDomain that does not have enough evidence. A sample with a workaround is available here.

Documentation

Please see Open XML SDK for the official documentation.

If you have how-to questions

Related tools

  • Open XML SDK 2.5 Productivity Tool: The Productivity Tool provides viewing and code generation compatible with the Open XML SDK 2.5.
  • Open XML Powertools: This provides example code and guidance for implementing a wide range of Open XML scenarios.
  • ClosedXml: This library provides a simplified object model on top of the OpenXml SDK for manipulating and creating Excel documents.
  • OfficeIMO: This library provides a simplified object model on top of the OpenXml SDK manipulating and creating Word documents.
  • OpenXML-Office: This nuget library provides a simplified object model on top of the OpenXml SDK manipulating and creating PPT and Excel documents.
  • Serialize.OpenXml.CodeGen: This is a tool that converts an OpenXml document into the .NET code required to create it.
  • Html2OpenXml: This is a tool that takes HTML and converts it to an OpenXml document.
  • DocxToSource: This is a tool designed to be a replacement for the old OpenXML SDK Productivity Tool.
  • OOXML Viewer: This is an extension for Visual Studio Code to View and Edit the xml parts of an Office Open XML file and to view a diff with the previous version of an OOXML part when saved from an outside program. Search "OOXML" in the VS Code extensions tab or download it from the VS Code Marketplace
  • ShapeCrawler: This library provides a simplified object model on top of the OpenXml SDK to manipulate PowerPoint documents.
  • OOXML Validator: VS Code extension to validate Office Open XML files. Search "OOXML" in the VS Code extensions tab or download it from the VS Code Marketplace

How can I contribute?

We welcome contributions! Many people all over the world have helped make this project better.

  • Contributing explains what kinds of contributions we welcome

Reporting security issues and security bugs

Security issues and bugs should be reported privately, via email, to the Microsoft Security Response Center (MSRC) secure@microsoft.com. You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Further information, including the MSRC PGP key, can be found in the Security TechCenter.

.NET Foundation

The Open XML SDK is a .NET Foundation project.

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information, see the .NET Foundation Code of Conduct.

License

The Open XML SDK is licensed under the MIT license.