Asp.net 自带报表的使用详解
1:新建報表所需的數據源dataset.cs
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.data;
namespace ********
{
public class dataset
{
public datatable creatdataset()
{
datatable dt = new datatable();
dt.columns.add("a");
dt.columns.add("b");
dt.columns.add("c");
return dt;
}
}
}
指定所需要綁定的table的列,返回datatable 類,creatdataset方法名稱隨便起,也可以在一個類裏面定義多個方法(不同數據源)
2:設計報表
報表設計這裡就不涉及了
3:把第一步新建的數據源加到報表裏面綁定
注意:這裡需要先引用 interop.vba.dll 才可以把新建的cs文件作為數據源導入
把數據源導入后綁定即可
4:直接把報表導出為pdf,excel等格式
reportviewer viewer = new reportviewer();
viewer.processingmode = processingmode.local;
viewer.localreport.reportembeddedresource = "***.page.report.report1.rdlc";
reportdatasource rds_1 = new reportdatasource("dataset1", dtreport);//dataset1為報表裏面的數據源名稱
viewer.localreport.datasources.add(rds_1);
reportparameter rp1 = new reportparameter("參數1","參數1的值" );//給參數賦值
reportparameter rp2 = new reportparameter("參數2","參數2的值" );
viewer.localreport.setparameters(new reportparameter[] {rp1, rp2 });
warning[] warnings;
string[] streamids;
string mimetype = string.empty;
string encoding = string.empty;
string extension = string.empty;
byte[] bytes = viewer.localreport.render("excel", null, out mimetype, out encoding, out extension, out streamids, out warnings);
//excel ,pdf ,word 等格式
// now that you have all the bytes representing the pdf report, buffer it and send it to the client.
response.buffer = true;
response.clear();
response.contenttype = mimetype;
response.addheader("content-disposition", "attachment; filename=1_" + datetime.now.tostring("yyyymmddhhssmm") + "" + "." + extension);
response.binarywrite(bytes); // create the file
response.flush(); // send it to the client to download
5:在頁面引用報表(rpresult為報表控件)
datatable dt = new datatable();//自己拼出數據源就可以
reportdatasource repdatasource = new reportdatasource("dataset1", dt);
//*設置報表參數,并顯示
this.rpresut.localreport.reportembeddedresource = "***.page.report.report1.rdlc"";
this.rpresut.localreport.datasources.clear();
this.rpresut.localreport.datasources.add(repdatasource);
reportparameter rp1 = new reportparameter("參數1","參數1的值" );//給參數賦值
reportparameter rp2 = new reportparameter("參數2","參數2的值" );
this.rpresut.localreport.setparameters(new reportparameter[] {rp1, rp2 });
this.rpresut.databind();
this.rpresut.localreport.refresh();
至此,報表的產出和顯示都ok了,如果需要更深入的了解,請查看其它文章