日期:2014-05-20  浏览次数:20446 次

关于ASP.net(C#)页面导出到Excel中的问题
页面上有一按钮,作用是将页面上的数据导出到Excel,但关键问题是如果使用后台代码往页面上写入数据,则动态写入的数据不会输出到Excel中,求解决方法,谢谢

前台代码:
excel.aspx
<%@   Page   Language= "C# "   AutoEventWireup= "true "   CodeFile= "excel.aspx.cs "   Inherits= "excel "   %>

<!DOCTYPE   html   PUBLIC   "-//W3C//DTD   XHTML   1.0   Transitional//EN "   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<html   xmlns= "http://www.w3.org/1999/xhtml "   >
<head   runat= "server ">
        <title> 无标题页 </title>
</head>
<body>
        <form   id= "form1 "   runat= "server ">
        <div>
                ajldjflasjdlfjsdfl <br   />
                <br   />
                <asp:Button   ID= "Button1 "   runat= "server "   OnClick= "Button1_Click "   Text= "Button "   /> </div>
        </form>
</body>
</html>


后台代码:
excel.aspx.cs
using   System;
using   System.Data;
using   System.Configuration;
using   System.Collections;
using   System.Web;
using   System.Web.Security;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.WebControls.WebParts;
using   System.Web.UI.HtmlControls;

public   partial   class   excel   :   System.Web.UI.Page
{
        protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                if   (!IsPostBack)
                {
                        //OutPutExcel();
                        Response.Write( "1111111111111 ");         //这一行数据不会输出到Excel中
                }
        }

        public   void   OutPutExcel()
                {
                        //定义文档类型、字符编码
                        Response.Clear();
                   Response.Buffer=   true;
                   Response.Charset= "GB2312 ";

                   Response.AppendHeader( "Content-Disposition ", "attachment;filename=FileFlow.xls ");
                   Response.ContentEncoding=System.Text.Encoding.GetEncoding( "GB2312 ");

                   Response.ContentType   =   "application/ms-excel ";
                   Page.EnableViewState   =   false;