.NET Library for Creating Word Processing Documents

Open Source .NET API allows to Read, Write, Manipulate & Convert Microsoft® Word files.

What is NPOI?

NPOI is a .NET version of the POI Java Project. It is an open-source .NET library to read and write Microsoft® Office file formats. NPOI.XWPF namespace allows you to manipulate the DOCX file format.

NPOI allows you to add text & paragraphs, insert hyperlinks, create & parse tables, insert images, and by using XWPFWordExtractor class you can also extract text from existing Word Processing Documents.

Previous Next

Getting Started with NPOI

Once you have met the prerequisites, you can install in using NuGet

Install NPOI from NuGet

 Install-Package NPOI -Version 2.4.1

Manipulate DOCX file using C#

NPOI 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 an existing file and append changes like text, paragraphs, tables, and more.

Create DOCX using NPOI - C#

XWPFDocument doc = new XWPFDocument();
doc.CreateParagraph();
using (FileStream sw = File.Create("fileformat.docx"))
{
        doc.Write(sw);
}
                        

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. The following simple lines of code can add Table in Word document in C#.

  1. Create a new DOCX document using XWPFDocument
  2. Add table in the document by using doc.CreateTable() method and set rows and column numbers as int
  3. Get first row and first cell by using table.GetRow(1).GetCell(1) and add text to it using setText() method
  4. Save the file by using FileStream() method and set output file name and creation file mode

Create Table in DOCX using NPOI - C#

XWPFDocument doc = new XWPFDocument();
                        
XWPFTable table = doc.CreateTable(3, 3);

table.GetRow(0).GetCell(0).SetText("File Format Developer Guide");

FileStream out1 = new FileStream("table.docx", FileMode.Create);
doc.Write(out1);
out1.Close();