繁体   English   中英

如何将MS Chart控件导出到Excel

[英]How to export MS Chart control to Excel

我已经使用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文件? (不使用字节数组也可以)谢谢

我能够找到实现它的正确方法。 将ms图表(ms图表控件)导出为ex​​cel作为图像。 很高兴在这里分享。 正确的源代码示例如下。

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();

谢谢 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM