将包含图像的页面导出到 Excel

发布于 2024-07-29 16:09:07 字数 640 浏览 0 评论 0原文

我的页面包含 2 个 gridview 和 2 个 mschart。 我想将此页面导出到 Excel。 为此,我将这些控件放入面板中并编写以下代码:

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

当此代码运行时,除了图表之外,一切正常。 他们并不是来表现出色的。 我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

面如桃花 2024-08-05 16:09:08

不是 100% 确定,但我猜图表被写入内存以显示在页面上,因此它们在请求后消失。

HTML 不允许在代码中嵌入图像,您必须引用外部文件。

选项:

  1. 检查图表是否允许将图像写入磁盘,但请注意您可能必须存储大量临时文件,以便 Excel 报告继续显示图像
  2. (推荐)将报告导出为 PDF。

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.
伴我心暖 2024-08-05 16:09:08

尝试以下代码。 我已经在本地 IIS 上进行了测试,它工作正常,并且在网格数据顶部包含诸如标题图像/徽标之类的图像。 您必须将图表作为图像包含在内。

Response.ContentType = "application/vnd.ms-excel";        
Response.AddHeader("Content-Disposition", "attachment; filename=test.xls;");                
StringWriter stringWrite = new StringWriter();        
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);        
dgrExport.DataSource = dtExport;        
dgrExport.DataBind();
dgrExport.RenderControl(htmlWrite);
string headerTable = @"<Table><tr><td><img src=""D:\\Folder\\1.jpg"" \></td></tr></Table>";
Response.Write(headerTable);
Response.Write(stringWrite.ToString());        
Response.End();

您可以根据您的要求调整图像的高度和宽度。 标记需要相同的高度和宽度设置。

Try the following code. I have tested at local IIS, it is working properly and including the image like Header Image/Logo on top of the grid data. you have to include your chart as images.

Response.ContentType = "application/vnd.ms-excel";        
Response.AddHeader("Content-Disposition", "attachment; filename=test.xls;");                
StringWriter stringWrite = new StringWriter();        
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);        
dgrExport.DataSource = dtExport;        
dgrExport.DataBind();
dgrExport.RenderControl(htmlWrite);
string headerTable = @"<Table><tr><td><img src=""D:\\Folder\\1.jpg"" \></td></tr></Table>";
Response.Write(headerTable);
Response.Write(stringWrite.ToString());        
Response.End();

you can adjust your image's height and width as per your requirement. Same height and width setting will be required for the <TD> tag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文