我用的是VSEnterprise2015
注意:如果要用VS自帶的報表,就需要在安裝Microsoft SQL Server Data Tools
下面講講具體步驟:
1.添加winform界面
2.添加生成報表界面這里要注意,工具箱的數據里面要有
ReportViewer控件,如果沒有則要工具箱-》數據-》選擇項-》.NET Framework組件-》勾選ReportViewer(命名空間對應的是Microsoft.Reporting.WinForms【winform程序,如果是asp.net則勾選命名空間是Microsoft.Reporting.WebForms】)
生成報表界面對應代碼
try { this.reportViewer1.LocalReport.ReportPath = "Report_JSHZB.rdlc";//Report_JSHZB.rdlc對應添加的報表文件 DataTable dt = new DataTable(); dt.Columns.Add("BMMC"); dt.Columns.Add("JYBS", typeof(int));//對應報表文件中的數據集字段 dt.Columns.Add("JYJE", typeof(decimal)); dt.Columns.Add("HCBS", typeof(int)); dt.Columns.Add("HCJE", typeof(decimal)); dt.Columns.Add("JSJE", typeof(decimal)); dt.Rows.Add("第一食堂", 13305, 27246.68, 0, 0.0, 27246.68); dt.Rows.Add("第二食堂", 12277, 27965.63, 1, 15.0, 27950.63); dt.Rows.Add("超市", 26, 1062.90, 0, 0.0, 1065.90); dt.Rows.Add("醫務室", 78, 857.00, 0, 0.0, 857.00); dt.Rows.Add("電子閱覽室", 157, 871.00, 0, 0.0, 871.00); dt.Rows.Add("圖書館", 120, 1176.50, 0, 0.0, 1176.50); dt.Rows.Add("學生公寓沐浴", 693, 5371.19, 0, 0.0, 5371.19); ReportDataSource rds1 = new ReportDataSource("DataSet_JSHZB", dt);//DataSet_JSHZB對應報表文件中的數據集 this.reportViewer1.LocalReport.DataSources.Add(rds1); ReportParameter Rar_CNDate = new ReportParameter(); Rar_CNDate.Name = "Rar_CNDate";//報表參數,對應報表文件中的參數 Rar_CNDate.Values.Add(CNDate.ToString("yyyy-MM-dd")); ReportParameter Rar_Time = new ReportParameter(); Rar_Time.Name = "Rar_Time"; Rar_Time.Values.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); ReportParameter Rar_UserName = new ReportParameter(); Rar_UserName.Name = "Rar_UserName"; Rar_UserName.Values.Add("系統管理員"); this.reportViewer1.LocalReport.SetParameters(Rar_CNDate); this.reportViewer1.LocalReport.SetParameters(Rar_Time); this.reportViewer1.LocalReport.SetParameters(Rar_UserName); this.reportViewer1.RefreshReport(); } catch (Exception ex) { MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error); } this.reportViewer1.RefreshReport();
3.添加報表文件
4.最終生成的報表