将gridview数据导出为ex​​cel。 [英] export gridview data into excel.?

查看:59
本文介绍了将gridview数据导出为ex​​cel。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





下面的代码将gridview数据导出到excel,



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





但是,它会添加额外的标题行G,H,I字母表。



如何设置范围到gridview列?



gridview在面板内如下







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

推荐答案

你必须确保疗法e网格中没有额外的列。网格上显示的所有列都将显示为excel中的列。
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.


这篇关于将gridview数据导出为ex​​cel。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆