博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ExcelReport第一篇:使用ExcelReport导出Excel
阅读量:6242 次
发布时间:2019-06-22

本文共 6032 字,大约阅读时间需要 20 分钟。

导航

目   录:

下一篇:

概述

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

 

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

步骤一:设计Excel模板

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

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

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

步骤三:为模板填充数据

 

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

//实例化一个参数容器,并加载模板填充规则文件
ParameterCollection collection = new ParameterCollection();
collection.Load(@"Template\Template.xml");
 
//实例化一个元素格式化器列表
List
formatters = new List
();
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
gradeInfoList = new List
();
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
(collection["GradeDetail", "CourseID"].X, gradeInfoList,
new TableColumnInfo
(collection["GradeDetail", "CGPA"].Y, t => t.CGPA),
new TableColumnInfo
(collection["GradeDetail", "CourseID"].Y, t => t.CourseID),
new TableColumnInfo
(collection["GradeDetail", "CourseName"].Y, t => t.CourseName),
new TableColumnInfo
(collection["GradeDetail", "CourseType"].Y, t => t.CourseType),
new TableColumnInfo
(collection["GradeDetail", "Credit"].Y, t => t.Credit),
new TableColumnInfo
(collection["GradeDetail", "EvaluationMode"].Y, t => t.EvaluationMode),
new TableColumnInfo
(collection["GradeDetail", "GainCredit"].Y, t => t.GainCredit),
new TableColumnInfo
(collection["GradeDetail", "GPA"].Y, t => t.GPA),
new TableColumnInfo
(collection["GradeDetail", "Grade"].Y, t => t.Grade),
new TableColumnInfo
(collection["GradeDetail", "Remark"].Y, t => t.Remark),
new TableColumnInfo
(collection["GradeDetail", "StudyNature"].Y, t => t.StudyNature),
new TableColumnInfo
(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
formatters = new List
();
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
gradeInfoList = new List
();
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
(collection["GradeDetail", "CourseID"].X, gradeInfoList,
new TableColumnInfo
(collection["GradeDetail", "CGPA"].Y, t => t.CGPA),
new TableColumnInfo
(collection["GradeDetail", "CourseID"].Y, t => t.CourseID),
new TableColumnInfo
(collection["GradeDetail", "CourseName"].Y, t => t.CourseName),
new TableColumnInfo
(collection["GradeDetail", "CourseType"].Y, t => t.CourseType),
new TableColumnInfo
(collection["GradeDetail", "Credit"].Y, t => t.Credit),
new TableColumnInfo
(collection["GradeDetail", "EvaluationMode"].Y, t => t.EvaluationMode),
new TableColumnInfo
(collection["GradeDetail", "GainCredit"].Y, t => t.GainCredit),
new TableColumnInfo
(collection["GradeDetail", "GPA"].Y, t => t.GPA),
new TableColumnInfo
(collection["GradeDetail", "Grade"].Y, t => t.Grade),
new TableColumnInfo
(collection["GradeDetail", "Remark"].Y, t => t.Remark),
new TableColumnInfo
(collection["GradeDetail", "StudyNature"].Y, t => t.StudyNature),
new TableColumnInfo
(collection["GradeDetail", "Type"].Y, t => t.Type)
));
//导出文件到Web
Export.ExportToWeb(Server.MapPath(@"Template\Template.xls"), "GradeDetail",
new SheetFormatterContainer("GradeDetail", formatters));

导出文件截图:

 

源码下载:

下载地址:

转载地址:http://otsia.baihongyu.com/

你可能感兴趣的文章
vi编辑
查看>>
JS组件系列——Bootstrap寒冬暖身篇:弹出框和提示框效果以及代码展示
查看>>
调取jSon数据--展示
查看>>
Caffe学习系列(3):视觉层(Vision Layers)及参数
查看>>
Redis事务
查看>>
《AngularJS深度剖析与最佳实践》简介
查看>>
Android----------WindowManager
查看>>
通过DAC来连接SQL Server
查看>>
Jquery学习笔记:利用parent和parents方法获取父节点
查看>>
Python批量删除指定目录下的指定类型的文件
查看>>
Java程序猿的JavaScript学习笔记(3——this/call/apply)
查看>>
Mac OS设置rootpassword
查看>>
FreeLink开源呼叫中心设计思想
查看>>
//6小时更新一次首页
查看>>
Android开发者应该深入学习的10个开源应用项目
查看>>
对《LINQ能不能用系列(一)数组筛选效率对比》中测试的几个问题
查看>>
SGU 275 To xor or not to xor
查看>>
Sharepoint学习笔记—ECMAScript对象模型系列-- 6、使用ECMA操作列表项对象(list item)...
查看>>
【转载】常用统计软件下载地址大全
查看>>
新的思想来源
查看>>