2013-08-21 67 views
1

我正在將GridView導出到Excel文件,但是當我打開 該文件時,首先出現有關 格式類型和擴展名不匹配的錯誤,以及當我打開它 整個頁面被帶入Excel文件,而不僅僅是網格視圖。將GridView導出到Excel導出整個頁面

我沒有使用更新面板。我試圖與GridView控件內按鈕和外 和相同的結果,所以現在看來​​,這可能是從代碼隱藏一些東西, 看起來是這樣的:

 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(); 

回答

0

你可以試試這個方法:

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(); 
     } 
    } 
} 
+0

什麼是strHeader? – coredump

+0

請參閱我的編輯。 – jomsk1e

0

使用此代碼:這將正常工作..

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) 
{ 

} 
0

解決! 你的代碼是正確的唯一的問題與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();