ActiveReports中如何在后台导出运行时绑定数据源报表

时间:2023-12-20 10:53:26

ActiveReports支持运行时绑定数据源功能,这种绑定数据源方法使用较为普及,然而很多系统中都需要在后台导出报表文件,所以用户就很困惑,ActiveReports中如何在后台导出运行时绑定数据源报表?到底是怎样的逻辑?

这篇文章就主要讲解了在MVC中导出运行时数据源的报表文件。

1. 新建MVC 工程

2. 在Index.cshtml 中初始化HTML5Viewer

<div>
<div id="viewerContainer" style="width:100%;height:800px;border:1px solid gray;margin-top:20px;"></div>
</div>
<script type="text/javascript">
$(function () {
var viewer = GrapeCity.ActiveReports.Viewer(
{
element: '#viewerContainer',
report: {
id: "客户信息.rdlx" },
reportService: {
url: 'ActiveReportsService.asmx'
},
uiType: 'desktop'
});
});
</script>

3. 新建报表文件【客户信息.rdlx】,并设置数据源为Object Provider

ActiveReports中如何在后台导出运行时绑定数据源报表

添加数据集,设置数据集字段

ActiveReports中如何在后台导出运行时绑定数据源报表

3. 新建Web服务文件,继承GrapeCity.ActiveReports.Web.ReportService

重写OnCreateReportHandler方法,实现LocateDataSource方法

  protected override object OnCreateReportHandler(string reportPath)
{
var instance = base.OnCreateReportHandler(reportPath);
var pageReport = instance as PageReport;
if (pageReport != null)
{
pageReport.Document.LocateDataSource += Document_LocateDataSource;
}
return instance;
}

4. 在LocateDataSource中调用导出Excel函数

 void Document_LocateDataSource(object sender, LocateDataSourceEventArgs args)
{ string customerID = args.Report.Parameters[0].CurrentValue.ToString();
args.Data = GetCustomer(customerID);
ExportToExcel(args.Report); }

5. 实现导出Excel方法

private void ExportToExcel(PageDocument report)
{ GrapeCity.ActiveReports.Export.Excel.Section.XlsExport xlsExport1 = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport();
xlsExport1.FileFormat = GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xlsx;
xlsExport1.Export(report, @"D:\Demo\\" + "\\XLS1t.xlsx");
}

Demo下载: