JXLS cho C#/.NET: Xuất báo cáo Excel từ template giống hệt Java JXLS với Jxls.Npoi
Nếu bạn từng làm Java và quen với JXLS (thiết kế sẵn file .xlsx template với comment jx:area, jx:each, công thức SUM(...) rồi đổ data vào), khi chuyển sang C#/.NET hẳn bạn sẽ thấy thiếu một bản port tương đương.
Mình vừa port toàn bộ JXLS 3.x từ Java sang C#/.NET với tên Jxls.Net (Jxls.Core + Jxls.Npoi), chạy trên NPOI, hỗ trợ .NET Standard 2.0 và .NET 8+, mã nguồn mở Apache-2.0.
1. Cài đặt
dotnet add package Jxls.Npoi
2. Thiết kế Excel template (template.xlsx)
Giữ nguyên cú pháp chuẩn của Java JXLS 3.x bằng Excel Cell Comments:
- Ô
A1thêm comment:jx:area(lastCell="C4") - Ô
A3thêm comment:jx:each(items="employees" var="e" lastCell="C3") - Hàng 3:
A3 = ${e.Name},B3 = ${e.Department},C3 = ${e.Salary} - Hàng 4:
C4 = =SUM(C3)(tự động giãn thành=SUM(C3:C100)theo số dòng dữ liệu)
3. Code C# đổ dữ liệu
using Jxls.Npoi.Builder;
var data = new Dictionary<string, object?>
{
["employees"] = new[]
{
new { Name = "Nguyen Van A", Department = "Engineering", Salary = 2500m },
new { Name = "Tran Thi B", Department = "Product", Salary = 2200m }
}
};
JxlsNpoiTemplateFillerBuilder.NewInstance()
.WithTemplate("template.xlsx")
.Build()
.Fill(data, new Jxls.Core.Builder.JxlsOutputFile("report.xlsx"));
Tính năng đã hỗ trợ
- Đầy đủ lệnh chuẩn JXLS:
jx:area,jx:each(select,orderBy,groupBy,direction="RIGHT",multisheet),jx:if,jx:grid,jx:image,jx:mergeCells,jx:autoRowHeight,jx:updateCell. - Tự động mở rộng công thức Excel (
SUM,AVERAGE,COUNT...) theo vùng dữ liệu sinh ra. - Hỗ trợ cả
.xlsx,.xlsvà streaming dữ liệu lớn quaSXSSF. - Đã có sẵn docs trên Context7 (
/tadatrolai/jxls-net) để các AI coding agent đọc trực tiếp.
Rất mong nhận được góp ý và một star trên GitHub nếu thư viện hữu ích cho dự án .NET của bạn!
All rights reserved