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

时间:2013-03-13 12:58:10

标签: c# asp.net gridview export-to-excel

我想从包含图像的网格中将数据导出到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中我无法看到图像,而不是图像正在收到一条消息,指出“无法显示链接的图像文件可能已被移动,重命名或删除。验证链接指向正确的文件和位置。'

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

完整文章: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();
}