C# 使用 NPOI 套件產生 Excel 檔案(xls, xlsx)

NPOI 是完全免費,並且不需要先安裝 Office 軟體就可以直接使用,要輸出 xls 和 xlsx 類型的檔案都沒有問題

安裝

先使用 NuGet 安裝 NPOI 套件,或是使用 .NET CLI 執行以下指令安裝
	
dotnet add package NPOI
    

建立檔案

Microsoft Office Excel 有兩種副檔名: xls 和 xlsx ,早期 Excel 2007 以前是使用 xls ,Excel 2007 (含)以後是使用 xlsx ,兩個檔案的建立方式只差副檔名和在使用哪個物件而已,兩個都是實作 IWorkbook 介面,所以替換非常方便。

建立 xls 檔案

    
using NPOI.SS.UserModel;
using NPOI.HSSF.UserModel;

IWorkbook workbook = new HSSFWorkbook(); // xls
var sheet = workbook.CreateSheet("工作表1");

string filePath = @"C:\Users\ruyut\Desktop\test.xls";
using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
    workbook.Write(fs);
}
    

建立 xlsx 檔案

    
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

IWorkbook workbook = new XSSFWorkbook(); // xlsx
var sheet = workbook.CreateSheet("工作表1");

string filePath = @"C:\Users\ruyut\Desktop\test.xlsx";
using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
    workbook.Write(fs);
}
    

由於兩者的使用方式非常相識,接下來都會使用 xlsx 做示範。

填入內容

    
IWorkbook workbook = new XSSFWorkbook(); // xlsx
var sheet = workbook.CreateSheet("工作表1");

// A1 填入 Hello World
sheet.CreateRow(0).CreateCell(0).SetCellValue("Hello World");


var row2 = sheet.CreateRow(2); // 建立第三 Row (橫向)
row2.CreateCell(0).SetCellValue(1); // A3 填入 1
row2.CreateCell(1).SetCellValue(2); // B3 填入 2
row2.CreateCell(2).SetCellValue(3); // C3 填入 3

row2.CreateCell(3).CellFormula = "SUM(A3:C3)"; // D3 設定公式 =SUM(A3:C3)
    

設定欄位格式和樣式

    
// 跨欄位合併 (A1 ~ C2)
sheet.AddMergedRegion(new CellRangeAddress(0, 1, 0, 2));

var  cellStyleA1 = workbook.CreateCellStyle(); // 建立儲存格樣式
cellStyleA1.Alignment = HorizontalAlignment.Center; // 水平置中
cellStyleA1.VerticalAlignment = VerticalAlignment.Center; // 垂直置中
sheet.GetRow(0).GetCell(0).CellStyle = cellStyleA1; // A1 套用樣式

// A3 設定填滿紅色
var cellStyleA3 = workbook.CreateCellStyle();
cellStyleA3.FillForegroundColor = IndexedColors.Red.Index; // 紅色
cellStyleA3.FillPattern = FillPattern.SolidForeground; // 填滿
row2.GetCell(0).CellStyle = cellStyleA3;


// B3 設定框線
var cellStyleB3 = workbook.CreateCellStyle();
cellStyleB3.BorderTop = BorderStyle.Thin;
cellStyleB3.BorderBottom = BorderStyle.Thin;
cellStyleB3.BorderLeft = BorderStyle.Thin;
cellStyleB3.BorderRight = BorderStyle.Thin;
row2.GetCell(1).CellStyle = cellStyleB3;
    

設定文字樣式

    
// C3 設定文字顏色為藍色
var cellStyleC3 = workbook.CreateCellStyle();
var fontC3 = workbook.CreateFont();
fontC3.Color = IndexedColors.Blue.Index;
cellStyleC3.SetFont(fontC3);
row2.GetCell(2).CellStyle = cellStyleC3;

// D3 設定文字為粗體 + 斜體 + 底線
var cellStyleD3 = workbook.CreateCellStyle();
var fontD3 = workbook.CreateFont();
fontD3.IsBold = true; // 粗體
fontD3.IsItalic = true; // 斜體
fontD3.Underline = FontUnderlineType.Single; // 底線
cellStyleD3.SetFont(fontD3);
row2.GetCell(3).CellStyle = cellStyleD3;


// A4 設定文字: 中文,字體 微軟正黑體,字體大小 16
var cellStyleA4 = workbook.CreateCellStyle();
var fontA4 = workbook.CreateFont();
fontA4.FontName = "微軟正黑體";
fontA4.FontHeightInPoints = 16;
cellStyleA4.SetFont(fontA4);
sheet.CreateRow(3).CreateCell(0).SetCellValue("中文");
sheet.GetRow(3).GetCell(0).CellStyle = cellStyleA4;
    

隱藏欄和列

    
// E,F 欄 (直向) 隱藏
sheet.SetColumnHidden(4, true);
sheet.SetColumnHidden(5, true);

// 5, 6 列 (橫向) 隱藏
sheet.CreateRow(4).CreateCell(0).SetCellValue("A5");
sheet.CreateRow(5).CreateCell(0).SetCellValue("A6");
sheet.GetRow(4).ZeroHeight = true;
sheet.GetRow(5).ZeroHeight = true;
    

範例輸出




參考資料:
GitHub - nissl-lab/npoi
GitHub - nissl-lab/npoi-examples

留言