C#利用ReportViewer生成報表
本文主要是利用微軟自帶的控件ReportViewer進行報表設計的小例子,具體內(nèi)容如下
涉及知識點:
ReportViewer :位于Microsoft.Reporting.WinForms命名空間, 主要用于報表的顯示
Report:報表,以rdlc結(jié)尾的文件,可視化設計報表模板。
報表數(shù)據(jù):內(nèi)置字段,參數(shù),圖像,數(shù)據(jù)集(本報表主要使用參數(shù),和數(shù)據(jù)集)
ReportParameter:使用名稱和值實例化新的報表參數(shù)
ReportDataSource:報表的數(shù)據(jù)源與DataTable對象聯(lián)系起來
效果圖如下:

相關(guān)代碼如下:
/// <summary>
/// 設置報表
/// </summary>
private void SetReport()
{
//第一步:清除之前的數(shù)據(jù)
this.rptView.LocalReport.DataSources.Clear();
//第二步:指定報表路徑
this.rptView.LocalReport.ReportPath = "Report2.rdlc";
//第三步:構(gòu)造新的DataTable
DataTable dt = new DataTable("DataTable1");
dt.Columns.Add("Name");
dt.Columns.Add("Score");
dt.Columns.Add("Id");
dt.Rows.Add(new object[] { "語文", 80, "Y0001" });
dt.Rows.Add(new object[] { "數(shù)學", 75, "S0001" });
dt.Rows.Add(new object[] { "英文", 96, "E0001" });
//名稱不能寫錯,和報表中的數(shù)據(jù)集名稱一致
ReportDataSource rdsItem = new ReportDataSource("DataSet1", dt);
//此處可以有多個數(shù)據(jù)源
this.rptView.LocalReport.DataSources.Add(rdsItem);
//第四步:構(gòu)造參數(shù)
List<ReportParameter> lstParameter = new List<ReportParameter>() {
new ReportParameter("Title",this.txtTitle.Text),
new ReportParameter("Id",this.txtId.Text),
new ReportParameter("Name",this.txtName.Text),
new ReportParameter("Age",this.txtAge.Text),
new ReportParameter("Sex",this.txtSex.Text),
new ReportParameter("Salary",this.txtSalary.Text),
new ReportParameter("Depart",this.txtDepart.Text)
};
this.rptView.LocalReport.SetParameters(lstParameter);
this.rptView.ZoomMode = ZoomMode.Percent;
this.rptView.ZoomPercent = 100;
//第五步:刷新報表
this.rptView.RefreshReport();
}
源碼下載鏈接
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
學習C#靜態(tài)函數(shù)及變量的一個精典例子與代碼
學習C#靜態(tài)函數(shù)及變量的一個精典例子與代碼...2007-03-03

