从gridview导出excel时删除图像

时间:2013-11-08 12:07:21

标签: c# asp.net excel gridview

Image

我从gridview导出excel并没有给我很好的格式。图像显示在Excel文件中。我不想要图像,我该怎么办?

这是我的代码

protected void Export_to_Excel(object sender, EventArgs e)
{
   //System.Diagnostics.Debugger.Break();
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=vault-extract-nsf.xls");
    Response.ContentType = "application/vnd.xlsx";
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    GridView1.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();
}

2 个答案:

答案 0 :(得分:1)

我做了,图像被忽略了。

protected void Export_to_Excel(object sender, EventArgs e)
    {
        //System.Diagnostics.Debugger.Break();
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=PatientSearchReport.xls");
        Response.ContentType = "application/vnd.xlsx";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);  
        GridView1.RenderControl(htmlWrite);
        string html2 = Regex.Replace(stringWrite.ToString(), @"(<input type=""image""\/?[^>]+>)", @"", RegexOptions.IgnoreCase);
        html2 = Regex.Replace(html2, @"(<input class=""checkbox""\/?[^>]+>)", @"", RegexOptions.IgnoreCase);
        html2 = Regex.Replace(html2, @"(<a \/?[^>]+>)", @"", RegexOptions.IgnoreCase);
        Response.Write(html2.ToString());
        Response.End();
    }

答案 1 :(得分:0)

你可以这样做

 protected void Export_to_Excel(object sender, EventArgs e)
 {

GridView tmpGrid = new GridView();
// here bind this grid with same datasource which you have used for GridView1 

//System.Diagnostics.Debugger.Break();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=vault-extract-nsf.xls");
Response.ContentType = "application/vnd.xlsx";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
tmpGrid.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}