.NET Library for Creating Word Processing Documents

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

 

NPOI is a .NET version of POI Java Project. It is an open source .NET library to read and write Microsoft Office file formats formats. NPOI.XWPF namespace allows you to manipulate 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 manually download the repository from GitHub or install from 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 and 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.

Create Table in DOCX using NPOI - C#

XWPFDocument doc = new XWPFDocument();
XWPFParagraph para = doc.CreateParagraph();
XWPFRun r0 = para.CreateRun();
r0.SetText("FileFormat");
para.FillBackgroundColor = "EEEEEE";
para.FillPattern = NPOI.OpenXmlFormats.Wordprocessing.ST_Shd.diagStripe;

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

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

XWPFTableCell c1 = table.GetRow(0).GetCell(0);
XWPFParagraph p1 = c1.AddParagraph();
XWPFRun r1 = p1.CreateRun();
r1.SetText("NPOI - DOCX");
r1.IsBold = true;

r1.FontFamily = "Courier";
r1.SetUnderline(UnderlinePatterns.DotDotDash);
r1.SetTextPosition(100);
c1.SetColor("FF0000");

table.GetRow(2).GetCell(2).SetText("only text");

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