fsr 发表于 2016-5-16 14:28:19

ActiveReportsWeb 到处excel报错

本帖最后由 Lenka.Guo 于 2016-5-16 14:52 编辑

ActiveReportsWeb 到处excel时报No data has been set. Please specify either a DataSet or a DataView to use SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString);
      private DataSet dataSetData;
      public DataSet DataSetData
      {

          get { return dataSetData; }

      }
      private DataSet LoadDataToDataSet()
      {

            string productSql = "select top 10 * From tb_borrowInfo";
            dataSetData = new DataSet();
            SqlCommand cmd = new SqlCommand(productSql, myConn);

            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;

            adapter.Fill(dataSetData, "tb_borrowInfo");
            return dataSetData;

      }


      protected void Page_Load(object sender, EventArgs e)
      {
               
                GrapeCity.ActiveReports.PageReport report1 = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("PageReport1.rdlx")));
                report1.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(WebViewer1_LocateDataSource1);//绑定数据
                WebViewer1.PdfExportOptions.FitWindow = false;//全屏编辑
                WebViewer1.PdfExportOptions.DisplayMode = GrapeCity.ActiveReports.Export.Pdf.Section.DisplayMode.Outlines;//显示模式
                WebViewer1.Report = report1;
      }

      private void WebViewer1_LocateDataSource1(object sender, GrapeCity.ActiveReports.LocateDataSourceEventArgs args)
      {

            args.Data = LoadDataToDataSet();


      }
      /// <summary>
      /// 导出excel
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      protected void Button1_Click(object sender, EventArgs e)
      {

            GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("PageReport1.rdlx")));
            _reportDef.Report.DataSources.DataSourceReference = "";
             string productSql = string.Format("SELECT top 10 * From tb_borrowInfo");
            _reportDef.Report.DataSets.Query.CommandText = productSql;
            GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
            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();


         
      }



Lenka.Guo 发表于 2016-5-16 14:50:56

Hi,

导出错误提示:No data has been set. Please specify either a DataSet or a DataView to use。

错误原因:在导出Excel 文件时未对报表绑定数据。


因为报表是运行时绑定数据源, 在导出Excel 文件时也是新生成报表对象,所以导出Excel 时缺少对报表绑定数据的代码。
_reportDef.Report.DataSources.DataSourceReference = "";
             string productSql = string.Format("SELECT top 10 * From tb_borrowInfo");
            _reportDef.Report.DataSets.Query.CommandText = productSql;以上代码只是为报表的DataSet 赋查询语句

因此需要在Button1_Click 方法中调用LocateDataSource 方法。代码应修改为与Page_Load 代码相同:
protected void Button1_Click(object sender, EventArgs e)
      {

            GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("PageReport1.rdlx")));
      
            GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);


_reportRuntime.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(WebViewer1_LocateDataSource1);//绑定数据
GrapeCity.ActiveReports.Export.Excel.Section.XlsExport XlsExport1 = new GrapeCity.ActiveReports.Export.Excel.Section.XlsExport();

.........
.........
.........

}

fsr 发表于 2016-5-16 15:29:48

可以了,谢谢

Lenka.Guo 发表于 2016-5-16 17:07:19

fsr 发表于 2016-5-16 15:29
可以了,谢谢

:hjyzw:
页: [1]
查看完整版本: ActiveReportsWeb 到处excel报错