最近开始整理GridView绑定数据的方法
刚好在网上看到一支GridView数据写入excel的方法
故而将传好的数据使用打开excel
却卡了一下午也没有debug成功。。。
下面附上代码:
export GridView to excel_职场using System;
export GridView to excel_职场using System.Collections;
export GridView to excel_职场using System.Configuration;
export GridView to excel_职场using System.Data;
export GridView to excel_职场using System.Linq;
export GridView to excel_职场using System.Web;
export GridView to excel_职场using System.Web.Security;
export GridView to excel_职场using System.Web.UI;
export GridView to excel_职场using System.Web.UI.HtmlControls;
export GridView to excel_职场using System.Web.UI.WebControls;
export GridView to excel_职场using System.Web.UI.WebControls.WebParts;
export GridView to excel_职场using System.Xml.Linq;
export GridView to excel_职场using System.Web.Configuration;
export GridView to excel_职场using System.Data.SqlClient;
export GridView to excel_职场using System.IO;
export GridView to excel_职场using System.Globalization;
export GridView to excel_职场
export GridView to excel_职场public partial class GridView_GridView : System.Web.UI.Page
export GridView to excel_职场{
export GridView to excel_职场        string source = WebConfigurationManager.ConnectionStrings["KPRCSConnectionString"].ConnectionString;
export GridView to excel_职场        protected void Page_Load(object sender, EventArgs e)
export GridView to excel_职场        {
export GridView to excel_职场                GridBind();
export GridView to excel_职场        }
export GridView to excel_职场        protected void GridBind()
export GridView to excel_职场        {
export GridView to excel_职场                SqlConnection conn = new SqlConnection(source);
export GridView to excel_职场                conn.Open();
export GridView to excel_职场                string select = "select * from Relabel";
export GridView to excel_职场                SqlDataAdapter da = new SqlDataAdapter(select, conn);
export GridView to excel_职场                DataSet ds = new DataSet();
export GridView to excel_职场                da.Fill(ds);
export GridView to excel_职场                GV1.DataSource = ds;
export GridView to excel_职场                GV1.DataBind();
export GridView to excel_职场        }
export GridView to excel_职场        protected void GV1_pageIndexChanging(object sender, GridViewPageEventArgs e)
export GridView to excel_职场        {
export GridView to excel_职场                GV1.EditIndex = -1;
export GridView to excel_职场                GV1.PageIndex = e.NewPageIndex;
export GridView to excel_职场                GridBind();
export GridView to excel_职场        }
export GridView to excel_职场        public override void VerifyRenderingInServerForm(Control control)
export GridView to excel_职场        {
export GridView to excel_职场                //注释掉下面的代码,否则在asp.net2.0下会报错(注:GridView是asp.net 2.0下的控件,1.1下一些控件也可以导出成Excel或者Word)
export GridView to excel_职场                //base.VerifyRenderingInServerForm(control);
export GridView to excel_职场        }
export GridView to excel_职场
export GridView to excel_职场        protected void expExcel_Click(object sender, EventArgs e)
export GridView to excel_职场        {
export GridView to excel_职场                Response.Clear();
export GridView to excel_职场                Response.BufferOutput = true;
export GridView to excel_职场                Response.Charset = "GB2312";
export GridView to excel_职场                Response.AppendHeader("Content-Dispositio", "p_w_upload;filename=FileName.xls");
export GridView to excel_职场                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
export GridView to excel_职场                Response.ContentType = "application/ms-excel";
export GridView to excel_职场                EnableViewState = false;
export GridView to excel_职场                CultureInfo cultureInfo = new CultureInfo("ZH-CN", true);
export GridView to excel_职场                StringWriter stringWriter = new StringWriter(cultureInfo);
export GridView to excel_职场                HtmlTextWriter textWriter = new HtmlTextWriter(stringWriter);
export GridView to excel_职场                GV1.RenderControl(textWriter);
export GridView to excel_职场                Response.Write(textWriter.ToString());
export GridView to excel_职场                Response.End();
export GridView to excel_职场        }
export GridView to excel_职场}
 
错误如下:RegisterForEventValidation 只能在 Render(); 期間呼叫
根据搜索找到的问题解决方法如下:
1:在.aspx的页面题头<%Page ...>的地方添加EnableEventValidation = "false" AutoEventWireup="true",怀着侥幸的心理debug了下,却伤心的遇到了下面的问题,web页面上显示如下:
文件的最上層無效。處理資源 'http://localhost:6075/mySkills/GridView/GridView.aspx' 發生錯誤。第 1 行, 位置 1
System.Web.UI.HtmlTextWriter
^
这让我情何以堪呀~~~等待解决中【抽泣】