本文主要是利用微软自带的控件ReportViewer进行报表设计的小例子,仅供学习分享使用,如有不足之处,还请指正。
涉及知识点:
- ReportViewer :位于Microsoft.Reporting.WinForms命名空间, 主要用于报表的显示
- Report:报表,以rdlc结尾的文件,可视化设计报表模板。
- 报表数据:内置字段,参数,图像,数据集(本报表主要使用参数,和数据集)
- ReportParameter:使用名称和值实例化新的报表参数
- ReportDataSource:报表的数据源与DataTable对象联系起来
效果图
如下:
核心代码
相关代码如下:
1 ///2 /// 设置报表 3 /// 4 private void SetReport() 5 { 6 //第一步:清除之前的数据 7 this.rptView.LocalReport.DataSources.Clear(); 8 //第二步:指定报表路径 9 this.rptView.LocalReport.ReportPath = "Report2.rdlc";10 //第三步:构造新的DataTable11 DataTable dt = new DataTable("DataTable1");12 dt.Columns.Add("Name");13 dt.Columns.Add("Score");14 dt.Columns.Add("Id");15 dt.Rows.Add(new object[] { "语文", 80, "Y0001" });16 dt.Rows.Add(new object[] { "数学", 75, "S0001" });17 dt.Rows.Add(new object[] { "英文", 96, "E0001" });18 //名称不能写错,和报表中的数据集名称一致19 ReportDataSource rdsItem = new ReportDataSource("DataSet1", dt);20 //此处可以有多个数据源21 this.rptView.LocalReport.DataSources.Add(rdsItem);22 //第四步:构造参数23 ListlstParameter = new List () {24 new ReportParameter("Title",this.txtTitle.Text),25 new ReportParameter("Id",this.txtId.Text),26 new ReportParameter("Name",this.txtName.Text),27 new ReportParameter("Age",this.txtAge.Text),28 new ReportParameter("Sex",this.txtSex.Text),29 new ReportParameter("Salary",this.txtSalary.Text),30 new ReportParameter("Depart",this.txtDepart.Text)31 };32 this.rptView.LocalReport.SetParameters(lstParameter);33 this.rptView.ZoomMode = ZoomMode.Percent;34 this.rptView.ZoomPercent = 100;35 //第五步:刷新报表36 this.rptView.RefreshReport();37 }
源码链接