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.
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.
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.
Subscribe to be the first to get our expert-written articles and tutorials for developers!