首页 > 代码库 > ExcelReport第一篇:使用ExcelReport导出Excel

ExcelReport第一篇:使用ExcelReport导出Excel

导航

目   录:基于NPOI的报表引擎——ExcelReport

下一篇:ExcelReport源码解析

概述

本篇将通过导出学生成绩的示例演示“使用ExcelReport导出Excel”的步骤。

 

示例(导出学生成绩明细)

步骤一:设计Excel模板

首先,使用Excel软件设计模板(我没有安装MSOffice,用LibreOffice Calc做的演示)。在模板中要填充数据的位置用$[ParameterName]代替。

技术分享

步骤二:生成模板填充规则文件

使用模板填充规则文件生成工具(ERTool)为步骤一设计的Excel模板生成填充规则文件。

技术分享

步骤三:为模板填充数据

实现代码(导出到本地):

//实例化一个参数容器,并加载模板填充规则文件
ParameterCollection collection = new ParameterCollection();
collection.Load(@"Template\Template.xml");
 
//实例化一个元素格式化器列表
List<ElementFormatter> formatters = new List<ElementFormatter>();
formatters.Add(new CellFormatter(collection["GradeDetail", "Dept"], "某某学院"));   //添加一个单元格格式化器
formatters.Add(new CellFormatter(collection["GradeDetail", "Class"], "某某班级"));
formatters.Add(new CellFormatter(collection["GradeDetail", "StudNo"], "2009*****"));
formatters.Add(new CellFormatter(collection["GradeDetail", "StudName"], "韩兆新"));
formatters.Add(new CellFormatter(collection["GradeDetail", "ExportDate"], DateTime.Now));
 
List<GradeInfo> gradeInfoList = new List<GradeInfo>();
gradeInfoList.Add(new GradeInfo() { CGPA = 18, CourseID = "KC-0001", CourseName = "高等数学", CourseType = "理论课", Credit = 6, EvaluationMode = "考试", GainCredit = 6, GPA = 3, Grade = 86, StudyNature = "初修", Type = "必修课" });
gradeInfoList.Add(new GradeInfo() { CGPA = 2, CourseID = "KC-0002", CourseName = "计算机应用基础", CourseType = "理论课", Credit = 2, EvaluationMode = "考试", GainCredit = 2, GPA = 1, Grade = 93, StudyNature = "初修", Type = "必修课" });
gradeInfoList.Add(new GradeInfo() { CGPA = 9, CourseID = "KC-0003", CourseName = "C程序设计", CourseType = "理论课", Credit = 3, EvaluationMode = "考试", GainCredit = 3, GPA = 3, Grade = 97, StudyNature = "初修", Type = "必修课", Remark = "备注信息" });
 
//添加一个Table格式化器
formatters.Add(new TableFormatter<GradeInfo>(collection["GradeDetail", "CourseID"].X, gradeInfoList,
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CGPA"].Y, t => t.CGPA),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CourseID"].Y, t => t.CourseID),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CourseName"].Y, t => t.CourseName),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CourseType"].Y, t => t.CourseType),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Credit"].Y, t => t.Credit),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "EvaluationMode"].Y, t => t.EvaluationMode),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "GainCredit"].Y, t => t.GainCredit),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "GPA"].Y, t => t.GPA),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Grade"].Y, t => t.Grade),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Remark"].Y, t => t.Remark),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "StudyNature"].Y, t => t.StudyNature),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Type"].Y, t => t.Type)
));
//导出文件到本地
Export.ExportToLocal(@"Template\Template.xls", saveFileDlg.FileName,
new SheetFormatterContainer("GradeDetail", formatters));

实现代码(导出到Web):

//实例化一个参数容器,并加载模板填充规则文件
ExcelReport.ParameterCollection collection = new ExcelReport.ParameterCollection();
collection.Load(Server.MapPath(@"Template\Template.xml"));
 
//实例化一个元素格式化器列表
List<ElementFormatter> formatters = new List<ElementFormatter>();
formatters.Add(new CellFormatter(collection["GradeDetail", "Dept"], "某某学院"));   //添加一个单元格格式化器
formatters.Add(new CellFormatter(collection["GradeDetail", "Class"], "某某班级"));
formatters.Add(new CellFormatter(collection["GradeDetail", "StudNo"], "2009*****"));
formatters.Add(new CellFormatter(collection["GradeDetail", "StudName"], "韩兆新"));
formatters.Add(new CellFormatter(collection["GradeDetail", "ExportDate"], DateTime.Now));
 
List<GradeInfo> gradeInfoList = new List<GradeInfo>();
gradeInfoList.Add(new GradeInfo() { CGPA = 18, CourseID = "KC-0001", CourseName = "高等数学", CourseType = "理论课", Credit = 6, EvaluationMode = "考试", GainCredit = 6, GPA = 3, Grade = 86, StudyNature = "初修", Type = "必修课" });
gradeInfoList.Add(new GradeInfo() { CGPA = 2, CourseID = "KC-0002", CourseName = "计算机应用基础", CourseType = "理论课", Credit = 2, EvaluationMode = "考试", GainCredit = 2, GPA = 1, Grade = 93, StudyNature = "初修", Type = "必修课" });
gradeInfoList.Add(new GradeInfo() { CGPA = 9, CourseID = "KC-0003", CourseName = "C程序设计", CourseType = "理论课", Credit = 3, EvaluationMode = "考试", GainCredit = 3, GPA = 3, Grade = 97, StudyNature = "初修", Type = "必修课", Remark = "备注信息" });
 
//添加一个Table格式化器
formatters.Add(new TableFormatter<GradeInfo>(collection["GradeDetail", "CourseID"].X, gradeInfoList,
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CGPA"].Y, t => t.CGPA),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CourseID"].Y, t => t.CourseID),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CourseName"].Y, t => t.CourseName),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "CourseType"].Y, t => t.CourseType),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Credit"].Y, t => t.Credit),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "EvaluationMode"].Y, t => t.EvaluationMode),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "GainCredit"].Y, t => t.GainCredit),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "GPA"].Y, t => t.GPA),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Grade"].Y, t => t.Grade),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Remark"].Y, t => t.Remark),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "StudyNature"].Y, t => t.StudyNature),
new TableColumnInfo<GradeInfo>(collection["GradeDetail", "Type"].Y, t => t.Type)
));
//导出文件到Web
Export.ExportToWeb(Server.MapPath(@"Template\Template.xls"), "GradeDetail",
new SheetFormatterContainer("GradeDetail", formatters));

导出文件截图:

技术分享

 

源码下载:

技术分享

下载地址:https://github.com/hanzhaoxin/ExcelReport

ExcelReport第一篇:使用ExcelReport导出Excel