GridView导出Excel研究
Introduction:
?
將GridView中的數據導出為Excel是web應用中的常見功能。在不同的應用場景下有不同的導出技術。在本文中我將介紹一些導出的技術,希望對您有所幫助
GridView Export the Excel (Basic Code):?
.
首先看一個基礎的應用。創建一個表格,見截圖
<!--[if !vml]--> <!--[if !vml]-->
?<!--[endif]-->?
?
然后將數據庫中的數據綁定到GridView中的數據,代碼如下:
| private void BindData() { SqlConnection myConnection = new SqlConnection("Server=localhost;Database=School;Trusted_Connection=true"); SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM Users", myConnection); DataSet ds = new DataSet(); ad.Fill(ds); gvUsers.DataSource = ds; gvUsers.DataBind(); } |
<!--[if !vml]-->
<!--[endif]--> <!--[if !vml]--><!--[endif]-->
?
現在,GridView中已經綁定了數據,接下來的任務就是導出到Excel。下面是button事件中的代碼
| Response.ClearContent(); Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls"); Response.ContentType = "application/excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); gvUsers.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); |
?
并且還需要override一下VerifyRenderingInServerForm方法(這一點非常重要,否則在點擊按鈕后會報錯,譯者注),代碼如下:
| public override void VerifyRenderingInServerForm(Control control) { } |
?
點擊導出按鈕后會彈出對話框,詢問您打開或保存。選擇打開文件,導出到Excel的結果如下圖:
<!--[if !vml]-->
<!--[endif]--> <!--[if !vml]--><!--[endif]-->
Exporting GridView to Excel With Style:
您是否注意到了以上代碼存在一些的問題?是的,ID列開頭的0都被截去了。如果你的ID是000345,導出后就編程了345。這個問題可以通過把css添加到輸出流中來解決。為了使ID列正確顯示,您需要將其儲存為文本格式。Excel中的文本格式表示為"mso-number-format:"\@"。
| protected void Btn_ExportClick(object sender, EventArgs e) { string style = @"<style> .text { } </script> "; Response.ClearContent(); Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls"); Response.ContentType = "application/excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); gvUsers.RenderControl(htw); // Style is added dynamically Response.Write(style); Response.Write(sw.ToString()); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { } |
?在上面的代碼中,我通過”style”變量來控制GridView列的樣式。并通過Respnose.Write方法將其添加到輸出流中。最后把樣式添加到ID列。這一步需要在RowDataBound事件中完成
| protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[1].Attributes.Add("class", "text"); } } |
?修改的結果如下:
<!--[if !vml]--><!--[endif]--> <!--[if !vml]-->
<!--[endif]-->
| ? |
Exporting GridView With LinkButtons and Paging:?
?
如果要導出的GridView中包含LinkButton或者分頁(出現分頁碼時,譯者注) 則將出現錯誤:
<!--[if !vml]--><!--[endif]--> <!--[if !vml]-->
<!--[endif]-->
通過修改頁文件可以修正這個問題:EnableEventValidation = "false".
| <%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> |
看一下導出的文件
<!--[if !vml]-->
?
在導出的文件中可以看見linkbutton和dropdownlist控件,雖然dropdownlist控件顯示的數據的確是用戶所選的項,但怎么看也不像是一個導出文件(我倒是覺的挺cool的:)譯者注),現在應如何移除dropdownlist并顯示選擇的文字呢?
?
我寫了一個DisableControls函數,用使循環的方法將linkbutton和dropdownlist替換成literal控件
| private void DisableControls(Control gv) { LinkButton lb = new LinkButton(); Literal l = new Literal(); string name = String.Empty; for (int i = 0; i < gv.Controls.Count; i++) { if (gv.Controls[i].GetType() == typeof(LinkButton)) { l.Text = (gv.Controls[i] as LinkButton).Text; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } else if (gv.Controls[i].GetType() == typeof(DropDownList)) { l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text; gv.Controls.Remove(gv.Controls[i]); gv.Controls.AddAt(i, l); } ? if (gv.Controls[i].HasControls()) { DisableControls(gv.Controls[i]); } } } |
?
方法非常簡單,只需將linkbuton和dropdownlist替換成literal控件,并將選擇項賦值給literal控件的文本屬性。該方法需要在導出前調用
| protected void Btn_ExportExcelPaging(object sender, EventArgs e) { DisableControls(gvUsers); Response.ClearContent(); Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls"); Response.ContentType = "application/excel"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); gvUsers.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); } |
?
現在的Excel中就只剩下選中文本了
<!--[if !vml]-->
<!--[endif]--> <!--[if !vml]--><!--[endif]-->
注:轉至? http://www.cnblogs.com/stswordman/archive/2006/08/24/485641.html
轉載于:https://www.cnblogs.com/dodui/archive/2010/09/25/1834707.html
總結
以上是生活随笔為你收集整理的GridView导出Excel研究的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Git和Repo扫盲——如何取得Andr
- 下一篇: MyEclipse 中文乱码