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();
}
在上面
相关文档:
Web 部件的一项主要功能是使最终用户能够个性化网页并保存其个性化设置。修改 Web 部件页的一个方面包括编辑可见 WebPart 控件的外观、布局、行为和其他属性。
Web 部件控件集中的几种控件可提供编辑功能。其中包括 EditorZone 控件,该控件是 Web 部件控件集中用于承载网页上的 EditorP ......
ImportCatalogPart Web 服务器控件可导入 WebPart 控件的说明文件(或用作 WebPart 控件的其他 ASP.NET 服务器控件)。这样就可以将该控件通过预先指定的设置添加到网页中。该说明文件使用户可以共享 WebPart 控件的设置。
说明文件与控件本身不同。它是以 .WebPart 文件扩展名结尾的 XM ......
数据绑定控件比较
(Reapter\DataList\GridView\DatailsView\FormView):
1.插入功能方面:
DetailsView和FormView具有插入功能,其它控件没有
2.模板
DataList\FormView\Repeater三种必须编辑模板,而
GridView和DetailsView只有在将列转换成模板列以后才会出现各种模板.
3.自动分页功能
GridView ,DetailsView和Form ......
添加一个类:把下面的文件放在类中就可以了。
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
public class MessageBox
{
public MessageBox()
{
& ......
ASP.NET中多国语言的实现
http://www.cnblogs.com/firstyi/archive/2008/03/13/1103970.html
ASP.NET缓存提高站点性能
http://www.cnblogs.com/firstyi/archive/2007/08/15/856676.html
客户端脚本管理
http://www.cnblogs.com/firstyi/archive/2006/11/13/559049.html ......