yxx7347 发表于 2017-12-13 12:46:12

运行时绑定数据的rdl报表导出pdf时出现错误



下列导出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();
    }



KearneyKang 发表于 2017-12-13 13:40:25

您好这是导出PDF的后台代码,您参考下// Provide the page report you want to render.
System.IO.FileInfo rptPath = new System.IO.FileInfo(@"..\..\PageReport1.rdlx");
GrapeCity.ActiveReports.PageReport pageReport = new GrapeCity.ActiveReports.PageReport(rptPath);

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

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

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

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

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

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;
    }



但在导出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并且绑定动态数据源的代码
private void tsbExcel_Click(object sender, EventArgs e)
      {
            string file_name = @"..\..\RdlReport1.rdlx";
            //GrapeCity.ActiveReports.PageReport report = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(@"123.rdlx"));
            //GrapeCity.ActiveReports.Document.PageDocument reportDocument = new GrapeCity.ActiveReports.Document.PageDocument(report);
            GrapeCity.ActiveReports.PageReport pageReport = new GrapeCity.ActiveReports.PageReport(new System.IO.FileInfo(file_name));
            pageReport.Document.LocateDataSource += new LocateDataSourceEventHandler(LocateData);
            this._document = pageReport.Document;
         
            System.IO.DirectoryInfo outputDirectory = new System.IO.DirectoryInfo(@"D:\MyExcel");
            outputDirectory.Create();
         
            GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings excelSetting = new GrapeCity.ActiveReports.Export.Excel.Page.ExcelRenderingExtensionSettings();
            excelSetting.FileFormat = GrapeCity.ActiveReports.Export.Excel.Page.FileFormat.Xls;
            excelSetting.MultiSheet = false;         
            GrapeCity.ActiveReports.Extensibility.Rendering.ISettings setting = excelSetting;

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

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

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

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

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

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

reportDocument.Render(wordRenderingExtension, outputProvider, wordSetting);         



yxx7347 发表于 2017-12-13 17:20:40

另外,关于区域报表导出下载的示例代码有吗?

yxx7347 发表于 2017-12-13 17:23:37

总感觉activereports的编程文档资源较为零散,有完整的文档吗?英文的也行

KearneyKang 发表于 2017-12-14 09:07:27

您好!这里有一个AR的产品白皮书,这是一个英文的文档。里面有比较详细的介绍。
http://gcdn.gcpowertools.com.cn/forum.php?mod=viewthread&tid=42506&extra=page%3D1
页: [1]
查看完整版本: 运行时绑定数据的rdl报表导出pdf时出现错误