将GridView导出到Excel导出整个页面

我将一个GridView导出到一个Excel文件,但是当我打开这个文件时,首先我得到一个关于格式types和扩展名不匹配的错误,当我打开它时,整个页面被带入到Excel文件中,而不仅仅是网格视图。

我没有使用更新面板。 我尝试使用GridView中的button和外部和相同的结果,所以它似乎可能是从代码隐藏的东西看起来像这样:

  Response.Clear(); Response.Buffer = true; string filename = "GridViewExport_" + DateTime.Now.ToString() + ".xls"; Response.AddHeader("content-disposition", "attachment;filename=" + filename); Response.Charset = String.Empty; Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView3.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(); HttpContext.Current.ApplicationInstance.CompleteRequest(); 

你可以试试这个方法:

 void ExportDataSetToExcel(GridView grdData, string filename) { grdData.BorderStyle = BorderStyle.Solid; grdData.BorderWidth = 1; grdData.BackColor = Color.WhiteSmoke; grdData.GridLines = GridLines.Both; grdData.Font.Name = "Verdana"; grdData.Font.Size = FontUnit.XXSmall; grdData.HeaderStyle.BackColor = Color.DimGray; grdData.HeaderStyle.ForeColor = Color.White; grdData.RowStyle.HorizontalAlign = HorizontalAlign.Left; grdData.RowStyle.VerticalAlign = VerticalAlign.Top; HttpResponse response = HttpContext.Current.Response; response.Clear(); response.Charset = ""; response.ContentType = "application/vnd.ms-excel"; response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename+ "\""); using (var sw = new StringWriter()) { using (var htw = new HtmlTextWriter(sw)) { grdData.RenderControl(htw); response.Write(sw.ToString()); response.End(); } } } 

使用此代码:这将工作正常..

aspx代码::

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Debug ="true" enableEventValidation ="false" Inherits="default"%> 

这是aspx代码:

  protected void ConvertToExcel_Click(object sender, EventArgs e) { Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Report.xls")); Response.ContentType = "application/ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); GridView1.AllowPaging = false; GridView1.DataBind(); GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF"); for (int i = 0; i < GridView1.HeaderRow.Cells.Count; i++) { GridView1.HeaderRow.Cells[i].Style.Add("background-color", "#bfc2c7"); } int j = 1; foreach (GridViewRow gvrow in GridView1.Rows) { if (j <= GridView1.Rows.Count) { if (j % 2 != 0) { for (int k = 0; k < gvrow.Cells.Count; k++) { gvrow.Cells[k].Style.Add("background-color", "#EFF3FB"); } } } j++; } GridView1.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { } 

解决了 !!! 你的代码是正确的唯一的问题与Response.Flush(); 在代码中使用而不是Response.Flush(); 你应该使用Response.End();

以下是工作代码:

  Response.Clear(); Response.Buffer = true; string filename = "GridViewExport_" + DateTime.Now.ToString() + ".xls"; Response.AddHeader("content-disposition", "attachment;filename=" + filename); Response.Charset = String.Empty; Response.ContentType = "application/vnd.ms-excel"; StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView3.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.End(); // Response.Flush(); HttpContext.Current.ApplicationInstance.CompleteRequest();