0

I have generated a chart in my web application (.Net 4.0) using MS Chart Controls. My requirement is to export the particular chart into an Excel file as an image. I have written the below mentioned code attempting to write the chart image to the excel file as a byte array. But it resulted some unknown characters in my excel file. (The byte array might have been written directly to the file).

byte[] bytes2;
    using (var chartimage = new MemoryStream())
    {
        Chart1.SaveImage(chartimage, ChartImageFormat.Png);
        bytes2 = chartimage.GetBuffer();
    }

    Response.Clear();
    Response.ContentType = "application/ms-excel";
    Response.AddHeader("content-disposition", "attachment; filename=StudentResults.xls");
    Response.BinaryWrite(bytes2);

    Response.End();

Can any of you please help me to write this chart to an excel file in a correct way? (either without using a byte array would also fine) Thanks

Sugandika
  • 772
  • 5
  • 10
  • 1
    You're going to have to actually create the spreadsheet first rather than trying to just save an image as an .XLS! Try [one of these libraries](http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp) – Rup May 16 '12 at 11:37
  • @Rup - Thank you very much for your quick response and suggestion. I'm glad to say that I have found out the way to implement it correctly. I will share my source code, so that everyone can know about it. Thanks again – Sugandika May 16 '12 at 12:01

1 Answers1

0

I was able to find out the correct way of implementing it; export ms chart (ms chart controls) to excel as an image. I'm glad to share it here. the correct source code sample was as follows.

string tmpChartName = "test2.jpg";
    string imgPath = HttpContext.Current.Request.PhysicalApplicationPath + tmpChartName;

    Chart1.SaveImage(imgPath);
    string imgPath2 = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/" + tmpChartName);

    Response.Clear();
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment; filename=test.xls;");
    StringWriter stringWrite = new StringWriter();
    HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    string headerTable = @"<Table><tr><td><img src='" + imgPath2 + @"' \></td></tr></Table>";
    Response.Write(headerTable);
    Response.Write(stringWrite.ToString());
    Response.End();

Thanks :)

Sugandika
  • 772
  • 5
  • 10
  • Hmm, that's still not a real Excel file - you're now emitting HTML as an .XLS rather than an actual .XLS. And your solution doesn't support more than one concurrent user: if you're going to go down this route it would be better to have another controller generate and return the image then have this controller generate an URL to that. However I still think you should create a real XLS file not HTML. – Rup May 16 '12 at 12:07
  • @Rup - Thank you very much for your ideas. I will consider it in order to come up with a better solution. – Sugandika May 16 '12 at 12:13