我如何从包含图像的网格导出数据到excel

本文关键字:数据 excel 网格 包含 图像 | 更新日期: 2023-09-27 18:03:54

我想从包含图像的网格导出数据到excel。

我使用以下代码-

 private void CreateXL()
 {
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=xxxxxxxt.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.xls";
    StringWriter objStringWriter = new StringWriter();
    HtmlTextWriter objHtmlTextWriter = new HtmlTextWriter(objStringWriter);
    Gridview.RenderControl(objHtmlTextWriter);
    Response.Write(objStringWriter.ToString());
    Response.End();
 }

但是在excel中,我无法看到图像,而不是图像,我得到一个消息说,"链接的图像无法显示文件可能已被移动,重命名或删除。"验证链接指向正确的文件和位置。

我该如何解决这个问题?

我如何从包含图像的网格导出数据到excel

全文:http://www.aspsnippets.com/Articles/Export-GridView-with-Images-to-Word-Excel-and-PDF-Formats-in-ASP.Net.aspx

private void Excel_Export()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
GridView1.DataBind();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
}
GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:'@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}