This is a migrated thread and some comments may be shown as answers.

Exporting a report to file programmatically

3 Answers 820 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Simsonic
Top achievements
Rank 1
Simsonic asked on 07 Oct 2009, 11:48 AM

Requirements

RadControls version

Q2 2009
.NET version

2.0
Visual Studio version

2005/2008
programming language C#
browser support

all browsers supported by RadControls


PROJECT DESCRIPTION
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.

C#

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

3 Answers, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 07 Oct 2009, 01:39 PM
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,
Steve
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.
0
olepadre
Top achievements
Rank 1
answered on 18 Feb 2014, 09:06 PM
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.Clear()
        Response.ContentType = result.MimeType
        Response.Cache.SetCacheability(HttpCacheability.Private)
        Response.Expires = -1
        Response.Buffer = True
        Response.AddHeader("Content-Disposition", String.Format("{0};FileName=""{1}""", "attachment", fileName))
        Response.BinaryWrite(result.DocumentBytes)
        Response.End()
 
    End Sub
0
Nasko
Telerik team
answered on 21 Feb 2014, 01:09 PM
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

Regards,
Nasko
Telerik

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.

Tags
General Discussions
Asked by
Simsonic
Top achievements
Rank 1
Answers by
Steve
Telerik team
olepadre
Top achievements
Rank 1
Nasko
Telerik team
Share this question
or