易截截图软件、单文件、免安装、纯绿色、仅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 AppearanceEditorPart Web 服务器控件概述


      AppearanceEditorPart Web 服务器控件提供了一个编辑器控件,最终用户可以使用该控件在相关联的 WebPart 控件上编辑数个用户界面 (UI) 属性。
      AppearanceEditorPart 控件使最终用户能够编辑 WebPart 控件的几个用户界面属性。下表列出了用户可自定义的功 ......

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


      Web 部件的一项主要功能是使最终用户能够个性化网页并保存其个性化设置。修改 Web 部件页的一个方面包括编辑可见 WebPart 控件的外观、布局、行为和其他属性。
Web 部件控件集中的几种控件可提供编辑功能。其中包括 EditorZone 控件,该控件是 Web 部件控件集中用于承载网页上的 EditorP ......

ASP.NET页面的CheckBoxList组件

CheckBox选择组件是一个程序中都经常的组件。在程序设计中使用到该组件,一般都不会只使用到一个,往往是以多个此类组件的形式出现的。在ASP.NET页面中如果要使用到多个CheckBox组件,除了添加多个CheckBox组件在页面中之外,还有一种比较方便的方法,就是使用CheckBoxList 组件。CheckBoxList组件是由一组的CheckBox组件组 ......

ASP.net的web.config的中文解释

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<!-- 动态调试编译
设置 compilation debug="true" 以将调试符号(.pdb 信息)插入到编译页中。因为这将创建执行起来较慢的大文件,所以应该只在调试时将该值设置为 true,而所有其他时候都设置为false。有关更多信息, ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号