GridView导出Excel研究
Introduction:
?
將GridView中的數(shù)據(jù)導(dǎo)出為Excel是web應(yīng)用中的常見功能。在不同的應(yīng)用場(chǎng)景下有不同的導(dǎo)出技術(shù)。在本文中我將介紹一些導(dǎo)出的技術(shù),希望對(duì)您有所幫助
GridView Export the Excel (Basic Code):?
.
首先看一個(gè)基礎(chǔ)的應(yīng)用。創(chuàng)建一個(gè)表格,見截圖
<!--[if !vml]--> <!--[if !vml]-->
?<!--[endif]-->?
?
然后將數(shù)據(jù)庫中的數(shù)據(jù)綁定到GridView中的數(shù)據(jù),代碼如下:
| 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]-->
?
現(xiàn)在,GridView中已經(jīng)綁定了數(shù)據(jù),接下來的任務(wù)就是導(dǎo)出到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方法(這一點(diǎn)非常重要,否則在點(diǎn)擊按鈕后會(huì)報(bào)錯(cuò),譯者注),代碼如下:
| public override void VerifyRenderingInServerForm(Control control) { } |
?
點(diǎn)擊導(dǎo)出按鈕后會(huì)彈出對(duì)話框,詢問您打開或保存。選擇打開文件,導(dǎo)出到Excel的結(jié)果如下圖:
<!--[if !vml]-->
<!--[endif]--> <!--[if !vml]--><!--[endif]-->
Exporting GridView to Excel With Style:
您是否注意到了以上代碼存在一些的問題?是的,ID列開頭的0都被截去了。如果你的ID是000345,導(dǎo)出后就編程了345。這個(gè)問題可以通過把css添加到輸出流中來解決。為了使ID列正確顯示,您需要將其儲(chǔ)存為文本格式。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"); } } |
?修改的結(jié)果如下:
<!--[if !vml]--><!--[endif]--> <!--[if !vml]-->
<!--[endif]-->
| ? |
Exporting GridView With LinkButtons and Paging:?
?
如果要導(dǎo)出的GridView中包含LinkButton或者分頁(出現(xiàn)分頁碼時(shí),譯者注) 則將出現(xiàn)錯(cuò)誤:
<!--[if !vml]--><!--[endif]--> <!--[if !vml]-->
<!--[endif]-->
通過修改頁文件可以修正這個(gè)問題:EnableEventValidation = "false".
| <%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> |
看一下導(dǎo)出的文件
<!--[if !vml]-->
?
在導(dǎo)出的文件中可以看見linkbutton和dropdownlist控件,雖然dropdownlist控件顯示的數(shù)據(jù)的確是用戶所選的項(xiàng),但怎么看也不像是一個(gè)導(dǎo)出文件(我倒是覺的挺cool的:)譯者注),現(xiàn)在應(yīng)如何移除dropdownlist并顯示選擇的文字呢?
?
我寫了一個(gè)DisableControls函數(shù),用使循環(huán)的方法將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]); } } } |
?
方法非常簡(jiǎn)單,只需將linkbuton和dropdownlist替換成literal控件,并將選擇項(xiàng)賦值給literal控件的文本屬性。該方法需要在導(dǎo)出前調(diào)用
| 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(); } |
?
現(xiàn)在的Excel中就只剩下選中文本了
<!--[if !vml]-->
<!--[endif]--> <!--[if !vml]--><!--[endif]-->
注:轉(zhuǎn)至? http://www.cnblogs.com/stswordman/archive/2006/08/24/485641.html
轉(zhuǎn)載于:https://www.cnblogs.com/dodui/archive/2010/09/25/1834707.html
總結(jié)
以上是生活随笔為你收集整理的GridView导出Excel研究的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Git和Repo扫盲——如何取得Andr
- 下一篇: MyEclipse 中文乱码