How to export gridview to excel & Word file with formatting in asp.net
In this post I am explain how to export gridview to Excel & Word file with formatting (css file) in asp.net.

Export Gridview Selected Rows in ASP.NET

 CountryStateCity
IndiaAssamAbhayapuri
IndiaAssamBadarpur
IndiaWest BengalMalda
IndiaWest BengalMedinipur
IndiaWest BengalMejia
IndiaWest BengalMemari
IndiaWest BengalMidnapore
IndiaWest BengalMidnapur
United KingdomAberdeen CityAberdeen
United KingdomAberdeen CityDyce
United KingdomAberdeen CityMilltimber
United KingdomAberdeen CityOld Aberdeen
United KingdomAberdeen CityPeterculter
United KingdomAberdeen CityStoneywood
United KingdomAberdeenshireMacduff
United KingdomAberdeenshireMintlaw
United KingdomAberdeenshireNewtonhill
United KingdomAberdeenshireOldmeldrum
United KingdomAberdeenshirePeterhead
USACaliforniaAcampo
USACaliforniaActon
USACaliforniaAdelanto
USACaliforniaAdin
USACaliforniaAgoura Hills
 

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASPExportGrid._Default" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
   
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <link href="myGrid.css" rel="stylesheet" />
    <h3>Export Gridview in Word & Excel</h3>
    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="5" CssClass="myGrid"
        AlternatingRowStyle-CssClass="alt">
        <Columns>
            <asp:BoundField HeaderText="Rank" DataField="Rank" />
            <asp:BoundField HeaderText="River Name" DataField="RiverName" />
            <asp:BoundField HeaderText="Length (KM)" DataField="LengthInKM" />
            <asp:BoundField HeaderText="Drainage Area" DataField="DrainAgeArea" />
            <asp:BoundField HeaderText="Avarage Discharge" DataField="AvarageDischarge" />
            <asp:BoundField HeaderText="Outflow" DataField="Outflow" /> 
        </Columns>
    </asp:GridView>
    <div>
        <asp:Button ID="btnExportWord" runat="server" Text="Export To Word" OnClick="btnExportWord_Click" />&nbsp;
        <asp:Button ID="btnExportExcel" runat="server" Text="Export To Excel" OnClick="btnExportExcel_Click" />
    </div>
</asp:Content>

Posted By : Sourav Mondal