Asp.net 自帶報(bào)表的使用詳解
1:新建報(bào)表所需的數(shù)據(jù)源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方法名稱隨便起,也可以在一個(gè)類裏面定義多個(gè)方法(不同數(shù)據(jù)源)
2:設(shè)計(jì)報(bào)表
報(bào)表設(shè)計(jì)這裡就不涉及了
3:把第一步新建的數(shù)據(jù)源加到報(bào)表裏面綁定
注意:這裡需要先引用 Interop.VBA.dll 才可以把新建的CS文件作為數(shù)據(jù)源導(dǎo)入
把數(shù)據(jù)源導(dǎo)入后綁定即可
4:直接把報(bào)表導(dǎo)出為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為報(bào)表裏面的數(shù)據(jù)源名稱
viewer.LocalReport.DataSources.Add(rds_1);
ReportParameter rp1 = new ReportParameter("參數(shù)1","參數(shù)1的值" );//給參數(shù)賦值
ReportParameter rp2 = new ReportParameter("參數(shù)2","參數(shù)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:在頁面引用報(bào)表(rpResult為報(bào)表控件)
DataTable dt = new DataTable();//自己拼出數(shù)據(jù)源就可以
ReportDataSource repDataSource = new ReportDataSource("DataSet1", dt);
//*設(shè)置報(bào)表參數(shù),并顯示
this.rpResut.LocalReport.ReportEmbeddedResource = "***.Page.Report.Report1.rdlc"";
this.rpResut.LocalReport.DataSources.Clear();
this.rpResut.LocalReport.DataSources.Add(repDataSource);
ReportParameter rp1 = new ReportParameter("參數(shù)1","參數(shù)1的值" );//給參數(shù)賦值
ReportParameter rp2 = new ReportParameter("參數(shù)2","參數(shù)2的值" );
this.rpResut.LocalReport.SetParameters(new ReportParameter[] {rp1, rp2 });
this.rpResut.DataBind();
this.rpResut.LocalReport.Refresh();
至此,報(bào)表的產(chǎn)出和顯示都OK了,如果需要更深入的了解,請(qǐng)查看其它文章
相關(guān)文章
ASP.NET設(shè)計(jì)網(wǎng)絡(luò)硬盤之下載或在線查看實(shí)現(xiàn)代碼
在目錄瀏覽中,如果選擇的是一個(gè)文件,單擊“打開”按鈕就可以進(jìn)行文件下載2012-10-10ASP.NET實(shí)現(xiàn)讀取Excel內(nèi)容并在Web上顯示
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)讀取Excel內(nèi)容并在Web上顯示,很實(shí)用的一個(gè)技巧,需要的朋友可以參考下2014-08-08將Access數(shù)據(jù)庫中數(shù)據(jù)導(dǎo)入到SQL Server中的詳細(xì)方法實(shí)例
將Access數(shù)據(jù)庫中數(shù)據(jù)導(dǎo)入到SQL Server中的詳細(xì)方法實(shí)例,需要的朋友可以參考一下2013-03-03使用SNK密鑰文件保護(hù)你的DLL和代碼不被反編譯教程
這篇文章主要介紹了使用SNK密鑰文件保護(hù)你的DLL和代碼不被反編譯教程, SNK,作為程序后綴的時(shí)候,是.net中的強(qiáng)密匙加密文件,需要的朋友可以參考下2014-09-09ADO.NET實(shí)現(xiàn)對(duì)SQL Server數(shù)據(jù)庫的增刪改查示例
本篇文章主要介紹了ADO.NET實(shí)現(xiàn)對(duì)SQL Server數(shù)據(jù)庫的增刪改查示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-01-01ASP.NET對(duì)SQLServer的通用數(shù)據(jù)庫訪問類
這篇文章主要實(shí)現(xiàn)了ASP.NET對(duì)SQLServer的通用數(shù)據(jù)庫訪問類2016-02-02ASP.NET中動(dòng)態(tài)控制RDLC報(bào)表
ASP.NET中動(dòng)態(tài)控制RDLC報(bào)表...2006-09-09