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!"); } }}
示例代码:ExcelReport/tree/master/examples/1.%E5%8F%82%E6%95%B0%E6%B8%B2%E6%9F%93%E5%99%A8%E7%A4%BA%E4%BE%8B" rel="nofollow">参数渲染示例
源码地址:ExcelReport" rel="nofollow">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("Roster", StudentLogic.GetList(), new ParameterRenderer ("No1", t => num++), new ParameterRenderer ("Name", t => t.Name), new ParameterRenderer ("Gender", t => t.Gender ? "男" : "女"), new ParameterRenderer ("Class", t => t.Class), new ParameterRenderer ("RecordNo", t => t.RecordNo), new ParameterRenderer ("Phone", t => t.Phone), new ParameterRenderer ("Email", t => t.Email) ), new ParameterRenderer("Author", "hzx") ) ); } catch (Exception e) { Console.WriteLine(e); } Console.WriteLine("finished!"); Console.ReadKey(); } }}
示例代码:ExcelReport/tree/master/examples/2.%E5%8D%95%E8%A1%8C%E9%87%8D%E5%A4%8D%E6%B8%B2%E6%9F%93%E7%A4%BA%E4%BE%8B" rel="nofollow">单行重复渲染示例
源码地址:ExcelReport" rel="nofollow">https://github.com/hanzhaoxin/ExcelReport