2011-07-04 42 views
5

我想將GridView數據轉換爲Excel表格。如何使用C#lang將asp.net中的gridview數據轉換爲excel表格

我已經寫了下面的代碼,但它給錯誤:

protected void Button1_Click(object sender, EventArgs e) 
{  
    Response.Clear(); 
    Response.AddHeader("content-disposition", "attachment;filename=Avukat.xls"); 
    Response.Charset = ""; 

    Response.ContentType = "application/vnd.xls"; 
    System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 
    GridView1.RenderControl(htmlWrite); 
    Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"); 
    Response.Write(stringWrite.ToString()); 
    Response.End(); 
} 

錯誤:

Control 'ctl00_ContentPlaceHolder1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

回答

2

我覺得你的GridView包含一個linkbutton/Imagebutton or another type of control,這就是爲什麼你得到一個異常時,你正試圖將GridView導出到Excel。

在使用控件之前,您需要在後面的頁面代碼或後面的BasePage代碼中添加以下行。

public override void VerifyRenderingInServerForm(Control control) 
{ 
} 

您可以使用此代碼,該代碼進行測試和工作完美:

System.IO.StringWriter sw = new System.IO.StringWriter(); 
HtmlTextWriter htw = new HtmlTextWriter(sw); 
Response.AddHeader("content-disposition", "attachment; filename=Avukat.xls"); 
Response.ClearContent(); 

Response.AddHeader("content-disposition", attachment); 

GridView1.RenderControl(htw); 
Response.Write(sw.ToString()); 
Response.Flush(); 
Response.End(); 
+0

它顯示在此行中Response.AddHeader錯誤(「內容處置「, 附件); –

+0

什麼是錯誤? –

+0

「類型'GridView'的控件'ctl00_ContentPlaceHolder1_GridView1'必須放置在runat = server的表單標籤中」這個錯誤我明白了。 –

1

你只需要複製下面的代碼。

public override void VerifyRenderingInServerForm(Control control) 
    { 

     // Confirms that an HtmlForm control is rendered for the 
     specified ASP.NET server control at run time. 

    } 
+0

什麼是寫在函數內我不明白pl z建議我 –

+0

@lucky jain:不要寫任何代碼,只需將此代碼放置在.cs頁面上。希望它會對你有所幫助。 –

+0

謝謝你我得到它那裏是我必須在頁面標題中寫入一個聲明,即「EnableEventValidation =」false「」 –

0
//**how gridview data download in excel in asp.net** 

protected void btnDownload_Click(object sender, EventArgs e) 
{ 
    CommonFunction objComm = new CommonFunction(); 
    Hashtable objHash = new Hashtable(); 
    //Hashtable htParam = new Hashtable(); 
    objHash.Clear(); 
    string str = ddlMonthlyYrs.SelectedItem.Text.ToString(); 
    if (str == "Select") 
    { 
     objHash.Add("@Cmonth", ""); 
    } 
    else 
    { 
     objHash.Add("@Cmonth", str.Substring(0, 6)); 
    } 
    if (ddlPaymentTerm.SelectedItem.Text == "Select") 
    { 
     objHash.Add("@PaymentTerm", ""); 
    } 
    else 
    { 
     objHash.Add("@PaymentTerm", ddlPaymentTerm.SelectedValue.ToString()); 
    } 
    if (ddlPayMode.SelectedItem.Text == "Select") 
    { 
     objHash.Add("@PaymentMode", ""); 
    } 
    else 
    { 
     objHash.Add("@PaymentMode", ddlPayMode.SelectedValue); 
    } 

    objHash.Add("@PolicyNo", txtPolicyNo.Text); 

    objHash.Add("@AgentCode", txtAgnCode.Text); 
    objHash.Add("@AgentName", txtAgnName.Text); 

    DataSet objDS = objComm.GetDataSetForPrcDBConn("Prc_GetIncBasedataRp", objHash, "Commssion"); 

    BasicComm.CommonFunction objCom = new BasicComm.CommonFunction(); 
    Response.Clear(); 
    //Response.Charset=」"; 
    Response.ContentType = "application/vnd.ms-excel"; 
    System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 
    System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite); 
    System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid(); 
    dg.DataSource = objDS.Tables[0]; 
    dg.DataBind(); 
    dg.RenderControl(htmlWrite); 
    Response.Write(stringWrite.ToString()); 
    Response.End(); 
} 
+1

嗨pradeep,歡迎來到Stackoverflow。但是,請考慮正確地格式化代碼。您可以在編輯器中使用「代碼」樣式。 – Marcel

1
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 

this.gvTask.RenderBeginTag(htmlWrite); 
this.gvTask.HeaderRow.RenderControl(htmlWrite); 
foreach (GridViewRow row in this.gvTask.Rows) 
{ 
    row.RenderControl(htmlWrite); 
} 
this.gvTask.FooterRow.RenderControl(htmlWrite); 
this.gvTask.RenderEndTag(htmlWrite); 
2

你已經錯過了這一部分:

HtmlForm _HtmlForm= new HtmlForm(); 
GridView1.Parent.Controls.Add(_HtmlForm); 
_HtmlForm.Attributes["runat"] = "server"; 
_HtmlForm.Controls.Add(GridView1); 
_HtmlForm.RenderControl(htmlWrite); 

我的意思是你應該使用這行代碼:

_HtmlForm.RenderControl(htmlWrite); 

相反的:

GridView1.RenderControl(htmlWrite); 

但我認爲這將是更好地使用這個免費的開源組件,它的進口/從/到不同的Excel和Word格式出口:http://npoi.codeplex.com/

相關問題