gotoStudy 发表于 2016-9-27 09:59:09

activereport 导出时动态绑定数据 MVC5

本帖最后由 Lenka.Guo 于 2016-9-27 10:17 编辑

请问 在MVC5下,导出Excel,动态绑定数据源,数据源类型是object provider,改怎么做

Lenka.Guo 发表于 2016-9-27 10:16:54

MVC5中如果使用HTML5Viewer的话,不需要做特殊设置,使用html5Viewer自带的导出Excel按钮即可。
如果是WebViewer的话,运行时绑定数据源需要重写WebService文件,然后再WebServce中实现LocateDataSource方法,在这个方法中来调用导出Excel方法即可。

gotoStudy 发表于 2016-9-27 10:26:19

因为我不需要展示,只需要导出Excel,所以我没有展现页面,导出不是在展现页面,还有我用的是WebViewer

Lenka.Guo 发表于 2016-9-27 10:40:01

本帖最后由 Lenka.Guo 于 2016-9-27 10:54 编辑

gotoStudy 发表于 2016-9-27 10:26
因为我不需要展示,只需要导出Excel,所以我没有展现页面,导出不是在展现页面,还有我用的是WebViewer
您的需求可以分为以下几步完成:
首先在MVC中为报表动态绑定数据源(参考帖子:http://gcdn.gcpowertools.com.cn/showtopic-19954-1-1.html):绑定数据源成功后执行导出Excel实现思路:

[*]在工程中添加一个【Web 服务】文件,修改该类继承的原始类型为GrapeCity.ActiveReports.Web.ReportService。
[*]重写OnCreateReportHandler方法,在这个方法中根据传进来的报表路径,生成报表对象。
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;
      }


[*]调用报表对象的Document_LocateDataSource方法来绑定数据源
void Document_LocateDataSource(object sender, LocateDataSourceEventArgs args)
      {
            switch (args.Report.PageReport.Report.Description)
            {
                case "客户信息":
args.Data = GetCustomer(customerID);
<b>                  ExportToExcel(args.Report);</b>
                  break;
                case "订单信息":
                  string orderID = args.Report.Parameters.CurrentValue.ToString();
                  args.Data = GetOrder(orderID);
                  break;
                default:
                  break;
            }
      }


[*]绑定数据源成功后,直接执行导出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\\" + "\\XLSExpt.xlsx");
      }





gotoStudy 发表于 2016-9-27 11:28:05

请问 如果是windowsform环境下导出Excel,也是不需要显示界面,直接导出Excel,用运行时绑定数据,data privoder 方式,该怎么实现

Lenka.Guo 发表于 2016-9-27 11:35:35

gotoStudy 发表于 2016-9-27 11:28
请问 如果是windowsform环境下导出Excel,也是不需要显示界面,直接导出Excel,用运行时绑定数据,data pri ...

也是同样的思路,在完成LocateDataSource方法后调用导出Excel的代码,如4#

gotoStudy 发表于 2016-9-27 12:02:17

有没有windowsform下导出excel 的demo啊

Lenka.Guo 发表于 2016-9-27 12:16:56

gotoStudy 发表于 2016-9-27 12:02
有没有windowsform下导出excel 的demo啊
稍等,我给您简单做一个吧

Lenka.Guo 发表于 2016-9-27 15:59:56

Winform Demo, 供您了解:

gotoStudy 发表于 2016-9-27 16:27:04

按您给的demo导出的文件带有第一行和第一列是空的,请问有什么方法能去掉这个首行首列的空白
页: [1] 2
查看完整版本: activereport 导出时动态绑定数据 MVC5