将所有数据导出为ex​​cel

时间:2014-04-08 16:37:38

标签: c# asp.net sql-server-2008 gridview

这是我的代码。

ASPX:

<%@ Page Title="" Language="C#" EnableEventValidation="false" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Administrator.aspx.cs" Inherits="Administrator" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <div style="width:100%;overflow:auto">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="accid0v" style="width:100%;overflow:auto"
            HorizontalAlign="Center" AllowPaging="True" AllowSorting="True" 
            DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand" 
            CellPadding="4" ForeColor="#333333" GridLines="None" >
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
            <asp:BoundField DataField="accid0v" HeaderText="AccID" ReadOnly="True" 
                SortExpression="accid0v" />
            <asp:BoundField DataField="fcustn0v" HeaderText="First Name" 
                SortExpression="fcustn0v" />
            <asp:BoundField DataField="lcustn0v" HeaderText="Last Name" 
                SortExpression="lcustn0v" />
            <asp:BoundField DataField="contph0v" HeaderText="Phone Number" 
                SortExpression="contph0v" />
            <asp:BoundField DataField="email0v" HeaderText="Email ID" 
                SortExpression="email0v" />
            <asp:BoundField DataField="billin0v" HeaderText="Address" 
                SortExpression="billin0v" />
            <asp:BoundField DataField="actd0v" HeaderText="Activation Date" 
                SortExpression="actd0v" />
            <asp:BoundField DataField="Total" HeaderText="Total" ReadOnly="True" 
                SortExpression="Total" />
            <asp:BoundField DataField="Open" HeaderText="Open" ReadOnly="True" 
                SortExpression="Open" />
            <asp:BoundField DataField="Disconnected" HeaderText="Disconnected" 
                ReadOnly="True" SortExpression="Disconnected" />
            <asp:BoundField DataField="No Communication" HeaderText="No Communication" 
                ReadOnly="True" SortExpression="No Communication" />
            <asp:BoundField DataField="Refund Request" HeaderText="Refund Request" 
                ReadOnly="True" SortExpression="Refund Request" />
            <asp:BoundField DataField="Resolved" HeaderText="Resolved" ReadOnly="True" 
                SortExpression="Resolved" />
            <asp:BoundField DataField="Scheduled Callback" HeaderText="Scheduled Callback" 
                ReadOnly="True" SortExpression="Scheduled Callback" />
            <asp:BoundField DataField="Resolved by third party" 
                HeaderText="Resolved by third party" ReadOnly="True" 
                SortExpression="Resolved by third party" />
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:techvConnectionString %>" 
        SelectCommand="SELECT * from Table1">
    </asp:SqlDataSource>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Export To Excel" 
        onclick="Button1_Click" />
</asp:Content>

C#:

protected void Button1_Click(object sender, EventArgs e)
    {

        Response.AddHeader("content-disposition", "attachment; filename=FileName.xls");

        Response.ContentType = "application/vnd.xls";

        System.IO.StringWriter stringWrite = new System.IO.StringWriter();

        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

        GridView1.RenderControl(htmlWrite);

        Response.Write(stringWrite.ToString());

        Response.End();

    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
}

当我点击我的导出到Excel按钮时,我希望它将所有GridView数据导出到Excel,但没有选择按钮和GridView的页脚。

0 个答案:

没有答案