Click here to Skip to main content
15,746,813 members
Articles / Web Development
Posted 2 Oct 2011


36 bookmarked

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

Rate me:
Please Sign up or sign in to vote.
4.67/5 (3 votes)
2 Oct 2011CPOL3 min read
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

  1. Overview
  2. Case Study/Problem Statement
  3. Solution Statement
  4. Prerequisites
  5. How to get started?
  6. Few Twists and Turns (Tips and Tricks)
  7. Conclusion


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.

Case Study/Problem Statement

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.

Solution Statement

What we can achieve?

  • We can save Microsoft .NET chart as image and can export it in report viewer RDLC.
  • We can export web page as is in report viewer.
  • Some AXD files format do not export seamlessly in Word, Excel and PDF. For such cases, we can leverage report viewer export function.


How to Get Started?

  1. Create Web solution. Add one web page aspx. Drag and drop Report viewer web control.
  2. Add property EnableExternalImage=true.

    Image 1

  3. Add property localpath=.
  4. Add New Item, RDLC file.
  5. Drag and drop Image control from toolbox into RDLC file.
  6. Go to View menu. Select Report Data option Refer Fig 2.

    Image 2

  7. Add one report parameter. Refer to Fig 2.

    Image 3

  8. 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.

    Image 4

    Select Image source external for given Image control.

    Image 5

    Assign the report parameter path in function expression.

    Image 6

  9. 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.
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            Microsoft.Reporting.WebForms.ReportParameter[] ReportParameters = 
    			new Microsoft.Reporting.WebForms.ReportParameter[1];
                ReportParameters[0] = 
    		new Microsoft.Reporting.WebForms.ReportParameter
    		("ReportParameter1", file:///+
  10. ***Most important point to be taken care of; ensure to add script manager in master page as report viewer component default works in asynchronous Ajax mode. Else you will get the below runtime error.

    Image 7

  11. Press Ctrl+F5 to view the output.

***Few Twists and Turns (Tips and Tricks)

  1. 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.

    public void ExportChart(Chart chart)
        MemoryStream image = new MemoryStream();
        chart.SaveImage(image, ChartImageFormat.Png);
        Response.ContentType = "image/png";
    		("attachment; filename={0}.png", Page.Title.Replace(" ", "_")));
        Response.OutputStream.Write(image.ToArray(), 0, (int)image.Length); }
  2. 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:
               ReportViewer1.LocalReport.ReportPath = "report1.rdlc";
               List<microsoft.reporting.webforms.reportparameter> _ParamList = 
    		new List<microsoft.reporting.webforms.reportparameter>();
               ReportViewer1.LocalReport.EnableExternalImages = true;
                objReportParameter.Add(new Microsoft.Reporting.WebForms.ReportParameter
    				("ImagePath", "/Images/Sample.JPG"));
                Microsoft.Reporting.WebForms.Warning[] warnings;
                string[] streamids;
                string mimeType;
                string encoding;
                string extension;
     string deviceInfo = "<deviceinfo>" +
                                        "  <outputformat>PDF</outputformat>" +
                                        "  <pagewidth>8.5in</pagewidth>" +
                                        "  <pageheight>11in</pageheight>" +
                                        "  <margintop>0.5in</margintop>" +
                                        "  <marginleft>0.25in</marginleft>" +
                                        "  <marginright>0.25in</marginright>" +
                                        "  <marginbottom>1in</marginbottom>" +
    byte[]writeBinaryBytes = new byte[0];
    writeBinaryBytes = ReportViewer1.LocalReport.Render
    	("PDF", deviceInfo, out mimeType, out encoding, out extension, 
    	out streamids, out warnings);
                HttpContext.Current.Response.Buffer = true;
    		("content-disposition", "attachment; filename=" + "San.pdf");
                HttpContext.Current.Response.ContentType = "application/octet-stream";


Hope the above approach/solution gives you some directions whenever you come across a similar problem statement.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Technical Lead
Australia Australia
Whatsup-->Exploring--> MVC/HTML5/Javascript & Virtualization.......!

Comments and Discussions

GeneralMy vote of 5 Pin
Member 335003925-Nov-11 0:07
Member 335003925-Nov-11 0:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.