• ASP.NET导出Gridview中的数据到Excel
  • 发布于 1周前
  • 80 热度
    0 评论

Gridview控件是.NET开发中非常重要的一个控件,我们在开发基于.NET的B/S系统时经常会用到Gridview控件来显示列表清单信息,在基于实际的项目中,经常会遇到这样的需求:把Gridview中的数据导出到Excel中。那么我们该如何把Gridview中的数据导出到Excel呢?这里关键是要使用HtmlTextWriter对象来实现,具体实现步骤如下:

1.前台页面代码

1.1Gridview控件(这里用Linq绑定数据源)


<asp:GridView ID="GVList" runat="server" AllowPaging="true" PageSize="15" Width="99%" AllowSorting="true"
                DataKeyNames="ID" CssClass="gridViewStyle" AutoGenerateColumns="false">
                <HeaderStyle CssClass="headerStyle" />
                <RowStyle CssClass="rowStyle" />
                <EmptyDataRowStyle CssClass="emptyRowStyle" />
                <Columns>
                    <asp:BoundField DataField="ApplicantEmployeeNo" HeaderText="员工号<br/>EmployeeNo." SortExpression="OTDate"
                        HtmlEncode="false" />
                    <asp:BoundField DataField="ApplicantName" HeaderText="姓名<br/>Name" HtmlEncode="false" SortExpression="ApplicantName" />
                    <asp:BoundField DataField="ApplicantDept" HeaderText="部门<br/>Dept." HtmlEncode="false" SortExpression="ApplicantDept" />
                    <asp:BoundField HeaderText="加班日期起<br/>Date from" HtmlEncode="false" DataField="OTDate" SortExpression="OTDate"
                        DataFormatString="{0:yyyy-MM-dd}" />
                    <asp:BoundField HeaderText="加班时间起<br/>Time from" HtmlEncode="false" DataField="StartTime" SortExpression="StartTime"
                        DataFormatString="{0:HH:mm}" />
                    <asp:BoundField HeaderText="加班日期止<br>Date to" HtmlEncode="false" DataField="OTDate" SortExpression="OTDate"
                        DataFormatString="{0:yyyy-MM-dd}" />
                    <asp:BoundField HeaderText="加班时间止<br/>Time to" HtmlEncode="false" DataField="EndTime" SortExpression="EndTime"   DataFormatString="{0:HH:mm}" />
                </Columns>
                <RowStyle CssClass="rowStyle" />
            </asp:GridView>
            <asp:LinqDataSource ID="LinqDSAll" runat="server" ContextTypeName="Com.Kion.CN.NewOverTime.Model.NewOTDBDataContext"
                OrderBy="ID desc" TableName="V_AplicationList" AutoPage="true" AutoSort="true"
                OnSelecting="LinqDSAll_Selecting">
            </asp:LinqDataSource>
1.2按钮控件



 <asp:Button ID="btnExportGrid" runat="server" Text="导出列表清单数据>>" CssClass="btnStyle"
                            OnClick="btnExportGrid_Click" OnClientClick="clickOnce(this, '正在导出...')" UseSubmitBehavior="false" />

1.3前台页面需要加EnableEventValidation="false",否则系统会报错

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ApplicationList.aspx.cs" EnableEventValidation="false"
    Inherits="AskForLeave.WebUI.ApplicationList" Theme="Gray" %>

2.后端代码

记得先添加如下两个命名空间的引用

using System.IO;
using System.Web.UI;


        protected void btnExportGrid_Click(object sender, EventArgs e)
        {
            GVList.AllowPaging = false;
            StringWriter sw = new StringWriter();
            HtmlTextWriter htmlwrite = new HtmlTextWriter(sw);
            string excelName = "ExportList.xls";
            Response.ContentType = "application/ms-excel";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(excelName));
            GVList.DataBind();
            GVList.RenderControl(htmlwrite);
            Response.Write(sw.ToString());
            Response.Flush();
            Response.End();
            GVList.AllowPaging = true;
            GVList.DataBind();
        
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            // Confirms that an HtmlForm control is rendered for        
            //如果要限制控件必须位于<form runat=server>标记以内,则请先呼叫这个方法,才进行展现,如此一来,如果控件放在标记以外,便会显示错误信息。
        }
好了,就上面几段代码就可以轻松实现把Gridview中的数据导出到Excel的效果了,是不是很简单?代码直接复制就可以了

用户评论