.net实现GridView的动态绑定数据,并对GridView的数据下载保存为本地excel文件

2 篇文章 0 订阅

1、首先创建控件

在.aspx前端文件里添加两个控件,分别是Button按钮控件(用于点击按钮进行下载保存excel文件)和GridView控件(用于显示要展示的数据)

代码如下:

<asp:Button ID="bt_toExcel" runat="server" Text="导出Excel" OnClick="bt_toExcel_Click" />

<asp:GridView ID="GridView_1test" runat="server" DataKeyNames="number" CellPadding="4" ForeColor="#333333" GridLines="None" Style="text-align: center" AutoGenerateColumns="False"  ShowHeaderWhenEmpty="True" Width="720px">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <asp:BoundField DataField="number" HeaderText="工号" SortExpression="number" />
                <asp:BoundField DataField="name" HeaderText="姓名" SortExpression="name" />
                <asp:BoundField DataField="age" HeaderText="年龄" SortExpression="age" />
                <asp:BoundField DataField="sex" HeaderText="性别" SortExpression="sex" />
                <asp:BoundField DataField="tel" HeaderText="联系方式" SortExpression="tel" />
                <asp:BoundField DataField="xueli" HeaderText="学历" SortExpression="xueli" />
                <asp:BoundField DataField="deptName" HeaderText="部门" SortExpression="deptName" />
                <asp:BoundField DataField="state" HeaderText="状态" SortExpression="state" />
                <asp:TemplateField HeaderText="">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkbtn_1detail" runat="server" selectId1='<%# Eval("number") %>'>详情</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>

2、sql数据库里建个表并添加相应数据:

注意:各字段名要和GridView控件里的DataField值相同

CREATE TABLE [dbo].[Staffs_Info] (
    [number]         INT        NOT NULL,
    [name]           NCHAR (20) NULL,
    [age]            INT        NULL,
    [sex]            NCHAR (10) NULL,
    [tel]            NCHAR (15) NULL,
    [deptName]       NCHAR (20) NULL,
    [xueli]          NCHAR (10) NULL,
    [state]          NCHAR (10) NULL,
    PRIMARY KEY CLUSTERED ([number] ASC)
);

3、在后台.C#文件动态绑定数据源:

比如可以写到Page_Load事件里面,注意是利用GridView控件的ID来进行绑定的,本例的GridView控件ID为:GridView_1test

string strSQL = "select * from Staffs_Ifo ";
DataTable dt = DbManager.ExecuteQuery(strSQL);
GridView_1test.DataSource = dt;//设置数据源,用于填充控件中的项的值列表
GridView_1test.DataBind();//将控件及其所有子控件绑定到指定的数据源

显示结果如下:

 4、点击按钮,实现下载保存为Excel文件:

对按钮控件事件写如下代码:

protected void lnkbtn_1toExcel_Click(object sender, EventArgs e)
    {
        string FileType = "application/ms-excel";
        string FileName = "工作人员.xls";//要保存的文件名
        string style = @"<style>.text{mso-number-format:/@}</script>";//导入到excel时,保存表里数字列中前面存在的 0 .
        Response.Clear();
        Response.Charset = "GB2312";
        Response.ContentEncoding = Encoding.UTF7;
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType = FileType;
        this.EnableViewState = false;
        this.GridView_1test.AllowPaging = false;
        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        this.GridView_1test.RenderControl(htw);
        Response.Write(style);
        Response.Write(sw.ToString());
        //Response.Write(dt.ToString());
        Response.End();
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值