RDLC系列鏈接
最近新換了工作,終於從單純的開發中脫離出來,換成主運維和偶爾開發了,但還沒有轉行。本來打算找工作是想轉行的,畢竟三線城市搞IT,以后真的不好說。最近經理讓給財務做一個報表展示系統。由於之前一直都是做B2C的網站,流程和報表方面幾乎沒有涉獵。只能從博客園和csdn上來查找資料,由於大部門人都是用的水晶報表,RDLC的資料挺少。所以找了好久,也自己實驗了好久,終於項目第一期做的項目差不多了,今天在家休息,就拿出點時間來整理一下。這個項目分兩期,第一期主要是數據表的展示,第二期會有矩陣,折線圖和柱狀圖的使用。所以,文章先把自己使用到的整理出來。參考的文章鏈接如下:
http://www.cnblogs.com/waxdoll/archive/2006/02/25/337713.html 蠟人張(想必做RDLC的都看過)
http://www.gotreportviewer.com/ GotReportViewer(很多功能都是照着它上面的實例來做的)
下面就按照我的步驟先來個簡單的栗子吧。
1.新建項目,這個大家都會了吧。就不嘮叨了,按照自己的習慣命名就好,我這里是demo1
2.然后是新建數據源,在網站或者項目上右擊,添加—>新建項—>數據—>數據集,命名為demo1.xsd,點擊確定
3.在數據源頁面拖入DataTabel,按照下圖設計表結構
4.新建報表,在網站或者項目上右擊,添加—>新建項—>Reporting—>報表,命名為demo1.rdlc,點擊確定
5.下面是重頭戲,設計報表,在左側的報表項中選擇表,拖到右邊的白色區域,會彈出如下對話框,輸入名稱選擇數據源以及數據集,點擊確認然后將需要顯示的字段添加到表中即可。
6.新建一個頁面,名稱demo1.aspx,在頁面設計中拖入ScriptManager和Reportview控件
7.然后開始寫后台綁定數據的代碼了,如下
demo.aspx.cs

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.Data; 8 using System.Data.SqlClient; 9 using Microsoft.Reporting.WebForms; 10 11 namespace rdlc1 12 { 13 public partial class demo11 : System.Web.UI.Page 14 { 15 protected void Page_Load(object sender, EventArgs e) 16 { 17 if (IsPostBack == false) 18 { 19 FillDataToReport(); 20 } 21 } 22 23 private void FillDataToReport() 24 { 25 26 // DataTable dt1 = GetDataTabel(); 27 28 //if (dt1.Rows.Count > 0) 29 //{ 30 31 //} 32 33 DataTable dt = new DataTable(); 34 dt.Columns.Add("Dept", typeof(string)); 35 dt.Columns.Add("CostCenter", typeof(string)); 36 dt.Columns.Add("SalePrice", typeof(decimal)); 37 dt.Rows.Add("IT", "810", 867); 38 dt.Rows.Add("IT", "811", 877); 39 dt.Rows.Add("E", "710", 867); 40 dt.Rows.Add("E", "711", 877); 41 dt.Rows.Add("L", "710", 867); 42 dt.Rows.Add("L", "711", 877); 43 44 45 ReportViewer1.LocalReport.ReportPath = "demo1.rdlc"; 46 47 //顯示報表 48 ReportViewer1.LocalReport.DataSources.Clear(); 49 ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("dtDemo", dt));//要和設計報表時指定的名稱一致,這里是dtDemo 50 ReportViewer1.LocalReport.Refresh(); 51 52 53 } 54 55 } 56 }
8.一切貌似都大功告成,F5運行,彈出如下報錯。猶如晴天小霹靂,不要着急,僅需要配置下web.config即可
9.在web.config文件中添加添加httphandler節點即可,下面上我的web.config文件。

<?xml version="1.0"?> <!-- 有關如何配置 ASP.NET 應用程序的詳細消息,請訪問 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <httpHandlers> <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false" /> </httpHandlers> <compilation debug="true" targetFramework="4.0"> <assemblies> <add assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> <add assembly="Microsoft.ReportViewer.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> <add assembly="Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> <add assembly="System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation> </system.web> </configuration>
10.F5運行,可以鳥,上圖。
11.今天先整理一部分,后續的將抽空給大家呈現。主要是行分組和總計