易截截图软件、单文件、免安装、纯绿色、仅160KB

asp.net Gridview中的内容导出到Excel

1:Gridview中的内容导出到Excel 
 Asp.net 2.0中新增的gridview控件,是十分强大的数据展示控件,在前面的系列文章里,分别展示了其中很多的基本用法和技巧。在本文中,将继续探讨有关的技巧。
  一、Gridview中的内容导出到Excel
  在日常工作中,经常要将gridview中的内容导出到excel报表中去,在asp.net 2.0中,同样可以很方便地实现将整个gridview中的内容导出到excel报表中去,下面介绍其具体做法:
  首先,建立基本的页面default.aspx
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
<br/>
<asp:Button ID="BtnExport" runat="server" OnClick="BtnExport_Click"
Text="Export to Excel" />
</form>
  在default.aspx.cs中,写入如下代码:
protected void Page_Load(object sender, EventArgs e)
{
 if (!Page.IsPostBack)
 {
  BindData();
 }
}
private void BindData()
{
 string query = "SELECT * from customers";
 SqlConnection myConnection = new SqlConnection(ConnectionString);
 SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
 DataSet ds = new DataSet();
 ad.Fill(ds, "customers");
 GridView1.DataSource = ds;
 GridView1.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
 // Confirms that an HtmlForm control is rendered for
}
protected void Button1_Click(object sender, EventArgs e)
{
 Response.Clear();
 Response.AddHeader("content-disposition","attachment;filename=FileName.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);
 GridView1.AllowPaging = false;
 BindData();
 GridView1.RenderControl(htmlWrite);
 Response.Write(stringWrite.ToString());
 Response.End();
 GridView1.AllowPaging = true;
 BindData();
}
protected void paging(object sender,GridViewPageEventArgs e)
{
 GridView1.PageIndex = e.NewPageIndex;
 BindData();
}
  在上面


相关文档:

ASP.NET CatalogZone Web 服务器控件概述

 
      Web 部件控件(例如 CatalogZone 控件)的一项主要功能是可以让最终用户个性化网页并保存其个性化设置。CatalogZone 控件允许最终用户在运行时向 Web 部件页添加 WebPart 控件或其他服务器控件。CatalogZone 控件用作 Web 部件控件集内的主控件,用于在网页中承载 CatalogPart 控件 ......

ASP.NET ConnectionsZone Web 服务器控件概述


      使用 Web 部件控件,可以让用户在运行时创建两个服务器控件之间的连接,以形成连接并共享数据。一个控件用作数据的提供者,另一个控件用作数据的使用者。这两个控件可以是 WebPart 控件或任何其他类型的服务器控件。必须将它们设计为处理连接且位于 WebPartZoneBase 区域中。
 
在 ......

ASP.NET ImportCatalogPart Web 服务器控件概述


      ImportCatalogPart Web 服务器控件可导入 WebPart 控件的说明文件(或用作 WebPart 控件的其他 ASP.NET 服务器控件)。这样就可以将该控件通过预先指定的设置添加到网页中。该说明文件使用户可以共享 WebPart 控件的设置。
说明文件与控件本身不同。它是以 .WebPart 文件扩展名结尾的 XM ......

asp.net中使用javascript的方法及区别

1.使用 使用Response.Write,这种方法会把JS代码写在页面的最顶部(的前面):
2. 使用: page.ClientScript.RegisterStartupScript(); 这种方法会把JS代码嵌入在页面的底部、表单的最后 (前面),适用于要在页面控件加载完成后运行的JS代码
3.使用RegisterClientScriptBlock();这种方法会把JS代码嵌入在页面的顶部、 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号