網上好些代碼的原理大致與此類似,同樣都存在一個問題,就是:
類型“GridView”的控件“ctl00_center_GridView1”必須放在具有 runat=server 的窗體標記內。 說明: 執行當前 Web 請求期間,出現未處理的異常。請檢查堆棧跟蹤信息,以了解有關該錯誤以及代碼中導致錯誤的出處的詳細信息。 異常詳細信息:System.Web.HttpException: 類型“GridView”的控件“ctl00_center_GridView1”必須放在具有 runat=server 的窗體標記內。
這段錯誤描述是我在注釋了這段程序是報的錯,
復制代碼 代碼如下:
//publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
//{
// //base.VerifyRenderingInServerForm(control);
//}
雖然這個方法里的內容也被注釋了,也就是說這是個空方法,但是如果沒有個方法,程序就會報上面那個錯誤。最初見到這段錯誤說明是想到了以前做ajax程序時報的一個錯誤很是類似。同樣是因為沒有重寫VerifyRenderingInServerForm方法所致。在此提醒使用的朋友注意,下面貼出導出到Excel的代碼
復制代碼 代碼如下:
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.IO;
///summary>
///ToExcleHelper的摘要說明
////summary>
publicclassExportHelper
{
publicstaticvoidExportToExcel(IListdataList,string[]fields,string[]headTexts,stringtitle)
{
GridViewgvw=newGridView();
intColCount,i;
//如果篩選的字段和對應的列頭名稱個數相對的情況下只導出指定的字段
if(fields.Length!=0fields.Length==headTexts.Length)
{
ColCount=fields.Length;
gvw.AutoGenerateColumns=false;
for(i=0;iColCount;i++)
{
BoundFieldbf=newBoundField();
bf.DataField=fields[i];
bf.HeaderText=headTexts[i];
gvw.Columns.Add(bf);
}
}
else
{
gvw.AutoGenerateColumns=true;
}
SetStype(gvw);
gvw.DataSource=dataList;
gvw.DataBind();
ExportToExcel(gvw,title);
}
///summary>
///導出數據到Excel
////summary>
///paramname="DataList">IListData/param>
///paramname="Fields">要導出的字段/param>
///paramname="HeadName">字段對應顯示的名稱/param>
publicstaticvoidExportToExcel(IListdataList,string[]fields,string[]headTexts)
{
ExportToExcel(dataList,fields,headTexts,string.Empty);
}
///summary>
///設置樣式
////summary>
///paramname="gvw">/param>
privatestaticvoidSetStype(GridViewgvw)
{
gvw.Font.Name="Verdana";
gvw.BorderStyle=System.Web.UI.WebControls.BorderStyle.Solid;
gvw.HeaderStyle.BackColor=System.Drawing.Color.LightCyan;
gvw.HeaderStyle.ForeColor=System.Drawing.Color.Black;
gvw.HeaderStyle.HorizontalAlign=System.Web.UI.WebControls.HorizontalAlign.Center;
gvw.HeaderStyle.Wrap=false;
gvw.HeaderStyle.Font.Bold=true;
gvw.HeaderStyle.Font.Size=10;
gvw.RowStyle.Font.Size=10;
}
///summary>
///導出GridView中的數據到Excel
////summary>
///paramname="gvw">/param>
///paramname="DataList">/param>
publicstaticvoidExportToExcel(GridViewgvw,stringtitle)
{
stringfileName;
HttpContext.Current.Response.Buffer=true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
fileName=string.Format("xhmd{0:yyMMddHHmm}.xls",DateTime.Now);
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+fileName);
HttpContext.Current.Response.ContentType="application/vnd.ms-excel";
StringWritertw=newSystem.IO.StringWriter();
HtmlTextWriterhw=newSystem.Web.UI.HtmlTextWriter(tw);
gvw.RenderControl(hw);
if(!string.IsNullOrEmpty(title))
{
HttpContext.Current.Response.Write("b>center>fontsize=3face=Verdanacolor=#0000FF>"+title+"/font>/center>/b>");
}
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
gvw.Dispose();
tw.Dispose();
hw.Dispose();
gvw=null;
tw=null;
hw=null;
}
publicstaticvoidDataTable2Excel(System.Data.DataTabledtData)
{
System.Web.UI.WebControls.DataGriddgExport=null;
//當前對話
System.Web.HttpContextcurContext=System.Web.HttpContext.Current;
//IO用于導出并返回excel文件
System.IO.StringWriterstrWriter=null;
System.Web.UI.HtmlTextWriterhtmlWriter=null;
if(dtData!=null)
{
//設置編碼和附件格式
curContext.Response.ContentType="application/vnd.ms-excel";
curContext.Response.ContentEncoding=System.Text.Encoding.UTF8;
curContext.Response.Charset="";
//導出excel文件
strWriter=newSystem.IO.StringWriter();
htmlWriter=newSystem.Web.UI.HtmlTextWriter(strWriter);
//為了解決dgData中可能進行了分頁的情況,需要重新定義一個無分頁的DataGrid
dgExport=newSystem.Web.UI.WebControls.DataGrid();
dgExport.DataSource=dtData.DefaultView;
dgExport.AllowPaging=false;
dgExport.DataBind();
//返回客戶端
dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
}
}
您可能感興趣的文章:- 直接在線預覽Word、Excel、TXT文件之ASP.NET
- ASP.NET使用GridView導出Excel實現方法
- asp.net導出excel數據的常見方法匯總
- Asp.net導出Excel/Csv文本格式數據的方法
- Asp.Net使用Npoi導入導出Excel的方法
- asp.net導出Excel亂碼的原因及解決方法
- asp.net使用npoi讀取excel模板并導出下載詳解
- .Net中導出數據到Excel(asp.net和winform程序中)
- asp.net生成Excel并導出下載五種實現方法
- ASP.NET導出Excel打開時提示:與文件擴展名指定文件不一致解決方法
- asp.net中如何批量導出access某表內容到word文檔
- asp.net 按指定模板導出word,pdf實例代碼
- asp.net+Ligerui實現grid導出Excel和Word的方法