我想將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.
它顯示在此行中Response.AddHeader錯誤(「內容處置「, 附件); –
什麼是錯誤? –
「類型'GridView'的控件'ctl00_ContentPlaceHolder1_GridView1'必須放置在runat = server的表單標籤中」這個錯誤我明白了。 –