protected void Button1_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); ds.ReadXml(AppDomain.CurrentDomain.BaseDirectory + "XMLFile1.xml"); GridView1.DataSource = ds; GridView1.DataBind();
Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=TaoBaoItems.xls"); Response.Charset = "gb2312"; Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); EnableViewState = false; GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString()); Response.End(); }
Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server.
解决这个的办法有一个: public override void VerifyRenderingInServerForm(Control control) { } 加上上面这个重写的代码后,就能保存为excel文件了,是不是很爽啊? 那么不这样写可不可以呢? 回答当然也是肯定的: 办法就是: 不要使用gridview这个东东来绑定数据,用大伙比较喜欢的:Repeater控件来绑定程序! 代码还是一样滴: Code: protected void Button1_Click(object sender, EventArgs e) {
DataSet ds = new DataSet(); ds.ReadXml(AppDomain.CurrentDomain.BaseDirectory + "XMLFile1.xml"); GridView1.DataSource = ds; GridView1.DataBind();
Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=TaoBaoItems.xls"); Response.Charset = "gb2312"; Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); EnableViewState = false; GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString()); Response.End(); } HTML:
<asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <table> <tr><td>姓名</td><td>年龄</td></tr> </HeaderTemplate> <ItemTemplate> <tr><td><%#Eval("name")%></td><td><%#Eval("age")%></td></tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
这样导出成excel报表就没问题了。 其实有时候你会发现,如果你导出来的表格出现了乱码,这就很郁闷了,年龄列的表头名字为:“骞撮緞”,这是个什么编码? 我又不是神仙,我怎么知道,于是又想了个办法,代码如下: static void Main(string[] args) { string sttest = "骞撮緞"; EncodingInfo[] infoList=Encoding.GetEncodings(); foreach (EncodingInfo encode in infoList) { byte[] buffer1 = encode.GetEncoding().GetBytes(sttest); string strBuffer = Encoding.UTF8.GetString(buffer1); Console.WriteLine("Encoding={0},编码为:{1}", encode.Name.ToString(), strBuffer); } } 乖乖啊,乱码一大堆,不过如黑暗中见一丝曙光,有几个是“年龄”,于是挑了一个比较熟悉的编码:“GB18030”,于是将代码改成如下:
protected void Button1_Click(object sender, EventArgs e) { Response.AddHeader("content-disposition", "attachment;filename=TaoBaoItems.xls"); //网站编码 Response.Charset = "utf-8"; //输出编码 Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB18030"); Response.ContentType = "application/vnd.xls"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); Repeater1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString()); Response.End(); }
OK,一切搞定了,这个世界终于安静下来了! 从此以后,大伙导出excel就是成事不求人了!
---http://www.cnblogs.com/bbqqqbq/
Export GridView To Excel
利用GridView自行Render出與網頁上格式相同的HTML,配合application/vnd.xls MIME Type,GridView就可以直接在Excel中重現 protected void btnExportExcel_Click(object sender, EventArgs e) { Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=PoolExport.xls"); Response.ContentType = "application/vnd.xls"; System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(sw); //關閉換頁跟排序 GridView1.AllowSorting = false; GridView1.AllowPaging = false; //移去不要的欄位 GridView1.Columns.RemoveAt(GridView1.Columns.Count - 1); GridView1.DataBind(); //建立假HtmlForm避免以下錯誤 //Control 'GridView1' of type 'GridView' must be placed inside //a form tag with runat=server. //另一種做法是override VerifyRenderingInServerForm後不做任何事 //這樣就可以直接GridView1.RenderControl(htw); HtmlForm hf = new HtmlForm(); Controls.Add(hf); hf.Controls.Add(GridView1); hf.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); }
補充說明幾個重點: 1.由於是透過HTML+MIME宣告的原理,DataGrid、GridView都適用。 2.記得移去不要的欄位,取消排序、分頁的設定。 3.這個寫法會產生Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server的錯誤,可利用override VerifyRenderingInServerForm或另建一個假的HtmlForm來避免 解決方法1. 若查詢結果含有排序功能所以存成 Excel 時要先關閉 AllowSorting 功能(不然會有連結),然後需要再 DataBind 一次。 而在 DataBind 之前使用 EnableViewState = false; 即可避免「Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server. 」的錯誤 解決方法2. 只要加上 public override void VerifyRenderingInServerForm(Control control) { //避免「型別 xxx 的控制項 xxx 必須置於有 runat=server 的表單標記之中。」的問題 } 即可 4.這個寫法還會導致RegisterForEventValidation can only be called during Render()的錯誤,需在Page宣告中停用EnableEventValidation解決。 5. 這種產生HTML再標ContentType的方法只能產生單一Sheet 6. 匯出的Excel可以開啟 可是會出現檔案格式與附檔名格式不同的訊息這個警示
資料來源:http://blog.darkthread.net/blogs/darkthreadtw/archive/2009/12/06/1143.aspx
http://blog.sina.com.cn/s/blog_6d4576c70100m6n4.html