找回密码
 立即注册

QQ登录

只需一步,快速开始

yxx7347

初级会员

7

主题

24

帖子

233

积分

初级会员

积分
233

微信认证勋章

yxx7347
初级会员   /  发表于:2017-12-13 12:46  /   查看:3626  /  回复:6


下列导出pdf的代码运行到红色语句报错:无法连接数据源。

现已确认:1.Document_LocateDataSource没有问题(在同一页面下webviewer显示该报表正常)
                 2.内部已绑定数据的rdlx报表导出pdf正常。

请问如何解决?


protected void ButtonExportPDF_Click(object sender, EventArgs e)
    {
        GrapeCity.ActiveReports.PageReport _reportDef = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath(@"/Reports/RdlReport_SBTZ.rdlx")));
        _reportDef.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(Document_LocateDataSource);
        GrapeCity.ActiveReports.Document.PageDocument _reportRuntime = new GrapeCity.ActiveReports.Document.PageDocument(_reportDef);
        GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension _renderingExtension = new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();
        GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider _provider = new GrapeCity.ActiveReports.Rendering.IO.MemoryStreamProvider();
       _reportRuntime.Render(_renderingExtension, _provider);
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", Server.UrlPathEncode("attachment;filename=客户订单.pdf"));
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        _provider.GetPrimaryStream().OpenStream().CopyTo(ms);
        Response.BinaryWrite(ms.ToArray());
        Response.End();
    }



6 个回复

倒序浏览
KearneyKang讲师达人认证 悬赏达人认证
超级版主   /  发表于:2017-12-13 13:40:25
沙发
您好这是导出PDF的后台代码,您参考下
  1. // Provide the page report you want to render.
  2. System.IO.FileInfo rptPath = new System.IO.FileInfo(@"..\..\PageReport1.rdlx");
  3. GrapeCity.ActiveReports.PageReport pageReport = new GrapeCity.ActiveReports.PageReport(rptPath);

  4. // Create an output directory.
  5. System.IO.DirectoryInfo outputDirectory = new System.IO.DirectoryInfo(@"C:\MyPDF");
  6. outputDirectory.Create();

  7. // Provide settings for your rendering output.
  8. GrapeCity.ActiveReports.Export.Pdf.Page.Settings pdfSetting = new GrapeCity.ActiveReports.Export.Pdf.Page.Settings();

  9. // Reduce the report size and report generation time.
  10. pdfSetting.OptimizeStatic = true;

  11. // Set the rendering extension and render the report.
  12. GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension pdfRenderingExtension = new GrapeCity.ActiveReports.Export.Pdf.Page.PdfRenderingExtension();
  13. GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(outputDirectory, System.IO.Path.GetFileNameWithoutExtension(outputDirectory.Name));

  14. // Overwrite output file if it already exists
  15. outputProvider.OverwriteOutputFile = true;

  16. pageReport.Document.Render(pdfRenderingExtension, outputProvider, pdfSetting);
复制代码


回复 使用道具 举报
yxx7347
初级会员   /  发表于:2017-12-13 16:40:37
板凳
您给的这段代码好像也没有涉及绑定外部数据源的语句。
我现在需要导出并下载PDF的报表是一个rdl报表,该报表用下列代码实现了数据绑定和在webviewer显示。其中Document_LocateDataSource()实现了绑定外部dataset。

    private void BuildReport()
    {

                 GrapeCity.ActiveReports.PageReport rptSBTZ = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(Server.MapPath(@"/Reports/RdlReport_SBTZ.rdlx")));
                rptSBTZ.Document.LocateDataSource += new GrapeCity.ActiveReports.LocateDataSourceEventHandler(Document_LocateDataSource);
                WebViewer1.Report = rptSBTZ;

    }

    private void Document_LocateDataSource(object sender,GrapeCity.ActiveReports.LocateDataSourceEventArgs Args)
    {
        if (Args.DataSourceName == "DataSource1")
        {
            if (Args.DataSetName == "DataSet1")
            {
                Args.Data = GetData();
            }
        }
    }

    private object GetData()
    {
        string ReportSQL = ViewState["ReportSQL"].ToString();
        string ReportBy = CheckAccess_Login1.UserName;
        string ReportDate = string.Format("{0:yyyy-MM-dd}", DateTime.Now);
        DataSet ds = BLL.SQLDBTools.GetDataBySQL(ReportSQL);
        return ds.Tables[0];
    }



但在导出pdf时好像并没有进行数据绑定。

除了下载PDF,我还要下载excel和Word文档。AR11有关于这部分操作的详细文档吗?

1楼代码参考的是:http://blog.gcpowertools.com.cn/post/ActiveReports-Export.aspx
本楼上述代码参考的视频教程。





回复 使用道具 举报
KearneyKang讲师达人认证 悬赏达人认证
超级版主   /  发表于:2017-12-13 17:02:38
地板
本帖最后由 KearneyKang 于 2017-12-13 17:07 编辑

您好,数据源的绑定跟您其它动态数据源绑定一样。如下是导出Excel并且绑定动态数据源的代码
  1. private void tsbExcel_Click(object sender, EventArgs e)
  2.         {
  3.             string file_name = @"..\..\RdlReport1.rdlx";
  4.             //GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(@"123.rdlx"));
  5.             //GrapeCity.ActiveReports.Document.PageDocument reportDocument = new GrapeCity.ActiveReports.Document.PageDocument(report);
  6.             GrapeCity.ActiveReports.PageReport pageReport = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(file_name));
  7.             pageReport.Document.LocateDataSource += new LocateDataSourceEventHandler(LocateData);
  8.             this._document = pageReport.Document;
  9.          
  10.             System.IO.DirectoryInfo outputDirectory = new System.IO.DirectoryInfo(@"D:\MyExcel");
  11.             outputDirectory.Create();
  12.          
  13.             GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings excelSetting = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings();
  14.             excelSetting.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xls;
  15.             excelSetting.MultiSheet = false;         
  16.             GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = excelSetting;

  17.             GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension excelRenderingExtension = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtension();
  18.             GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(outputDirectory, System.IO.Path.GetFileNameWithoutExtension(outputDirectory.Name));
  19.             outputProvider.OverwriteOutputFile = false;
  20.             _document.Render(excelRenderingExtension, outputProvider, excelSetting.GetSettings());
  21.         }
复制代码
导出word的代码
  1. // Provide the page report you want to render.
  2. GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport();
  3. GrapeCity.ActiveReports.Document.PageDocument reportDocument = new GrapeCity.ActiveReports.Document.PageDocument(report);

  4. // Create an output directory.
  5. System.IO.DirectoryInfo outputDirectory = new System.IO.DirectoryInfo(@"C:\MyWord");
  6. outputDirectory.Create();

  7. // Provide settings for your rendering output.
  8. GrapeCity.ActiveReports.Export.Word.Page.Settings wordSetting = new GrapeCity.ActiveReports.Export.Word.Page.Settings();

  9. // Set the FileFormat property to .OOXML.
  10. wordSetting.FileFormat = GrapeCity.ActiveReports.Export.Word.Page.FileFormat.OOXML;

  11. // Set the rendering extension and render the report.
  12. GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension wordRenderingExtension = new GrapeCity.ActiveReports.Export.Word.Page.WordRenderingExtension();
  13. GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider outputProvider = new GrapeCity.ActiveReports.Rendering.IO.FileStreamProvider(outputDirectory, System.IO.Path.GetFileNameWithoutExtension(outputDirectory.Name));

  14. // Overwrite output file if it already exists.
  15. outputProvider.OverwriteOutputFile = true;

  16. reportDocument.Render(wordRenderingExtension, outputProvider, wordSetting);           
复制代码


复制代码

回复 使用道具 举报
yxx7347
初级会员   /  发表于:2017-12-13 17:20:40
5#
另外,关于区域报表导出下载的示例代码有吗?
回复 使用道具 举报
yxx7347
初级会员   /  发表于:2017-12-13 17:23:37
6#
总感觉activereports的编程文档资源较为零散,有完整的文档吗?英文的也行
回复 使用道具 举报
KearneyKang讲师达人认证 悬赏达人认证
超级版主   /  发表于:2017-12-14 09:07:27
7#
您好!这里有一个AR的产品白皮书,这是一个英文的文档。里面有比较详细的介绍。
http://gcdn.gcpowertools.com.cn/ ... &extra=page%3D1
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部