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)简单的参数替换渲染示例代码
using ExcelReport;
using ExcelReport.Driver.NPOI;
using ExcelReport.Renderers;
using System;
using System.Drawing;
namespace _1.参数渲染器示例
{
internal class Program
{
private static void Main(string[] args)
{
try
{
// 项目启动时,添加
Configurator.Put(".xls", new WorkbookLoader());
ExportHelper.ExportToLocal(@"Template\Template.xls", "out.xls",
new SheetRenderer("参数渲染示例",
new ParameterRenderer("String", "Hello World!"),
new ParameterRenderer("Boolean", true),
new ParameterRenderer("DateTime", DateTime.Now),
new ParameterRenderer("Double", 3.14),
new ParameterRenderer("Image", Image.FromFile("Image/C#高级编程.jpg"))
)
);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("finished!");
}
}
}
示例代码:参数渲染示例
源码地址:https://github.com/hanzhaoxin/ExcelReport
3、ExcelReport(NPOI)单行重复渲染
使用ExcelReport(NPOI)单行重复渲染生成Excel文件中的数据表格
using ExcelReport;
using ExcelReport.Driver.NPOI;
using ExcelReport.Renderers;
using System;
namespace _2.单行重复渲染示例
{
internal class Program
{
private static void Main(string[] args)
{
// 项目启动时,添加
Configurator.Put(".xls", new WorkbookLoader());
try
{
var num = 1;
ExportHelper.ExportToLocal(@"Template\Template.xls", "out.xls",
new SheetRenderer("学生名册",
new RepeaterRenderer<StudentInfo>("Roster", StudentLogic.GetList(),
new ParameterRenderer<StudentInfo>("No1", 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();
}
}
}
示例代码:单行重复渲染示例