.NET Library for Creating Word Processing Documents

Read, Write, Manipulate & Convert Word files, Create Tables in DOCX via Open Source .NET API.

 

Open XML SDK is an open source API, developed by Microsoft and distributed under Microsoft open source code of conduct to manipulate Word Processing Documents including DOCXDOTXDOCMDOTM file formats

Using the API, you can add text, header, footer, endnotes, footnotes, styles, themes, and more. It allows you to generate high-performance word documents and extract data from them. The API support various .NET platforms including, .NET 3.5, .NET 4.0, .NET 4.6, and .NET Standard 1.3.

Previous Next

Getting Started with Open XML SDK

First of all, you require to have .NET Framework 3.5 or above. After that, please download the repository manually from GitHub or install it from NuGet.

Installation  Open XML SDK from NuGet

 Install-Package DocumentFormat.OpenXml

Manipulate DOCX file using C#

Open XML SDK allows .NET programmers to create as well as modify word processing from their own .NET applications. In order to modify an existing file, you can open and existing file and append changes like text, paragraphs, tables and more.

Add Paragraph in DOCX - C#

// Open an existing word processing document
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open("fileformat.docx", true))
{
    Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
    // Add paragraph
    Paragraph para = body.AppendChild(new Paragraph());
    Run run = para.AppendChild(new Run());
    run.AppendChild(new Text("File Format Developer Guide"));
}

Create a Table in DOCX using C#

.The API allows the developers to add a table in Word Processing documents. You can add a table, set table properties, set table grid and column grid properties. Furthermore, you can manage table cells and rows using TableCell and TableRow classes respectively.

Read Excel Data - C#

// Open an existing word processing document
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open("fileformat.docx", true))
{
    Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
    // Create a table.
    Table table = new Table();
    // Set the style and width for the table.
    TableProperties tableProperties = new TableProperties();
    TableStyle tableStyle = new TableStyle() { Val = "TableGrid" };
    // Make the table width 100% of the page width.
    TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct };
    // Apply
    tableProperties.Append(tableStyle, tableWidth);
    table.AppendChild(tableProperties);
    // Add columns
    TableGrid tableGrid = new TableGrid(new GridColumn(), new GridColumn(), new GridColumn());
    table.AppendChild(tableGrid);
    // Create 1 row to the table.
    TableRow tableRow = new TableRow();
    // Add a cell to each column in the row.
    TableCell tableCell = new TableCell(new Paragraph(new Run(new Text("Column 1"))));
    TableCell tableCell1 = new TableCell(new Paragraph(new Run(new Text("Column 2"))));
    //Append data
    tableRow.Append(tableCell, tableCell1);
    // Add row to the table.
    table.AppendChild(tableRow);
    // Add the table to the document
    body.AppendChild(table);
}