.NET Library for Creating Spreadsheet Documents

Read, Write, Manipulate & Convert Spreadsheet files via .NET API.

 

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.HSSF namespace allows you to manipulate XLS file format, while NPOI.XSSF namespace allows you to create & modify XLSX file format.

NPOI allows you to add text & paragraphs, insert hyperlinks, create & style cells & columns, insert images and read content from existing Spreadsheet files.

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 XLSX file using C#

NPOI allows .NET programmers to create as well as modify spreadsheet 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.

Manipulate XLSX using NPOI - C#

IWorkbook wb = new XSSFWorkbook();

// Create a Worksheet
ISheet ws = wb.CreateSheet("FileFormat");

ICellStyle style = wb.CreateCellStyle();

//Setting the line of the top border
style.BorderTop = BorderStyle.Thick;
style.TopBorderColor = 256;

style.BorderLeft = BorderStyle.Thick;
style.LeftBorderColor = 256;

style.BorderRight = BorderStyle.Thick;
style.RightBorderColor = 256;

style.BorderBottom = BorderStyle.Thick;
style.BottomBorderColor = 256;

IRow row = ws.CreateRow(0);
ICell cell = row.CreateCell(1);
cell.CellStyle = style;

FileStream sw = File.Create("fileformat.xlsx");
wb.Write(sw);
sw.Close();

Add Image in XLSX using C#

The API allows the developers to add images in spreadsheet documents. You can add a image and set image properties. The API allow various methods to manipulate in images in XLSX fileformat easily. IClientAnchor allows you set top, bottom, left and right positioning of image inside the worksheet.

Create Table in DOCX using NPOI - C#

IWorkbook wb = new XSSFWorkbook();
ISheet sheet1 = wb.CreateSheet("First Sheet");

// Add picture data to this workbook.
byte[] bytes = File.ReadAllBytes("fileformat.png");
int pictureIdx = wb.AddPicture(bytes, PictureType.PNG);

ICreationHelper helper = wb.GetCreationHelper();

// Create the drawing patriarch.  This is the top level container for all shapes.
IDrawing drawing = sheet1.CreateDrawingPatriarch();

// add a picture shape
IClientAnchor anchor = helper.CreateClientAnchor();

// set top-left corner of the picture,
// subsequent call of Picture#resize() will operate relative to it
anchor.Col1 = 3;
anchor.Row1 = 2;
IPicture pict = drawing.CreatePicture(anchor, pictureIdx);
// auto-size picture relative to its top-left corner
pict.Resize();

FileStream sw = File.Create("image.xlsx");
wb.Write(sw);
sw.Close();