ExcelReport第一篇:使用ExcelReport导出Excel

导航

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

下一篇:ExcelReport源码解析

概述

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

 

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

步骤一:设计Excel模板

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

image

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

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

image

步骤三:为模板填充数据

实现代码(导出到本地):
//实例化一个参数容器,并加载模板填充规则文件
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));
导出文件截图:

image

 

源码下载:

image

转载于:https://my.oschina.net/hanzhaoxin/blog/373279

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Excel Report BuilderExcel Report 万能报表平台Excel 使用WEB服务(webservice)访问远程数据库使用本软件可以使你的应用系统(数据库)和excel相连。可以把它嵌入到你的应用系统里,做为应用系统的外挂程序。可以利用excel强大的编辑功能,随心所欲地开发出精美的报表。本软件基于Web Service(Web服务)新技术,所以它支持web网络,可以通过Internet访问远程数据。只要你稍懂SQL,就可以在Excel Report 万能报表平台上设计报表。通过设置字段,参数等信息来设计报表。BI智能报表,支持透视表。提供存储过程接口,便于用户二次开发。有用户管理,报表权限的管理。支持多语种。 软件环境: 客户端: Windows 2000及以上版本,Microsoft Office 2000及以上版本 服务器端:Windows和Linux都可以,jsdk1.4,tomcat5 数据库:支持Oracle, SQL Server 等数据库。下载地址: http://203.208.248.203:81/pan/Excel/Excel_Report_Setup.rar相关软件下载地址: Jdk 1.4:http://203.208.248.203:81/pan/Excel/j2sdk-1_4_2_06-windows-i586-p.exeTomcat5:http://203.208.248.203:81/pan/Excel/jakarta-tomcat-5.0.27.exe SQL Server SP3:http://203.208.248.203:81/pan/Excel/sql2ksp3.exeSQL Server SP4:http://download.microsoft.com/download/9/b/f/9bff6646-2cdb-4069-ada0-548be9cb9338/SQL2000-KB884525-SP4-x86-CHS.EXE联系方式: http://pansoft.ik8.com QQ: 10124900MSN: bear_pan@hotmail.comE_Mail: bear_pan@163.comExcel Report BuilderExcel Report Builder is an easy and convenient tool for the creation and customization of reports which takes advantage of the formatting and presentation capabilities of Microsoft Excel. The program allows the rapid construction of reports.A GUI style design environment, which allows the user to design their reports visually inside MS Excel. Reports can be saved and then viewed and printed as pure Excel documents. The reports are created and printed rapidly.In fact, it is by harnassing the power of Excel that gives Excel Report Builder these abilities.No technical knowledge is needed in order for the user to customise his or her own reports. Key Features of the Database Report Builder for Excel include: Report creation using Microsoft Excel Get data through webservice,so you can get remote data Support of the SQL-queries for the data sets creation Calling of the stored procedures for the data sets creation Creation of the reports with parameters Work with the Microsoft Excel macros Charts creation in a reportSoftware: Client: Windows 2000 or above,Microsoft Office 2000 or above Server: jsdk1.4,tomcat5 Database: Oracle or SQL Server Download: http://203.208.248.203:81/pan/Excel/Excel_Report_Setup.rarOther Softwares: Jdk 1.4:http://203.208.248.203:81/pan/Excel/j2sdk-1_4_2_06-windows-i586-p.exeTomcat5:http://203.208.248.203:81/pan/Excel/jakarta-tomcat-5.0.27.exeSQL Server SP3:http://203.208.248.203:81/pan/Excel/sql2ksp3.exeSQL Server SP4:http://download.microsoft.com/download/9/b/f/9bff6646-2cdb-4069-ada0-548be9cb9338/SQL2000-KB884525-SP4-x86-CHS.EXEContact: http://pansoft.ik8.com QQ: 10124900MSN: bear_pan@hotmail.comE_Mail: bear_pan@163.com

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值