【问题标题】:How can I export data into excel from a grid which contains image?如何从包含图像的网格中将数据导出到 excel 中?
【发布时间】:2013-03-13 12:58:10
【问题描述】:

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

我该如何解决这个问题?

【问题讨论】:

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


【解决方案1】:

全文: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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多