找回密码
 立即注册

QQ登录

只需一步,快速开始

aj510105

注册会员

4

主题

15

帖子

130

积分

注册会员

积分
130

活字格认证

最新发帖
aj510105
注册会员   /  发表于:2015-6-24 16:53  /   查看:12265  /  回复:16
查看了下面地址绑定数据源的方法,还是有些疑问,望解答。
http://blog.gcpowertools.com.cn/ ... ime-DataSource.aspx

以下代码只能放到Page_Load事件下,才能被正确执行
report1.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(Document_LocateDataSource);

如果放在btn1_Click事件下,就不能正确执行,通过代码跟踪,发现没有运行Document_LocateDataSource方法,而是直接跳过了。

请问,如果我要导出报表,如何动态绑定数据源,实现和下面代码类似的效果,我的数据库是MSSql,希望能通过绑定dataset或者datatable的方式来传递数据
            string report = "rptInvoice";
            GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("~/Reports/" + report + ".rdlx")));
            _reportDef.Report.DataSources[0].DataSourceReference = "";
            _reportDef.Report.DataSources[0].ConnectionProperties.DataProvider = "OLEDB";
            _reportDef.Report.DataSources[0].ConnectionProperties.ConnectString = string.Format(&quotrovider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", Server.MapPath("~/Data/NWind_CHS.mdb"));

16 个回复

倒序浏览
aj510105
注册会员   /  发表于:2015-6-24 17:28:00
沙发
补充代码

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            // Provide the page report you want to render.
            GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("/Reports/PageReport1.rdlx")));
            _reportDef.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(Document_LocateDataSource);

            GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);

            // Create an output directory
            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            // Provide settings for your rendering output.
            GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings
            excelSetting = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings();
            excelSetting.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xlsx;
            excelSetting.MultiSheet = false;
            GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = excelSetting;

            //Set the rendering extension and render the report.
            GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension
            excelRenderingExtension = new
            GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension();
            GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
            _reportRuntime.Render(excelRenderingExtension, outputProvider, excelSetting.GetSettings());

            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("content-disposition", "inline;filename=自定义.xlsx");
            outputProvider.GetPrimaryStream().OpenStream().CopyTo(ms);
            Response.BinaryWrite(ms.ToArray());
            Response.End();
        }
回复 使用道具 举报
frank.zhang
社区贡献组   /  发表于:2015-6-24 17:30:00
板凳
您好,
没有运行LocateDataSource方法,需要确定报表是否使用DataSet provider数据源。

使用MS SQL 需要先将数据查询出来,通过DataTable的方式返回给报表。
不需要设置_reportDef.Report.DataSources相关代码。

如何从数据库查询,并返回datatable,需要您自己实现。
回复 使用道具 举报
aj510105
注册会员   /  发表于:2015-6-24 17:41:00
地板
protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }
            GrapeCity.ActiveReports.PageReport report1 = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("/Reports/PageReport1.rdlx")));
            report1.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(Document_LocateDataSource);
            WebViewer1.PdfExportOptions.FitWindow = false;
            WebViewer1.PdfExportOptions.DisplayMode = GrapeCity.ActiveReports.Export.Pdf.Section.DisplayMode.Outlines;
            WebViewer1.FlashViewerOptions.ShowSplitter = false;
            WebViewer1.Report = report1;
        }

        void Document_LocateDataSource(object sender, GrapeCity.ActiveReports.LocateDataSourceEventArgs args)
        {
            if (args.DataSourceName == "DataSource1")
            {
                if (args.DataSetName == "DataSet1")
                {
                    args.Data = GetData();
                }
            }
        }
private DataTable GetData()
        {
            代码省略……
            return dt;
        }

通过上面的方法,显示报表内容没有问题,但是导出的时候就不能运行LocateDataSource方法。可以肯定是使用了DatasetProvider数据源

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 使用道具 举报
frank.zhang
社区贡献组   /  发表于:2015-6-24 18:01:00
5#
您好,
在web中绑定的方式需要使用onlocatedatasource="WebViewer1_LocateDataSource"。

您可以参考以下例子程序:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复 使用道具 举报
aj510105
注册会员   /  发表于:2015-6-25 10:44:00
6#
看了您发的例子
在web中绑定的方式需要使用onlocatedatasource="WebViewer1_LocateDataSource"。
按照以上方法,可以在查询按钮中运行WebViewer1_LocateDataSource方法,动态传递datatable。测试成功。
但是要在客户端代码上绑定onlocatedatasource才能触发。

但如果是在导出excel的时候,如何绑定onlocatedatasource方法,将数据传递给report呢
回复 使用道具 举报
frank.zhang
社区贡献组   /  发表于:2015-6-25 10:58:00
7#
您好,
在WebViewer1_LocateDataSource使用
  1. args.Report.Parameters[0].CurrentValue.ToString();
复制代码
可以拿到参数。

导出的时候,通过拼SQL语句,进行查询。
希望可以帮助到您。
回复 使用道具 举报
aj510105
注册会员   /  发表于:2015-6-25 11:15:00
8#
不好意思,刚才可能我描述得不太准确

在显示报表内容的时候可以通过绑定WebView事件onlocatedatasource="WebViewer1_LocateDataSource"。
进入到WebViewer1_LocateDataSource方法获取数据。
但是在导出excel的时候,如何实现类似方法获取数据呢。
前面的代码中:
protected void btnExcel_Click(object sender, EventArgs e)
        {
            // Provide the page report you want to render.
            GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath("/Reports/PageReport1.rdlx")));
            _reportDef.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(Document_LocateDataSource);
经测试是没有运行到Document_LocateDataSource方法的。

您提到的
在WebViewer1_LocateDataSource使用
args.Report.Parameters[0].CurrentValue.ToString();
复制代码
可以拿到参数。

导出的时候,需要在WebViewer1_LocateDataSource方法中添加这条语句
再调用WebViewer1_LocateDataSource方法对吗
这里不是太明白。请问能否提供一个简单的例子,谢谢
回复 使用道具 举报
aj510105
注册会员   /  发表于:2015-6-25 11:23:00
9#
导出的时候怎么进入到WebViewer1_LocateDataSource方法中
回复 使用道具 举报
frank.zhang
社区贡献组   /  发表于:2015-6-25 11:32:00
10#
您好,
导出的时候,参数已经拿到了,可以不进入WebViewer1_LocateDataSource。

我提供一个动态加载报表,并且可以导出excel的例子程序。您可以参考下,看是否满足您的需求。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

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