views:

291

answers:

1

I have page which contains 2 gridview and 2 mschart. ı want to export this page to excel. For this purpose I took these controls into a panel and write this code:

    Response.Clear();        
    Response.ContentType = "application/vnd.msexcel";
    Response.AddHeader("content-disposition", "attachment; filename=rapor.xls");
    Response.ContentEncoding = System.Text.Encoding.Default;
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    panel1.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();

When this code run, everything is ok except charts. They aren't coming to excel. How can I do it?

+1  A: 

Not 100% sure, but I guess that the charts are written to memory to display on the page, so they vanish after the request.

HTML does not allow an image to be embedded in the code, you have to reference a external file.

Options:

  1. Check if the chart allow the image to be written to disk, but beware of the large number of temp files that you may have to store so the excel reports keep showing the image
  2. (Recomended) Export the report to PDF.
Eduardo Molteni