Click here to Skip to main content
12,999,833 members (49,066 online)
Rate this:
Please Sign up or sign in to vote.
See more:

the code below export gridview data into excel,

 //Export gridview data into excel
        protected void excelLinkBtn_Click(object sender, EventArgs e)
            Response.AddHeader("content-disposition", "attachment;filename=StudentReport.xls");
            Response.Charset = "";
            // If you want the option to open the Excel file without saving than
            // comment out the line below
            // Response.Cache.SetCacheability(HttpCacheability.NoCache)
            Response.ContentType = "application/vnd.xls";
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

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

But,it add extra header row with G, H, I alphabets.

how to set the range upto the gridview column?

gridview inside the panel as below

<asp:panel id="studentReportPanel" CssClass="legend"  runat="server"  groupingtext="Student Report"  BackColor="White" Height="300px" >
       <br />
       <br />
       <table width="100%">        
         <td class="style1">
           <asp:UpdatePanel runat="server" ID="updtPanel">
           <asp:Label runat="server" ID="sdtReportLbl" ForeColor="Black" Font-Bold="true" Font-Size="Large"></asp:Label>           
           <asp:TextBox ID="studentEntryLocations" runat="server" Height="135px" TextMode="MultiLine" Width="300px" style="display:none"></asp:TextBox> 
           <asp:TextBox ID="studentExitLocations" runat="server" Height="135px"  TextMode="MultiLine" Width="234px" style="display:none"></asp:TextBox>         
           <asp:TextBox ID="check" runat="server"></asp:TextBox>
           <asp:HiddenField ID="hdnTxt" runat="server" />
           <asp:HiddenField ID="hdnTxt1" runat="server" />
           <asp:HiddenField runat="server" ID="hdnRow" />
           <asp:HiddenField runat="server" ID="hdnCellno"/>
           <asp:HiddenField runat="server" ID="hdnrowIndex" />
           <asp:HiddenField runat="server" ID="hdnGridId" />
           <asp:HiddenField runat="server" ID="hdnTextBoxId" />
           <asp:HiddenField runat="server" ID="hdnCell" />
           <asp:GridView ID="studentReportGrid" runat="server" AutoGenerateColumns="false" HeaderStyle-BackColor="#0080C0" 
           HeaderStyle-Width="200" HeaderStyle-ForeColor="White" Width="100%" RowStyle-HorizontalAlign="Left"> 
             <asp:templatefield HeaderText="Date&time" HeaderStyle-HorizontalAlign="Left">
             <asp:BoundField DataField="STUDENT_NAME" HeaderText="Student name" ReadOnly="True"  HeaderStyle-HorizontalAlign="Left" /> 
             <asp:BoundField DataField="STUDENT_ID" HeaderText="Id"  HeaderStyle-HorizontalAlign="Left"/>  
             <asp:BoundField DataField="VEHICLE_NUMBER" HeaderText="Vehicle_no"  HeaderStyle-HorizontalAlign="Left"/>  
             <asp:templatefield HeaderText="Entry_location" HeaderStyle-HorizontalAlign="Left">
             <asp:templatefield HeaderText="Exit_location" HeaderStyle-HorizontalAlign="Left">
         <asp:AsyncPostBackTrigger ControlID="generateReportbtn" EventName="click" />
         <asp:AsyncPostBackTrigger ControlID="hdnBtn" EventName="click" />
        <td align="right">                                     
          <asp:LinkButton ID="pdfLinkBtn" runat="server" onclick="pdfLinkBtn_Click" Text="Export to PDF"></asp:LinkButton>                           
     <br />
     <br />

Posted 15-Dec-12 1:05am
Updated 16-Dec-12 3:12am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You have to make sure that there are no extra columns in the grid. All the columns which displayed on the grid will be displayed as columns in the excel.
hasbina 16-Dec-12 0:12am
sir, no extra column.only 6 column.but in excel horizontal scroll bar
after the 6 columns and show only header row upto horizontal scroll bar
without header text.why?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 17 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100