Telerik blogs

As many of you know, Telerik Reporting is incredibly versatile. Using it, you can create a single report, then take that one report and display it in Silverlight, WPF, WinForms, or ASP.NET using one of our four available report viewers. Recently, however, I've noticed that people are interested in exporting reports without even displaying a report viewer at all. I am happy to let you know that Telerik Reporting indeed supports this exact scenario. I suppose you could consider it a "5th" way of viewing reports if you wanted.

Setting up Telerik Reporting to export reports programmatically is actually quite easy. If you've done your homework by reading the documentation, you are probably well aware of the section entitled, Exporting Report Programmatically. It is indeed this section of the documentation that describes exactly what I'm going to show you below.

Here is the code for exporting a report programmatically from within a WinForms application.

private void button1_Click(object sender, EventArgs e)
{
    using (SaveFileDialog dlg = new SaveFileDialog())
    {
        if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            ReportLibrary.ProductsReport report = new ReportLibrary.ProductsReport();
            Telerik.Reporting.Processing.ReportProcessor reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
            Telerik.Reporting.Processing.RenderingResult renderingResult = reportProcessor.RenderReport("PDF", report, null);

            FileStream fs = new FileStream(dlg.FileName, FileMode.Create);
            fs.Write(renderingResult.DocumentBytes, 0, renderingResult.DocumentBytes.Length);
            fs.Close();
        }
    }          
}

As you can see, there are two lines of code that are essential to making this work. The first is the line that instantiates a ReportProcessor object. This object is responsible for rendering the report. The next essential line is the one that calls RenderReport(). As you can see, this method actually takes a string value of the renderer you want to use. Currently, PDF is specified, but you could actually change that value to any of those you see listed in the Rendering Extensions section of the documentation. Once the report has been rendered, you can use a FileStream to simply output it to a file on the local file system.

In addition to everything I've said above, since you have access to the bytes that make up the report, you could technically return the generated document as part of a response to a web request. For example, in the code I've provided below, I am returning a report in response to a call to an ASP.NET MVC controller action.

public ActionResult Schedule(int id, string format)
{
    Conference conference = _contextMgr.ConferenceRepository.Select().Where(c => c.ID == id).FirstOrDefault();
    if (conference == null) return View("Error", "Conference not found.");
 
    EventTracker.ReportLibrary.EventSchedule report = new ReportLibrary.EventSchedule();
    report.ReportParameters["ConferenceID"].Value = id;
 
    Telerik.Reporting.Processing.ReportProcessor reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
    Telerik.Reporting.Processing.RenderingResult renderingResult = reportProcessor.RenderReport(format, report, null);
 
    MemoryStream ms = new MemoryStream();
    ms.Write(renderingResult.DocumentBytes, 0, renderingResult.DocumentBytes.Length);
    ms.Flush();
 
    FileContentResult result = new FileContentResult(ms.GetBuffer(), renderingResult.MimeType);
    result.FileDownloadName = conference.Name + " Schedule." + format.ToLower();
 
    return result;
}

Click here to download the WinForms example source code.

Click here to download the source code to EventTracker. EventTracker is a suite of applications we demonstrated during the Q1 2011 Integration Webinar. It makes use of the MVC code I provided above (located in ConferencesController/Schedule action). The video is currently being edited, and will be available on Telerik TV soon.


Comments

Comments are disabled in preview mode.