GridView導(dǎo)入Excel的兩點小技巧?VS2005中 GridView導(dǎo)入Excel的導(dǎo)入需要注意的幾點 最近遇到GridView導(dǎo)入Excel問題,總結(jié)出幾點: 1、如果出現(xiàn)下面的錯誤提示可用重載VerifyRenderingInServerForm方法解決。 錯誤提示: 類型“GridView”的控件“GridView1”必須放在具有 runat=server 的窗體標(biāo)記內(nèi) 在后臺文件中重載VerifyRenderingInServerForm方法,如: public override void VerifyRenderingInServerForm(Control control) { //base.VerifyRenderingInServerForm(control); } 2、如果設(shè)置為 GetEncoding("GB2312"),導(dǎo)出的文件將會出現(xiàn)亂碼。 可用Response.ContentEncoding = System.Text.Encoding.UTF7; 或者Encoding.UTF8等來解決,不過導(dǎo)入格式和字體上個人感覺UTF7比UTF8效果好些; 因人而異了:) 相關(guān)代碼如下: Web.config配置: <?xml version="1.0"?> <configuration> <appSettings> <!--數(shù)據(jù)庫連接串--> <add key="ConnectionString" value="data source=.;initial catalog=Northwind;user id=sa;password=sa;persist security info=true;packet size=4096"/> </appSettings> <connectionStrings/> <system.web> <compilation debug="true"/> <authentication mode="Windows"/> </system.web> </configuration> ASPX頁面代碼: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www./1999/xhtml" > <head runat="server"> <title>無標(biāo)題頁</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AllowPaging="true" > </asp:GridView> </div> <asp:Button ID="Button1" runat="server" Text="導(dǎo)出到Excel" /> </form> </body> </html> 實例代碼: /**//* * // */ using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Xml; public partial class _Default : System.Web.UI.Page { /**//// <summary> /// 鏈接字符串 /// </summary> public string ConnectString { get { return ConfigurationManager.AppSettings["ConnectionString"]; } } /**//// <summary> /// 重載VerifyRenderingInServerForm方法 /// 確認(rèn)在運行時為指定的 ASP.NET 服務(wù)器控件呈現(xiàn) HtmlForm 控件。 /// </summary> /// <param name="control">ASP.NET 服務(wù)器控件,它必須位于 HtmlForm 控件中</param> public override void VerifyRenderingInServerForm(Control control) { //base.VerifyRenderingInServerForm(control); } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindData(); } } /**//// <summary> /// 綁定數(shù)據(jù) /// </summary> public void BindData() { // 查詢 string query = "SELECT * FROM Categories"; SqlConnection myConnection = new SqlConnection(ConnectString); SqlDataAdapter ad = new SqlDataAdapter(query, myConnection); DataSet ds = new DataSet(); ad.Fill(ds, "Categories"); GridView1.DataSource = ds; GridView1.DataBind(); } /**//// <summary> /// 內(nèi)存分頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Paging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; BindData(); } protected void Button1_Click(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls"); //gaoyang [10/21/2006] 經(jīng)測試如果設(shè)置為 GetEncoding("GB2312"),導(dǎo)出的文件將會出現(xiàn)亂碼。 Response.ContentEncoding = System.Text.Encoding.UTF7; //設(shè)置輸出文件類型為excel文件。 Response.ContentType = "application/ms-excel"; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.GridView1.RenderControl(oHtmlTextWriter); Response.Output.Write(oStringWriter.ToString()); Response.Flush(); Response.End(); } } |
|
來自: 悟靜 > 《.net和asp.net》