Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi,
 
the code below export gridview data into excel,
 
 
 //Export gridview data into excel
        protected void excelLinkBtn_Click(object sender, EventArgs e)
        {
            Response.Clear();
            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);
            studentReportGrid.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
        }
 

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%">        
        <tr>          
         <td class="style1">
           <asp:UpdatePanel runat="server" ID="updtPanel">
           <ContentTemplate>
           <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"> 
            <Columns>  
             <asp:templatefield HeaderText="Date&time" HeaderStyle-HorizontalAlign="Left">
              <itemtemplate>
              </itemtemplate>
             </asp:templatefield>
             <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">
              <itemtemplate>
              </itemtemplate>
             </asp:templatefield>
             <asp:templatefield HeaderText="Exit_location" HeaderStyle-HorizontalAlign="Left">
              <itemtemplate>
              </itemtemplate>
            </asp:templatefield>
         </Columns>
        </asp:GridView> 
        </ContentTemplate>
        <Triggers>
         <asp:AsyncPostBackTrigger ControlID="generateReportbtn" EventName="click" />
         <asp:AsyncPostBackTrigger ControlID="hdnBtn" EventName="click" />
          
         </Triggers>
         </asp:UpdatePanel>          
       </td>
       </tr>
       <tr>        
        <td align="right">                                     
          <asp:LinkButton ID="pdfLinkBtn" runat="server" onclick="pdfLinkBtn_Click" Text="Export to PDF"></asp:LinkButton>                           
        </td>
       </tr>
     </table>
     <br />
     <br />
     </asp:Panel>
 
 

Thanks..
Posted 15-Dec-12 1:05am
hasbina1.2K
Edited 16-Dec-12 3:12am
v3

1 solution

Rate this: bad
good
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.
  Permalink  
Comments
hasbina at 16-Dec-12 0:12am
   
@Akbar
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
0 Sergey Alexandrovich Kryukov 6,805
1 OriginalGriff 6,696
2 CPallini 5,350
3 George Jonsson 3,609
4 Gihan Liyanage 2,792


Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 17 Dec 2012
Copyright © CodeProject, 1999-2014
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