VS2005中 GridView导入Excel的导入需要注意的几点 错误提示: <?xml version="1.0"?>
<!-- 注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的 “网站”->“Asp.Net 配置”选项。 设置和注释的完整列表在 machine.config.comments 中,该文件通常位于 \Windows\Microsoft.Net\Framework\v2.x\Config 中 --> <configuration> <appSettings> <!--数据库连接串--> <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" 将调试符号插入 已编译的页面中。但由于这会 影响性能,因此只在开发过程中将此值 设置为 true。 --> <compilation debug="true"/> <!-- 通过 <authentication> 节可以配置 ASP.NET 使用的 安全身份验证模式, 以标识传入的用户。 --> <authentication mode="Windows"/> <!-- 如果在执行请求的过程中出现未处理的错误, 则通过 <customErrors> 节可以配置相应的处理步骤。具体说来, 开发人员通过该节可以配置 要显示的 html 错误页 以代替错误堆栈跟踪。 <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm" /> <error statusCode="404" redirect="FileNotFound.htm" /> </customErrors> --> </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.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AllowPaging="true" OnPageIndexChanging="Paging"> </asp:GridView> </div> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导出到Excel" /> </form> </body> </html>
/*
* // by XiaoYin [10/22/2006] */ 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方法 /// 确认在运行时为指定的 ASP.NET 服务器控件呈现 HtmlForm 控件。 /// </summary> /// <param name="control">ASP.NET 服务器控件,它必须位于 HtmlForm 控件中</param> public override void VerifyRenderingInServerForm(Control control) { //base.VerifyRenderingInServerForm(control); } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindData(); } } /// <summary> /// 绑定数据 /// </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> /// 内存分页 /// </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] 经测试如果设置为 GetEncoding("GB2312"),导出的文件将会出现乱码。 Response.ContentEncoding = System.Text.Encoding.UTF7; //设置输出文件类型为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(); } } |