NPOI C# Excel Export: Complete Guide

You can export to Excel using NPOI by following these steps:

  1. Firstly, you need to import the NPOI library. You can search for NPOI in the NuGet package manager and install it.
  2. Create an Excel document and input data. Here is an example code:
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;

// 创建一个Excel文档
IWorkbook workbook = new XSSFWorkbook();

// 创建一个工作表
ISheet sheet = workbook.CreateSheet("Sheet1");

// 添加数据到工作表
IRow row = sheet.CreateRow(0);
row.CreateCell(0).SetCellValue("Name");
row.CreateCell(1).SetCellValue("Age");

row = sheet.CreateRow(1);
row.CreateCell(0).SetCellValue("Alice");
row.CreateCell(1).SetCellValue(25);

// 保存Excel文档
using (FileStream file = new FileStream("data.xlsx", FileMode.Create, FileAccess.Write))
{
    workbook.Write(file);
}
  1. After running the program, an Excel file named “data.xlsx” will be generated with added data.

This way, you can export Excel using NPOI. You can manipulate the data according to your needs and add it to the worksheet.

bannerAds