• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
ARCGIS830208
博客园    首页    新随笔    联系   管理    订阅  订阅

ASP.net的GridView控件中的数据如何导入到Excel中~~~

第一中方法:

  新建一个网站:在Default.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>


    </div>
        <asp:GridView ID="GridView1" runat="server" >
        </asp:GridView>
        <asp:Button ID="BtnExport" runat="server" Text="导出" OnClick="Button1_Click" />
    </form>
</body>
</html>
然后再在Default.aspx.cs中写入如下代码:

        

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.OleDb;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }
    }
    private void BindData()
    {
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:/db1.mdb");
        OleDbDataAdapter thisadapter = new OleDbDataAdapter("select *from order by 房屋编号 asc", con);
        string sql = "select *from 物管费用表 where 1=1";
        thisadapter.SelectCommand.CommandText = sql;

        DataSet ds = new DataSet();
        con.Open();
        thisadapter.SelectCommand.ExecuteNonQuery();
        thisadapter.Fill(ds, "物管费用表");
        this.GridView1.DataSource = ds;
        this.GridView1.DataBind();

     
       
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an HtmlForm control is rendered for
    }

    protected void BtnExport_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();
    }

}
第二中方法:

   在Default.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 id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body id="Body1"  runat="server">
    <form id="form1" runat="server">
    <div id="Div1" runat="server">
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="名称" DataSourceID="SqlDataSource1" AllowPaging="True"
            BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px"
            CellPadding="2" ForeColor="Black" GridLines="None" >
            <FooterStyle BackColor="Tan" />
            <Columns>
                <asp:BoundField DataField="名称" HeaderText="名称" />
                <asp:BoundField DataField="结构" HeaderText="结构" />
                <asp:BoundField DataField="建筑面积" HeaderText="建筑面积" />
                <asp:BoundField DataField="层数" HeaderText="层数" />
                <asp:BoundField DataField="房屋数量" HeaderText="房屋数量" />
                <asp:BoundField DataField="单元数量" HeaderText="单元数量" />
                <asp:BoundField DataField="施工单位" HeaderText="施工单位" />
                <asp:BoundField DataField="竣工日期" HeaderText="竣工日期" />
            </Columns>//上面这些DataField都是数据库表中添加到字段名
            <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
                HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
            <HeaderStyle BackColor="Tan" Font-Bold="True" />
            <AlternatingRowStyle BackColor="PaleGoldenrod" />
        </asp:GridView>
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="导出" />
        <br />
   
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:db1ConnectionString3 %>"
        SelectCommand="SELECT [ID], [名称], [结构], [建筑面积], [层数], [备注], [竣工日期], [施工单位], [单元数量], [房屋数量] FROM [小区楼宇信息表]" ProviderName="<%$ ConnectionStrings:db1ConnectionString3.ProviderName %>"></asp:SqlDataSource>
    </form>
</body>
</html>

 然后再在Default.aspx.cs中写入下面代码:

            

using System;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();//绑定数据
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        System.Web.HttpContext curContext = System.Web.HttpContext.Current;

        Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
        Response.Charset = "UTF-8";//设置字符集
        curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;//设置编码集
      
        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView1.AllowPaging = false;//写到Excel的数据不用分页
        BindData();
        GridView1.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());//向客户端写数据
        Response.End();
        GridView1.AllowPaging = true;//恢复分页
        BindData();

    }
    private void BindData()
    {
        GridView1.DataBind();
    }
    public override void VerifyRenderingInServerForm(Control control)//这个方法不写的话可能报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。
    {
        // Confirms that an HtmlForm control is rendered for
    }

}

当然还有其他的方法,在此就介暂时给大家介绍这2种了~
 

 

posted @ 2008-10-30 16:54  吹过你的风  阅读(1522)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3