我使用MS Chart Controls在我的Web应用程序(.Net 4.0)中生成了一个图表。我的要求是将特定图表导出为Excel文件作为图像。我写了下面提到的代码,试图将图表图像作为字节数组写入excel文件。但它在我的excel文件中导致了一些未知的字符。 (字节数组可能已直接写入文件)。
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();
您能否帮助我以正确的方式将此图表写入excel文件? (不使用字节数组也可以) 感谢
答案 0 :(得分:0)
我能够找到实施它的正确方法;导出ms图表(ms图表控件)以excel作为图像。我很高兴在这里分享。正确的源代码示例如下。
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();
谢谢:)