Exporting a report to file programmatically

4 posts, 0 answers
  1. Simsonic
    Simsonic avatar
    2 posts
    Member since:
    Oct 2008

    Posted 07 Oct 2009 Link to this post


    RadControls version

    Q2 2009
    .NET version

    Visual Studio version

    programming language C#
    browser support

    all browsers supported by RadControls

    This is just a little methode wich exports a report to a file programmatically. So it is possible to export a report without using the ReportViewer.


    void ExportToFile(Telerik.Reporting.Report reportToExport, string exportFormat, string exportPath)
         ReportProcessor reportProcessor = new ReportProcessor();
         RenderingResult result = reportProcessor.RenderReport(exportFormat, reportToExport, null);

         string fileName = result.DocumentName + "." + exportFormat.ToLower();

         File.WriteAllBytes(exportPath + fileName, result.DocumentBytes);

  2. Steve
    Steve avatar
    10941 posts

    Posted 07 Oct 2009 Link to this post

    Hello Simsonic,

    There are KB articles elaborating on export and save of file to PDF, since version 2.x of Telerik Reporting:
    so there is no need for a code library and we have transferred this post to our forum section.

    Thank you for the involvement!
    All the best,
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft R3 2016 release webinar banner
  4. olepadre
    olepadre avatar
    8 posts
    Member since:
    Apr 2007

    Posted 18 Feb 2014 in reply to Steve Link to this post

    OK, forgive me, but I'm a newbie.  I have a designed a Telerik report for use in Report Viewer with the extension ".vb" and I have created a report in your Report Designer.  "trdx" imported into my project.  Both only produce a blank PDF.

    What am I supposed to use for the variable reportToExport (Telerik.Reporting.Report)
    rotected Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
            ReportViewer1.ReportSource.Parameters.Add("StartDate", Me.dtStart.SelectedDate)
            ReportViewer1.ReportSource.Parameters.Add("EndDate", Me.dtEnd.SelectedDate)
            Dim reportToExport As New Telerik.Reporting.Report
            reportToExport.DocumentName = "rpt_NICU_Results.trdx"
            Dim reportProcessor As New Telerik.Reporting.Processing.ReportProcessor()
            Dim instanceReportSource As New Telerik.Reporting.InstanceReportSource()
            instanceReportSource.ReportDocument = reportToExport
            Dim result As RenderingResult = reportProcessor.RenderReport("PDF", instanceReportSource, Nothing)
            Dim fileName As String = result.DocumentName + "." + result.Extension
            Response.ContentType = result.MimeType
            Response.Expires = -1
            Response.Buffer = True
            Response.AddHeader("Content-Disposition", String.Format("{0};FileName=""{1}""", "attachment", fileName))
        End Sub
  5. Nasko
    Nasko avatar
    909 posts

    Posted 21 Feb 2014 Link to this post

    Hello Robert,

    Note that the first two lines of your code:
    ReportViewer1.ReportSource.Parameters.Add("StartDate", Me.dtStart.SelectedDate)
    ReportViewer1.ReportSource.Parameters.Add("EndDate", Me.dtEnd.SelectedDate)
    won't affect the exported report, since it is not displayed in the report viewer.

    In order to pass parameter values to the report that is going to be exported programmatically, you should pass these values to the InstanceReportSource.Parameters collection in the following way:
    Dim instanceReportSource As New Telerik.Reporting.InstanceReportSource()
    instanceReportSource.ReportDocument = reportToExport
    isntanceReportSource.Parameters.Add("StartDate", Me.dtStart.SelectedDate)
    Dim result As RenderingResult = reportProcessor.RenderReport("PDF", instanceReportSource, Nothing)

    As for the value of the reportToExport variable, you have two options:
    1. If your report is created and stored as a class in the project e.g., MyReport.vb, you need to instantiate that class and use the report object later on:
      Dim reportToExport As Telerik.Reporting.Report = New MyReport()
      Dim instanceReportSource As New Telerik.Reporting.InstanceReportSource()
      instanceReportSource.ReportDocument = reportToExport
    2. If your report is created in the Standalone Report Designer and stored as .trdx file, you can use UriReportSource
      Dim uriReportSource As New Telerik.Reporting.UriReportSource()
      ' Specifying an URL or a file path
      uriReportSource.Uri = "rpt_NICU_Results.trdx"
      ' Adding the initial parameter values
      uriReportSource.Parameters.Add(New Telerik.Reporting.Parameter("StartDate", Me.dtStart.SelectedDate))

      or if you need to deserialize it to a report object, which you can use later in your application:
      Dim settings As New XmlReaderSettings()
      settings.IgnoreWhitespace = True
      Using xmlReader As System.Xml.XmlReader = System.Xml.XmlReader.Create("MyReport.trdx", settings)
          Dim xmlSerializer As New Telerik.Reporting.XmlSerialization.ReportXmlSerializer()
          Dim reportToExport As Telerik.Reporting.Report = DirectCast(xmlSerializer.Deserialize(xmlReader), Telerik.Reporting.Report)
      End Using


    New HTML5/JS REPORT VIEWER with MOBILE AND TOUCH SUPPORT available in Telerik Reporting Q3 2013! Get the new Reporting version from your account or download a trial.

Back to Top