导出Gridview以使用图像优秀

时间:2016-03-17 04:20:10

标签: c# asp.net excel gridview

在我的网页中,我有一个数据和一些图像的网格视图,在这里我想导出gridview到Excel图像,我尝试使用下面的代码,它只导出数据,这里我如何导出到擅长图像?

 Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    GridView1.HeaderRow.Style.Add("background-color", "Red");
    for (int i = 0; i < GridView1.Columns.Count - 2; i++)
    {
        GridView1.HeaderRow.Cells[i].Style.Add("background-color", "Red");
    }
    GridView1.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();

这里我使用Openoffice calc来读取excel文件

1 个答案:

答案 0 :(得分:3)

用此

替换功能代码
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();
}

有关详细信息,请参阅此link