0

图片

我正在从 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();
}
4

2 回答 2

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();
    }
于 2013-11-25T05:47:52.150 回答
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();
}
于 2013-11-08T13:56:34.167 回答