找回密码
 立即注册

QQ登录

只需一步,快速开始

fsr

注册会员

12

主题

26

帖子

172

积分

注册会员

积分
172

微信认证勋章

fsr
注册会员   /  发表于:2016-5-16 14:28  /   查看:3066  /  回复:3
本帖最后由 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[0].DataSourceReference = "";
             string productSql = string.Format("SELECT top 10 * From tb_borrowInfo");
            _reportDef.Report.DataSets[0].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();


         
        }



3 个回复

倒序浏览
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 时缺少对报表绑定数据的代码。
  1. _reportDef.Report.DataSources[0].DataSourceReference = "";
  2.              string productSql = string.Format("SELECT top 10 * From tb_borrowInfo");
  3.             _reportDef.Report.DataSets[0].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
地板

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部