【FastReport教程】在Web上顯示準備好的報表
FastReport.Net 2018.4版本涉及到網絡報表。現在,用戶可以以fpx格式顯示報表,即預先準備好的報表。fpx格式非常便于交換報表,因為它包含模板之外的數據。因此,要以fpx格式顯示報表,您根本不需要連接到數據源,這樣就消除了數據庫位于遠程服務器上時的問題。與收到數據有關的報表的編制不會有任何延誤。擁有此格式的報表,您可能希望在網頁上顯示它們。
讓我們創建一個空的ASP.Net MVC項目。 在Reference中,我們添加了FastReport.dll和FastReport.Web.dll庫,可在此處獲取: C:\ Program Files(x86)\ FastReports \ FastReport.Net \ Framework 4.0。 添加MVC 5 ViewPage(Razor)視圖。我們稱之為索引。這是它的默認內容:
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title></title> </head> <body> <div> </div> </body> </html>
添加WebReport對象。當然,我們可以在控制器中創建它。但是,您可以在視圖中創建它。
@{ Layout = null; } @{ // FastReport .Net prepared report preview example. FastReport.Web.WebReport webReport = new FastReport.Web.WebReport(true, true); webReport.ToolbarIconsStyle = FastReport.Web.ToolbarIconsStyle.Black; webReport.ToolbarBackgroundStyle = FastReport.Web.ToolbarBackgroundStyle.None; webReport.ToolbarStyle = FastReport.Web.ToolbarStyle.Large; webReport.ToolbarColor = System.Drawing.Color.White; webReport.BorderWidth = 1; webReport.BorderColor = System.Drawing.Color.Black; webReport.ShowZoomButton = false; webReport.ShowExports = false; webReport.PrintInBrowser = false; webReport.XlsxPrintFitPage = true; webReport.LoadPrepared(Server.MapPath("~/App_Data/Prepared.fpx")); } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>FastReport Prepared Report Preview</title> </head> <body> @webReport.GetHtml() </body> </html>
此外,我們以HTML格式添加了報表的標題和輸出,讓我們仔細看看我們創建的webReport對象的設置:
- ToolbarIconsStyle - 最上面的Web報表工具欄上的圖標樣式:黑色,藍色,自定義,綠色,紅色;
- ToolbarBackgroundStyle - Web報表工具欄的背景樣式:Custome,Dark,Light,Medium,None;
- ToolbarStyle - 在Web報表工具欄上顯示按鈕的樣式:大或小;
- ToolbarColor - 工具欄背景顏色;
- BorderWidth - Web報表框架的寬度;
- BorderColor - Web報表框架顏色;
- ShowZoomButton - 顯示縮放按鈕;
- ShowExports - 顯示導出菜單;
- PrintInBrowser - 允許從瀏覽器打印;
- XlsxPrintFitPage - 在導出到Excel 2007時,在一個頁面上啟用報表打印。
要顯示Web響應,我們需要導出到html。因此,在Web.config中我們添加處理程序:
<system.webServer> <handlers> <add name="FastReportHandler" path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers.WebExport"/> </handlers> </system.webServer>
讓我們運行應用程序:
它看起來像一個常規的網絡報表,這是一份fpx格式的預先準備的報表,現在我們可以使用frx和fpx報表。
上面的示例顯示了如何直接從視圖中使用fpx報表,但如果您更習慣于在控制器中使用邏輯,則使用ViewBag將報表從控制器傳輸到視圖。