在项目中要进行导出Excel报表给客户,里面有统计,就心思用Aspose.Cells 配合实体类数据直接导出。结果呢比较麻烦。然后就检索找到利用Excel模板配合实体类数据导出。
总的来说一般的报表生成,基本上是基于以下几种方式:一种是基于微软Excel内置的引擎来实现;一种是构造HTML格式的Excle报表;一种是基于控件的方式来处理,基于控件有很多种方式,个人认为比较有名的是Aspose.Cell和NPOI,下面用到的是Aspose.Cell
效果图:
Excel模板及相关变量
报表1模板如下所示(其中通过引用集合的对象是通过&=来引用,对象的属性或者列名,通过如&=Export.作业完成计划方式引用,非常直观方便)
Aspose.Cell控件支持多种参数变量的绑定操作,如支持DataSet、Datatable、IList集合,实体类集合、类对象等。
OverLoaded Method | Description |
---|---|
[public void SetDataSource ( DataTable dataTable )] | Sets data source of a DataTable object. |
[public void SetDataSource ( String dataSourceName, DataView dataView )] | Sets data source of a DataView object and binds it to a data source name. |
[public void SetDataSource ( DataView dataView )] | Sets data source of a DataView object. |
[public void SetDataSource ( String name, IDataReader dataReader, Int32 rowCount )] | Sets data source of a IDataReader object. |
[public void SetDataSource ( String variable, Object data )] | Sets data binding to a variable. |
[public void SetDataSource ( String variable, Object\[\] dataArray )] | Sets data array binding to a variable. |
[public void SetDataSource ( OleDbConnection connection )] | Sets data source of a OleDbConnection object. |
[public void SetDataSource ( SqlConnection connection )] | Sets data source of a SqlConnection object. |
参考:http://docs.aspose.com:8082/docs/display/cellsnet/Aspose.Cells.WorkbookDesigner+Class
下面我主要用List 实体类集合
protected void ExportExcel_ServerClick(object sender, EventArgs e) { HttpCookie getCookies = Request.Cookies["UserLogin"]; if (getCookies != null) { string[] strParm = hfExcelData.Value.Split(','); List<jianpinorzhen_view> getJianPinOrZhenData = SearchExportData.LstSearchExportJianPinOrZhen (Server.UrlDecode(getCookies["SqlConString"]), getCookies["CustomCode"],getCookies["CustomType"], strParm[0], strParm[1], strParm[2], strParm[3], strParm[4]); //创建一个workbookdesigner对象 WorkbookDesigner designer = new WorkbookDesigner(); //制定报表模板 switch (strParm[4]) { case "工厂入货箱单信息": designer.Open(Server.MapPath(@"model\AllWarehousing.xls")); break; case "上货开箱信息": designer.Open(Server.MapPath(@"model\Warehousing.xls")); break; case "检品之后信息": designer.Open(Server.MapPath(@"model\Dress_Jp.xls")); break; case "检针之后信息": designer.Open(Server.MapPath(@"model\Dress_Jz.xls")); break; default: break; } //设置实体类对象<span style="color:#FF0000;">这里设置了Export 变量,然后在模板里面我们就用了。 designer.SetDataSource("Export", getJianPinOrZhenData); //报表标题头部 //可以扩展多个 designer.SetDataSource("ExportUtils",strParm[4]); //报表截止日期 designer.SetDataSource("ExportDate", DateTime.Now.ToString("yyyy年MM月dd日")); //根据数据源处理生成报表内容 designer.Process(); //客户端保存的文件名//如果保存的文件名是汉字的话一定要编码,否则就是乱码 HttpUtility.UrlEncode(strParm[4]) string fileName = HttpUtility.UrlEncode(strParm[4]) + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; designer.Save(fileName, SaveType.OpenInExcel, FileFormatType.Excel2003, Response); Response.Flush(); Response.Close(); designer = null; Response.End(); }}
DataTable dt = ExcuteDataTable(querySql); dt.TableName = "Export"; if (dt.Rows.Count == 0) return; WorkbookDesigner designer = new WorkbookDesigner(); string fileName = HttpUtility.UrlEncode(strParm[4]) + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; designer.Open(fileName<code class="csharp spaces"></code>); //设置DataTable对象 designer.SetDataSource("Export",dt); //设置DataSet对象 designer.SetDataSource(ds.Tables["Export"]); designer.Process();