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 in Word & Excel

RankRiver NameLength (KM)Drainage AreaAvarage DischargeOutflow
1Nile – Kagera6650413232545555100
2Amazon – Ucayali – Apurímac640039767050000219000
3Yangtze63003917180000031900
4Mississippi–Missouri–Jefferson62753902298000016200
5Yenisei–Angara–Selenge55393445258000019600
6Yellow River546433957450002110
7Ob–Irtysh54103364299000012800
8Paraná – Río de la Plata48803030258267218000
9Congo–Chambeshi47002922368000041800
10Amur–Argun44442763185500011400
 

<%@ 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