報表生成器FastReport .Net如何使用ASP.NEТ MVC?

語言: CN / TW / HK

FastReport .Net是適用於Windows Forms,ASP.NET,MVC和.NET Core的全功能報表解決方案。它可以在Microsoft Visual Studio 2005-2019中使用。支援.Net Framework 2.0-4.x,.NET Core 3.0及以上版本。

在FastReport .NET 2021.1的新版本中,我們實現了對.NET 5的支援。添加了新條形碼-Deutsce Post Leitcode。將RTF轉換為報告物件的演算法已得到顯著改進。並且還添加了用於轉換數字的新功能。歡迎下載體驗。(點選下方按鈕下載)

立即點選下載FastReport.NET v2021.1最新版

Fastreport.NET線上購買價更低,專享85折起!趕緊加入購物清單吧!

在 "Medium Trust "模式下工作

這種模式被許多共享主機提供商使用。在此模式下,以下操作受到限制:

  • 報告編譯是不可能的。
  • 不可能使用MS Access資料來源。
  • 不可能使用RichObject,不可能使用一些使用WinAPI呼叫或臨時檔案(臨時檔案)的匯出過濾器。
  • 無法使用一些使用WinAPI呼叫或臨時檔案(PDF,Open Office)的匯出過濾器。
  • 根據提供者的不同,可能還有其他限制。

要在這種模式下使用報表,您需要將報表儲存為C#/VB.NET類,如 "儲存和載入報表 "部分所述。在這種情況下,不需要編譯報表。

除此之外,還需要在GAC中新增System.Windows.Forms.DataVisualization.dll程式集。這個程式集是Microsoft Chart Control的一部分,在FastReport中用於繪製圖表。請諮詢您的共享託管提供商關於將此程式集新增到 GAC 中的問題。

在Web Farm和Web Garden架構中工作 

要在多伺服器(Web Farm)或多處理器(Web Garden)架構中使用 FastReport 報表生成器,還需要為 WebReport 物件之間的資料同步建立特殊儲存。
在配置檔案web.config中新增以下行。

<appSettings>
<add key="FastReportStoragePath" value="\FS/WebReport_Exchange"/>
<add key="FastReportStorageTimeout" value="10"/>
<add key="FastReportStorageCleanup "value="1/>
</appSettings>

- FastReportStoragePath:在多伺服器架構中工作時,臨時檔案資料夾的路徑,每臺伺服器必須能夠訪問這個資料夾。
- FastReportStorageTimeout:報告的快取時間,以分鐘為單位。
- FastReportStorageCleanup:檢查過期快取條目的時間,以分鐘為單位。
通過URL檢查配置是否正確。
http://yoursite/app_folder/FastReport.Export.axd
你應該看到 "Cluster mode: ON".

使用ASP.NEТ MVC

當您在ASPX(MVC 2)中使用WebReport時,您不會有任何問題--只需要將控制元件從工具箱中拖到頁面上。WebReport會自動對web.config進行所有必要的修改。讓我們來看一個ASPX中的WebReport演示,可以在資料夾 \Demos\C#\MvcDemo中找到。

要在Razor(MVC 3,4)中使用WebReport,你需要在web應用程式根目錄下的web.config檔案中新增一行處理程式定義。

在<system.web> <httpHandlers>部分新增這一行,以便與IIS6一起使用。

<add path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers. WebExport" />

並在<system.webServer><handlers>部分新增這一行,以便與IIS7一起使用。

<add name="FastReportHandler" path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers.WebExport" />

然後修改包含Views資料夾中的web.config檔案。在section<system.web.webPages.razor> <namespaces>中新增這些行。

<add namespace="FastReport" />
<add namespace="FastReport.Web" />

在檔案_Layout.cshtml的<head>標籤中新增這幾行。

@WebReportGlobals.Scripts()
@WebReportGlobals.Styles()

現在你可以在檢視上繪製報表了。進入控制器並建立一個WebReport。

WebReport webReport = new WebReport(); // 建立物件
webReport.Width = 600; //設定寬度
webReport.Height = 800; //設定高度
webReport.Report.RegisterData(dataSet, "AppData"); // 資料繫結
webReport.ReportFile = this.Server.MapPath("~/App_Data/report.frx"); //從檔案中載入報表
ViewBag.WebReport = webReport; //傳送物件到View上

進入 "檢視",新增這一行:

@ViewBag.WebReport.GetHtml()

類似的建立WebReport的程式碼你也可以直接在View中寫。

我們來看一下Razor中WebReport的演示,資料夾為\Demos\C#\MvcRazor。有各種載入到報表中的樣本,包括預先準備的,還有一個使用StartReport事件的例子。

不要忘記在bin目錄下新增缺少的dll。

MVC中的匯出示例

當將FastReport.Net與ASP.Net MVC框架一起使用時,有一個簡單的方法可以在HTML表單上按按鈕建立任何支援格式的檔案。
在檢視中新增以下程式碼。

@using (Html.BeginForm("GetFile", "Home"))
{
<input id="pdf" type="submit" value="Export to PDF" />
}

- GetFile : 控制器處理程式的名稱
- Home:控制器名稱(例如:HomeController.cs)

在控制器中新增名稱空間。

using FastReport.Export.Pdf;

在控制器中新增方法GetFile。

public FileResult GetFile()
{
WebReport webReport = new WebReport();
// bind data
System.Data.DataSet dataSet = new System.Data.DataSet();
dataSet.ReadXml(report_path + "nwind.xml");
webReport.Report.RegisterData(dataSet, "NorthWind");
// load report
webReport.ReportFile = this.Server.MapPath("~/App_Data/report.frx");
// prepare report
webReport.Report.Prepare();
// save file in stream
Stream stream = new MemoryStream();
webReport.Report.Export(new PDFExport(), stream);
stream.Position = 0;
// return stream in browser
return File(stream, "application/zip", "report.pdf");
}

Excel 2007的例子。

using FastReport.Export.OoXML;
...
webReport.Report.Export(new Excel2007Export(), stream);
...
return File(stream, "application/xlsx", "report.xlsx");

FastReport.Net和jQuery

FastReport.Net的WebReport物件使用了jQuery庫。你可能已經在你的專案中使用了這個庫。

為了避免在客戶端瀏覽器中重複使用jQuery引導指令碼和樣式,當使用markup Razor時,你必須在_Layout.cshtml中使用以下行。

@WebReportGlobals.ScriptsWOjQuery()
@WebReportGlobals.StylesWOjQuery()

替換這些行,其中包括所有jQuery檔案:

@WebReportGlobals.Scripts()
@WebReportGlobals.Styles()

當使用ASPX標記時,您必須設定ExternalJquery = true(預設為false)