导出 Gridview 以使用图像进行 Excel

问题描述 投票:0回答:2

在我的网页中,我有一个包含数据和一些图像的gridview,这里我想将gridview导出到带有图像的excel,我正在尝试使用下面的代码,它仅导出数据,这里我如何导出到带有图像的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文件

c# asp.net excel gridview
2个回答
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();
}

请参阅此链接了解更多详情


0
投票

Bunun kodlarla alakasi yok。 Resimleri gride koyarken nasil koydugunla ilgili。 eger Server.MapPath koyarsan calisir ama bu seferde ekranda gorunmez.

© www.soinside.com 2019 - 2024. All rights reserved.