這2天糾結的報表基本上已近完成大部分功能。現在總結一下自己近期的學習成果
首先製作微軟RDLC報表由以下三部分構成:1.製作自己的DateSet集合(就是報表的資料集);2.製作自己的報表檔案.rdlc檔案,用於畫做報表樣式,裡面有微軟內建的匯出和列印功能,其實就為了少做這2個功能做少部分報表完全還不如自己產生報表靈活。製作顯示報表的前台頁面aspx檔案,基本上就是插入一個ReportViewer然後關聯上面的.rdlc檔案,注意別忘了更新資料來源和插入ScriptManager.
是我這是這幾天來做的,但基本都是建立這3個檔案,網上可以找很多教程可以學習,
這些是我主要參考的網站
http://www.cnblogs.com/JamesLi2015/archive/2010/01/30/1660086.html
http://www.cnblogs.com/lauyee/archive/2010/07/26/1783694.html
http://wenku.baidu.com/view/364f5048c850ad02de8041f5.html
http://www.cnblogs.com/lauyee/archive/2010/07/18/1780124.html
好吧,開始做了,前面都很簡單:怎麼做一個DataSet,怎麼繪製自己的RDLC,這些東西查查什麼是資料集,什麼是表頭,行組,列祖就可以。還有報表裡面的參數也可以傳入。用LocalReport.SetParameters()方法。
痛點發生在要動態構造一個DataSet,網上剛開始給了好多方法,但是自己老是不成功,後來總結出2點
1. 參照的資料集名稱錯誤,假如你的RDLC參照資料集DateSet1,在自動定義新的資料集時候
ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", ds.Tables[0]));,
必須把名字標記成DateSet1,否則會無法識別。還有新的資料集欄位必須和原有欄位名稱一致。
2. 這個最悲劇,竟然是自己的的SQL語句寫錯了,汗顏不說了...
看看原始碼和最後運行圖示
protected void Button1_Click(object sender, EventArgs e) { //先引用源報表並修改參數 ReportParameter rp = new ReportParameter("content", "庫房"); this.ReportViewer1.LocalReport.ReportPath = AppDomain.CurrentDomain.BaseDirectory + "Report4.rdlc"; this.ReportViewer1.LocalReport.SetParameters(new ReportParameter[] { rp }); string connstring = "Data Source=192.168.1.111;Initial Catalog=SameWayAgile_4_14;Integrated Security=True"; System.Data.SqlClient.SqlConnection conn1 = new System.Data.SqlClient.SqlConnection(connstring); System.Data.SqlClient.SqlCommand command1 = new System.Data.SqlClient.SqlCommand( "SELECT GoodsName AS 商品名稱, ABCTypeName AS ABC類, MiddleTypeName AS 中類, GoodsTypeName AS 大類, TotalValue AS 金額, ActualQuantity AS 數量 FROM Part_GoodsView", conn1); System.Data.SqlClient.SqlDataAdapter ada1 = new System.Data.SqlClient.SqlDataAdapter(command1); DataSet ds = new DataSet(); try { conn1.Open(); ada1.Fill(ds); ds.DataSetName = "DataSet1"; } catch (Exception ex) { string ss = ex.Message; } finally { conn1.Close(); command1.Dispose(); conn1.Dispose(); } if (ds.Tables[0] != null) { ReportViewer1.LocalReport.DataSources.Clear(); ReportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", ds.Tables[0])); } ReportViewer1.LocalReport.Refresh(); }
今天做了一個變態的報表,剛開始覺得不能實現,後來瞭解報表檔案特性還是可以實現的,就是矩陣裡面嵌套矩陣。哈哈
好了插圖