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

发布时间:2016/09/27 00:09 发布者:lenka.guo

返回博客中心

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

image

 

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

image

 

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下载:

 

 

您在使用产品过程中有任何疑问,可以登录葡萄城开发者社区和经验丰富的技术工程师、ActiveReports开发人员交流:了解更多

了解ActiveReports产品更多特性:

/developer/activereports

下载产品体验产品功能:

/download/?pid=16

标签: HTML5Viewer , 运行时数据绑定 , ActiveReports 10 , 多报表 , 报表


关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网