1、安装引用ExcelReport(NPOI)
1)使用Nuget界面管理器
搜索"ExcelReport"
和“ExcelReport.Driver.NPOI”,
在列表中分别找到它,点击"安装"
相关文档:VS(Visual Studio)中Nuget的使用
2)使用Package Manager命令安装
PM> Install-Package ExcelReport -Version 3.1.6
PM> Install-Package ExcelReport.Driver.NPOI -Version 2.0.3
3)使用.NET CLI命令安装
> dotnet add package ExcelReport --version 3.1.6
> dotnet add package ExcelReport.Driver.NPOI --version 2.0.3
2、使用ExcelReport(NPOI)生成CSV报表
using ExcelReport;
using ExcelReport.Driver.CSV;
using ExcelReport.Renderers;
using System;
namespace _4.CSV示例
{
internal class Program
{
private static void Main(string[] args)
{
// 项目启动时,添加
Configurator.Put(".csv", new WorkbookLoader());
try
{
var num = 1;
ExportHelper.ExportToLocal(@"Template\test.csv", "out.csv",
new SheetRenderer("test",
new RepeaterRenderer<StudentInfo>("Roster", StudentLogic.GetList(),
new ParameterRenderer<StudentInfo>("No", t => num++),
new ParameterRenderer<StudentInfo>("Name", t => t.Name),
new ParameterRenderer<StudentInfo>("Gender", t => t.Gender ? "男" : "女"),
new ParameterRenderer<StudentInfo>("Class", t => t.Class),
new ParameterRenderer<StudentInfo>("RecordNo", t => t.RecordNo),
new ParameterRenderer<StudentInfo>("Phone", t => t.Phone),
new ParameterRenderer<StudentInfo>("Email", t => t.Email)
),
new ParameterRenderer("Author", "hzx")
)
);
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine("finished!");
Console.ReadKey();
}
}
}
示例代码:CSV报表示例
源码地址:https://github.com/hanzhaoxin/ExcelReport