Export .NET MSChart to Excel/PDF Using Report Viewer 2010

This article will demonstrate the approach to use the report viewer control to act as a medium to export image,web form in PDF/Excel. This will help us to do away with third party control to achieve our end objectives.

Table of Contents

The business requirement to have reports in Microsoft Office tool has always been a challenge and a need. There is no clear cut solution so as to have Excel, PDF or Word component from Microsoft ASP.NET toolkit to be used as a control. To fulfill our needm we’ve an option to use Microsoft reporting services such as Crystal report or report viewer control where we can create a report using these tools and can easily have all export or print features in build.

But what if we already spend a considerable amount of cost and effort in creating reports which do not have these export features. In our case, it happened so we already created the reports in primitive ASP.NET web pages using gridview, repeater and normal HTML controls. These reporting web pages consist of Microsoft .NET charting components, images and data in tables, etc. Now we got a requirement from the client to provide a feature to export these HTML reports in Excel, Word and PDF. We tried all options available online to render the .NET chart controls into Excel but all were in vain. Finally we thought of making use of Report viewer control redistributable 2010 but again to redevelop all the web pages/code components and migrating it into report viewer involves effort. We were running out of time and we thought of an approach of using report viewer EnableExternalImage property.

Add one sample image in solution and give the reference of report parameter in its function expression. Also select external Image. Refer Fig Note: This is where we create a binding- bridge between aspx source image(path) to report viewer Image Control. Using report parameter, we pass file path details from aspx and image gets embedded into report viewer’s image control container.

Select Image source external for given Image control.

Assign the report parameter path in function expression.

Moving on to Code Behind. It’s time to write few lines of the code where we set report parameter and invoke report viewer rendering engine. Write the following code under page_load.

If you have Microsoft chart component and you want to get this exported into Excel, PDF and Word, use the below code. Save the Microsoft .NET chart object in memory or some server path. Pass this path as input parameter values to report parameter of report viewer control.

The above implementation approach renders the content in report viewer and end users need to select options to export PDF, Word or Excel. We can twig the above code and include the below snippet to directly open up the PDF /Word or Excel. Check out the sample code below to do this: