Yazı Font Küçült Yazı Font Büyült

Gridview daki Bilgileri Excel e  Aktarma

 

Merhaba arkadaşlar. Bu makalemizde GridView nesnesindeki verileri Excel dosyasına aktaracağız.

 

Resim1

Şekil 1

 

Resim2

Şekil 2

 

Default.aspx.cs

 

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

 

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        HtmlForm form = new HtmlForm();

        string dosya = "attachment;filename=personel.xls";

        Response.ClearContent();

        Response.AddHeader("content-disposition", dosya);

        Response.ContentType = "appliction/ms-excel";

        StringWriter sw = new StringWriter();

        HtmlTextWriter hw = new HtmlTextWriter(sw);

        form.Controls.Add(GridView1);

        this.Controls.Add(form);

        form.RenderControl(hw);

        Response.Write(sw.ToString());

        Response.End();

 

    }

}

 

Default.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

   

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

            CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource1"

            ForeColor="#333333" GridLines="None">

            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />

            <Columns>

                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"

                    ReadOnly="True" SortExpression="ID" />

                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />

                <asp:BoundField DataField="FullName" HeaderText="FullName"

                    SortExpression="FullName" />

                <asp:BoundField DataField="Login" HeaderText="Login" SortExpression="Login" />

            </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>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server"

            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"

            SelectCommand="SELECT [ID], [Email], [FullName], [Login] FROM [Employees]">

        </asp:SqlDataSource>

        <br />

        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"

            Text="Excel'e Aktar" />

   

    </div>

    </form>

</body>

</html>

 

Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek dileğiyle.  Bol kodlu günler. Bahadır ŞAHİN