ASP.NET MVC中,页面报表通过LocateDataSource事件动态设置数据源,怎么把Matrix显示的数据导出到Excel中?我的代码如下,有错误,异常信息是:发生了一个意外错误。更多信息: 'No data has been set. Please specify either a DataSet or a DataView to use'
- /// <summary>
- /// 有问题
- /// </summary>
- /// <param name="reportName"></param>
- /// <returns></returns>
- public ActionResult ExportToExcel(string reportName)
- {
- DataTable dt = (DataTable)Session["DataTable"];
- string pathName = "~/ActiveReports/";
- string fileName = pathName + reportName;
- FileInfo fileInfo = new FileInfo(Server.MapPath(fileName));
- PageReport report = new PageReport(fileInfo);
- report = (PageReport)Session["Report"];
-
- GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(report);
- GrapeCity.ActiveReports.Export.Excel.Section.XlsExport XlsExport1 = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport();
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- XlsExport1.FileFormat = GrapeCity.ActiveReports.Export.Excel.Section.FileFormat.Xlsx;
- XlsExport1.Export(_reportRuntime, ms);
- Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
- Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.xlsx"));
- Response.BinaryWrite(ms.ToArray());
- Response.End();
- return Json("");
- }
- private DataTable dataTable;
- public ActionResult GetActiveReport(string reportName, ReportSearchConditionViewModel condition)
- {
- dataTable = WorkFileRepository.GetWorkFileDataTable(condition);
- string pathName = "~/ActiveReports/";
- string fileName = pathName + reportName;
- FileInfo fileInfo = new FileInfo(Server.MapPath(fileName));
- PageReport report = new PageReport(fileInfo);
- // 创建并设置数据源
- DataSource dataSource = new DataSource();
- dataSource.Name = "DataSource1";
- dataSource.ConnectionProperties.DataProvider = "DATASET";
- // 设置数据集
- GrapeCity.ActiveReports.PageReportModel.DataSet dataSet =
- new GrapeCity.ActiveReports.PageReportModel.DataSet();
- Query query = new Query();
- dataSet.Name = "DataSet1";
- dataSet.Query = query;
- query.DataSourceName = "DataSource1";
- // 添加字段
- foreach (DataColumn dc in dataTable.Columns)
- {
- Field field = new Field(dc.ColumnName, dc.ColumnName, null);
- dataSet.Fields.Add(field);
- }
- // 将数据源和数据集绑定到报表中
- report.Report.DataSources.Add(dataSource);
- report.Report.DataSets.Add(dataSet);
- report.Document.LocateDataSource += new LocateDataSourceEventHandler(Document_LocateDataSource);
- Session["Report"] = report;
- return PartialView("WebViewer", report);
- }
- private void Document_LocateDataSource(Object sender, LocateDataSourceEventArgs args)
- {
- args.Data = dataTable;
- }
复制代码 |
|