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转换为报告对象的算法已得到显着改进。并且还添加了用于转换数字的新功能。
在 "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检查配置是否正确。
你应该看到 “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> 部分添加这一行,以便与IIS6一起使用。
并在
<add name="FastReportHandler" path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers.WebExport" />
然后修改包含Views文件夹中的web.config文件。在section<system.web.webPages.razor> 中添加这些行。
<add namespace="FastReport" />
<add namespace="FastReport.Web" />
在文件_Layout.cshtml的标签中添加这几行。
@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)。