2012-10-15 142 views
1

我使用下面的代碼導出我GridViewExcelASP.Net網格導出到Excel

string attachment = "attachment; filename=Contacts.xls"; 
Response.ClearContent(); 
Response.AddHeader("content-disposition", attachment); 
Response.ContentType = "application/ms-excel"; 
StringWriter sw = new StringWriter(); 
HtmlTextWriter htw = new HtmlTextWriter(sw); 
gvReports.RenderControl(htw); 
Response.Write(sw.ToString()); 
Response.End(); 

此代碼是從this網站複製。我遵循了所有的指示,但在執行任何事情之後。沒有例外(除Thread.Abort,我認爲這是因爲Response.End())。

我也用Response.Flush()但什麼也沒有發生也不例外或文件等

感謝。

編輯現在,代碼如下如下:

我的GridView中控制。該控件在asp.net頁面上,該頁面具有以下方法。按鈕在ascx控件

public override void VerifyRenderingInServerForm(Control control) 
{ 

} 

點擊事件:

protected void btnExportToExcel_Click(object sender, EventArgs e) 
{ 
    ExportToExcel(); 
} 

private void ExportToExcel() 
{ 
    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); 
    gvReports.AllowPaging = false; 
    gvReports.DataBind(); 
    gvReports.RenderControl(hw); 
    Response.Output.Write(sw.ToString()); 
    Response.Flush(); 
    Response.End(); 
} 

gvReports沒有鏈接,複選框等。5個綁定列和1個按鈕。

什麼都沒有發生。

+0

gvReports。的DataBind();應該叫做 – MMK

+0

@MMK – Kashif

回答

0

用戶控件代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.IO; 

public partial class WebUserControl : System.Web.UI.UserControl 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
      this.gridView.DataSource = this.SqlDataSource1; 
      this.gridView.DataBind(); 

    } 
    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); 
     this.gridView.AllowPaging=false; 
     this.gridView.DataBind(); 
     this.gridView.RenderControl(hw); 
     Response.Output.Write(sw.ToString()); 
     Response.Flush(); 
     Response.End(); 
    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     excel_Export(); 
    } 
} 

用戶控制主機頁面:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 
     <uc1:WebUserControl ID="WebUserControl1" runat="server" /> 
    </asp:Content> 

你真的不需要以下事件中的代碼託管網頁的用戶控制的背後,是因爲Excel導出是被通過包含網格視圖和按鈕的用戶控件完成。

public override void VerifyRenderingInServerForm(Control control) 
{ 
     /* Verifies that the control is rendered */ 
} 

希望這會幫助你解決問題。

+0

的回答請看編輯請看編輯。 – Kashif

0

iTextSharp的DLL文件,你會得到最好的方法,這樣做

Click here

+0

是不是隻針對PDF的iTextSharpt? – Kashif

+0

不,你可以將gridview轉換爲excel,word,csw和pdf,而不會受到分頁的影響,請參閱我提供的鏈接。 – LearningAsp

0

我使用MVC3我的解決方案,但代碼我使用推數據輸出到Excel文件:

 var grid = new GridView 
     { 
      DataSource = from lineItem in rows 
         select new 
         { 
          lineItem.ProjectName, 
          lineItem.Sat, 
          lineItem.Sun, 
          lineItem.Mon, 
          lineItem.Tue, 
          lineItem.Wed, 
          lineItem.Thu, 
          lineItem.Fri 
         } 
     }; 
     var fileName = string.Format("{0}:{1}", userName, timesheetDate); 
     grid.DataBind(); 

     Response.ClearContent(); 
     Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xls"); 
     Response.ContentType = "application/msexcel"; 
     var sw = new StringWriter(); 
     var htw = new HtmlTextWriter(sw); 
     grid.RenderControl(htw); 
     Response.Write(sw.ToString()); 
     Response.End(); 

眼前的差異,我看到的是:

I take my data and create a new GridView 
Response.ClearContent() instead of a Response.Clear() 
Response.Write instead of Response.Output.Write 

其他一切看起來都是一樣的,我們都使用一個單擊事件來調用一個void函數。所以我在猜測,如果你做了這些改變,你的代碼應該開始爲你工作。

自從我實施這個解決方案以來我已經有一段時間了,而且我找不到這樣的網站,所以我無法向您展示我的原始資源。對於那個很抱歉。