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)多行重复渲染
使用ExcelReport(NPOI)通过excel模板,多行重复渲染生成Excel文件中的数据表格
using ExcelReport;
using ExcelReport.Driver.NPOI;
using ExcelReport.Renderers;
using System;
namespace _3.多行重复渲染示例
{
internal class Program
{
private static void Main(string[] args)
{
// 项目启动时,添加
Configurator.Put(".xls", new WorkbookLoader());
try
{
ExportHelper.ExportToLocal(@"Template\Template.xls", "out.xls",
new SheetRenderer("多行重复渲染示例",
new RepeaterRenderer<StudentInfo>("rptStudentInfo", StudentLogic.GetList(),
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)
)
)
);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("finished!");
}
}
}
示例代码:多行重复渲染示例
源码地址:https://github.com/hanzhaoxin/ExcelReport