Develop Chart in ASP.NET With Export to Excel and PDF For MS Excel as Well as Open Office

Recently while working on a project in ASP.NET 4.0, I came across a scenario where I had to develop a chart that can be exported to PDF and Excel. Apart from this it has to be viewable in both Microsoft Office and Open Office.

Let us try to develop this functionality.

Create a new empty web application and add a new webpage Chart.aspx.

ASP.NET 4.0 comes with a new chart control as in the following:

1.jpg

Go to the toolbox and drag a Chart control onto the page.

Now specify the datasource. Here I am using sqldatasource and a view of Northwind database to demonstrate the issue.

2.jpg

Now build the application and run Chart.aspx page.

3.jpg

As seen above, the chart has been generated successfully. But, now we want to export the chart in PDF or Excel format.

First, let us see how to export the chart to Excel.

Let us add one button to the page. Add the following button click event:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.IO;  
  8. namespace ReportView  
  9. {  
  10.     public partial class Chart : System.Web.UI.Page  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.         }  
  15.         protected void btnExportToExcel_Click(object sender, EventArgs e)  
  16.         {  
  17.             string attachment = "attachment; filename=Chart.xls";  
  18.             Response.ClearContent();  
  19.             Response.AddHeader("content-disposition", attachment);  
  20.             Response.ContentType = "application/vnd.ms-excel";  
  21.             StringWriter sw = new StringWriter();  
  22.             HtmlTextWriter htw = new HtmlTextWriter(sw);  
  23.             chart.RenderControl(htw);  
  24.             Response.Write(sw.ToString());  
  25.             Response.End();  
  26.         }  
  27.     }  
  28. }   

Now run the page and click on the Export to Excel button. Excel will be generated.

Save the Excel file and open it in Microsoft Office. You will see that it has added one image tag of the chart but actually the image is not rendered.

There is one workaround for this. Add the following event for the button:

  1. protected void btnExportToExcel_Click(object sender, EventArgs e)  
  2. {  
  3.     string tmpChartName = "ChartImage.jpg";  
  4.     string imgPath = HttpContext.Current.Request.PhysicalApplicationPath + tmpChartName;  
  5.     chart.SaveImage(imgPath);  
  6.     string imgPath2 = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/" + tmpChartName);  
  7.     Response.Clear();  
  8.     Response.ContentType = "application/vnd.ms-excel";  
  9.     Response.AddHeader("Content-Disposition""attachment; filename=Chart.xls;");  
  10.     StringWriter stringWrite = new StringWriter();  
  11.     HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);  
  12.     string headerTable = @"";  
  13.     Response.Write(headerTable);  
  14.     Response.Write(stringWrite.ToString());  
  15.     Response.End();  
  16. } 

Here we are first saving the chart as an image. After saving the image we are creating one HTML with a table with a single tr and td tag and an image tag inside that td tag. We are then binding the generated chart image to the img tag as shown above.

Build the application and click on the Export to Excel button. Excel will be generated.

Save the Excel file and open in Microsoft Office. The chart will be displayed in the Excel file.

But there is one issue. If you don't have Microsoft office installed and you are using Open Office, then nothing will be displayed in the generated Excel file.

Let us try to change the MIME type in the code above and try to export the chart.

  1. Response.ContentType = "application/vnd.oasis.opendocument.spreadsheet";  
  2. Response.AddHeader("Content-Disposition""attachment; filename=Chart.ods;");

You will see that the chart is not being exported in the Open Office spreadsheet.

Also, what if you want to export the chart above to PDF? Specifying MIME for PDF in the code above will not work.

So directly using the chart control in your page has some limitations. The solution for this is simple.

Remove the chart control from the page and add a local RDLC report.

4.jpg

Drag the chart control from the toolbox displayed in the RDLC report and specify the datasource and view for the chart.

5.jpg

Save the report and go to the Chart.aspx page.

Add one ReportViewer control and ScriptManager control in the page and specify the Chart.rdlc report in the ReportViewer.

6.jpg

Specify the datasource in ReportViewer to the Northwind sqldatasource and run the page.

The chart will be displayed as in the following:

7.jpg
In the toolbar of ReportViewer there are options for exporting to Excel, PDF and Word.

After exporting the chart can be clearly viewed in Microsoft Office as well as Open Office.

Chart can also be exported to PDF without any issue.

Thus our task is accomplished without using any third party components. Thanks to the ReportViewer control.


Similar Articles