標籤:web開發 mvc easyui web開發架構
RDLC是一個不錯的報表,有著比較不錯的設計模式和展現效果,在我的Winform開發裡面,使用RDLC也是一個比較方便操作,如可以參考文章《DevExpress的XtraReport和微軟RDLC報表的使用和對比》或者《會員管理系統的設計和開發(2)-- RDLC報表的設計及動態載入》進行瞭解。但是基於MVC方式,如何構建和展現RDLC報表呢?本文主要介紹如何在基於MVC4+EasyUI的Web開發架構上進行RDLC的整合和使用。
1、RDLC綁定資料來源
RDLC的報表設計,是使用VS直接進行編輯的,因此它不管是在Web上,還是在Winform上,設計的方式都差不多,好像綁定資料來源的方式有一點不同,WInform的可以選擇基於,而Web的只能基於資料庫連接方式構建資料繫結對象。
它們兩者之間在建立資料來源的時候,彈出的對話方塊選擇介面如下所示,看起來Winform的方式選擇資料來源的方式很多樣化。
650) this.width=650;" src="http://images.cnitblog.com/blog/8867/201411/200920343591653.png" style="border:0px;" />
650) this.width=650;" src="http://images.cnitblog.com/blog/8867/201411/200920460466914.png" style="border:0px;" />
最終,通過Web方式構建了一個資料集,並在設計檢視裡面設計一個使用者的報表介面,並綁定它們對應的欄位,具體如下所示。
650) this.width=650;" src="http://images.cnitblog.com/blog/8867/201411/200924005781886.png" style="border:0px;" />
在RDLC的設計介面上,它們的操作內容好像是一樣的,沒有什麼差異,可以使用各種數學函數SUM、COUNT等,也可以使用對欄位的運算式,格式樣式等方式,從而方便構建出一些準確、美觀的報表,這部分可以參考文中開始的那兩篇引用文章,這裡就不再進行細節上的贅述,因為它們是一樣的。
2、在Web介面上建立一些功能按鈕和實現
我們在MVC項目裡面,建立並儲存好RDLC報表檔案到對應的Report目錄上,如下所示。
650) this.width=650;" src="http://images.cnitblog.com/blog/8867/201411/200929297182497.png" style="border:0px;" />
然後建立一個視圖,並在視圖裡面構建一些按鈕,方便構建查詢不同的報表格式功能,具體效果如下所示。
650) this.width=650;" src="http://images.cnitblog.com/blog/8867/201411/200931291566409.png" style="border:0px;" />
HTML代碼如下所示
<div style="padding:10px; border:1px solid black"><div><a href="@Url.Action("UserRdlcReport", new { format = "Image" })" class="easyui-linkbutton" data-options="iconCls:‘icon-view‘">圖片輸出</a><a href="@Url.Action("UserRdlcReport", new { format = "PDF" })" class="easyui-linkbutton" data-options="iconCls:‘icon-view‘"> PDF輸出</a><a href="@Url.Action("UserRdlcReport", new { format = "Excel" })" class="easyui-linkbutton" data-options="iconCls:‘icon-view‘">Excel輸出</a><a href="@Url.Action("UserRdlcReport", new { format = "Word" })" class="easyui-linkbutton" data-options="iconCls:‘icon-view‘">Word輸出</a></div></div><div id="autoUpdate" style="display: none; overflow-y: auto" class="SlideContainer"><table width="100%" height="100%"><tr><td><table><tr><td></td><td></td></tr></table></td></tr><tr><td><iframe id="myReport" width="100%" height="800"></iframe></td></tr></table></div>
完成這些布局後,我們還需要在對應的控制器裡面,對RDLC的報表進行資料繫結並呈現出來。
綁定RDLC報表,並賦值對應的資料來源操作如下所示。
LocalReport localReport = new LocalReport();localReport.ReportPath = Server.MapPath("~/Report/WHC.UserReport.rdlc");var dt = baseBLL.GetAll();ReportDataSource reportDataSource = new ReportDataSource("DataSet1", dt);localReport.DataSources.Add(reportDataSource);
呈現的作業碼如下所示,預設我們以圖片進行展現。
Warning[] warnings;string[] streams;byte[] renderedBytes;renderedBytes = localReport.Render(reportType,deviceInfo,out mimeType,out encoding,out fileNameExtension,out streams,out warnings);return File(renderedBytes, (format.ToLower() == "image") ? "image/jpeg" : mimeType);
最終預設可以看到圖片的報表展現效果。
650) this.width=650;" src="http://images.cnitblog.com/blog/8867/201411/200936400464662.png" style="border:0px;" />
當然,我們上面還有其他功能的操作,如PDF功能的展現,這個是一個不錯的格式展現,每頁分的很好,如果在IE裡面,會獨立開啟PDF檔案;如果是Chrome瀏覽器,則會在瀏覽器裡面直接開啟,比較好。
650) this.width=650;" src="http://images.cnitblog.com/blog/8867/201411/200939239372927.png" style="border:0px;" />
當然,Excel和Word就只能下載進行查看了,因為瀏覽器並不支援直接在上面進行預覽查看的了,除非藉助其他控制項或者做法。
3、圖片內容的連續輸出
我們瞭解RDLC的話,應該知道,一般RDLC報表,它都是通過一個DeviceInfo的資訊進行展現的,如下所示是一個標準的DeviceInfo對象。
string deviceInfo ="<DeviceInfo>" +" <OutputFormat>" + deviceType + "</OutputFormat>" +" <PageWidth>8.5in</PageWidth>" +" <PageHeight>11in</PageHeight>" +" <MarginTop>0.5in</MarginTop>" +" <MarginLeft>1in</MarginLeft>" +" <MarginRight>1in</MarginRight>" +" <MarginBottom>0.5in</MarginBottom>";
但是這樣的內容,如果展現圖片的話,就只會展示一頁的內容,一般是800的高度這樣子,但是我的報表裡面可能有很多記錄,如何能夠讓它全部展現出來呢?
方法是有的,不過不是很完美,就是需要計算大概的尺寸,然後修改PageHeight的數值,讓它動態刪除最大的記錄,達到全部內容都可以輸出看到。
為了達到這個目的,我對圖片格式輸出的報表,對它的高度進行了一個簡單的計算,然後換成它的標準高度,這樣代碼如下所示。
if(format.ToLower() == "image"){double inchValue = (dt.Count / 37.0) * 11; deviceInfo += string.Format(" <PageHeight>{0}in</PageHeight>", inchValue);}else{deviceInfo += " <PageHeight>11in</PageHeight>";}
最後介面代碼如下所示。
650) this.width=650;" src="http://images.cnitblog.com/blog/8867/201411/200948324841477.png" style="border:0px;" />
650) this.width=650;" src="http://images.cnitblog.com/blog/8867/201411/200948408282528.png" style="border:0px;" />
本文出自 “伍華聰的部落格” 部落格,請務必保留此出處http://wuhuacong.blog.51cto.com/1779896/1829093
基於MVC+EasyUI的Web開發架構經驗總結(15)--在MVC項目中使用RDLC報表