ASP.NET導(dǎo)出數(shù)據(jù)到Excel的實(shí)現(xiàn)方法
類(lèi)型“GridView”的控件“ctl00_center_GridView1”必須放在具有 runat=server 的窗體標(biāo)記內(nèi)。 說(shuō)明: 執(zhí)行當(dāng)前 Web 請(qǐng)求期間,出現(xiàn)未處理的異常。請(qǐng)檢查堆棧跟蹤信息,以了解有關(guān)該錯(cuò)誤以及代碼中導(dǎo)致錯(cuò)誤的出處的詳細(xì)信息。 異常詳細(xì)信息:System.Web.HttpException: 類(lèi)型“GridView”的控件“ctl00_center_GridView1”必須放在具有 runat=server 的窗體標(biāo)記內(nèi)。
這段錯(cuò)誤描述是我在注釋了這段程序是報(bào)的錯(cuò),
//publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
//{
// //base.VerifyRenderingInServerForm(control);
//}
雖然這個(gè)方法里的內(nèi)容也被注釋了,也就是說(shuō)這是個(gè)空方法,但是如果沒(méi)有個(gè)方法,程序就會(huì)報(bào)上面那個(gè)錯(cuò)誤。最初見(jiàn)到這段錯(cuò)誤說(shuō)明是想到了以前做ajax程序時(shí)報(bào)的一個(gè)錯(cuò)誤很是類(lèi)似。同樣是因?yàn)闆](méi)有重寫(xiě)VerifyRenderingInServerForm方法所致。在此提醒使用的朋友注意,下面貼出導(dǎo)出到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的摘要說(shuō)明
///</summary>
publicclassExportHelper
{
publicstaticvoidExportToExcel(IListdataList,string[]fields,string[]headTexts,stringtitle)
{
GridViewgvw=newGridView();
intColCount,i;
//如果篩選的字段和對(duì)應(yīng)的列頭名稱(chēng)個(gè)數(shù)相對(duì)的情況下只導(dǎo)出指定的字段
if(fields.Length!=0&&fields.Length==headTexts.Length)
{
ColCount=fields.Length;
gvw.AutoGenerateColumns=false;
for(i=0;i<ColCount;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>
///導(dǎo)出數(shù)據(jù)到Excel
///</summary>
///<paramname="DataList">IListData</param>
///<paramname="Fields">要導(dǎo)出的字段</param>
///<paramname="HeadName">字段對(duì)應(yīng)顯示的名稱(chēng)</param>
publicstaticvoidExportToExcel(IListdataList,string[]fields,string[]headTexts)
{
ExportToExcel(dataList,fields,headTexts,string.Empty);
}
///<summary>
///設(shè)置樣式
///</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>
///導(dǎo)出GridView中的數(shù)據(jù)到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;
//當(dāng)前對(duì)話(huà)
System.Web.HttpContextcurContext=System.Web.HttpContext.Current;
//IO用于導(dǎo)出并返回excel文件
System.IO.StringWriterstrWriter=null;
System.Web.UI.HtmlTextWriterhtmlWriter=null;
if(dtData!=null)
{
//設(shè)置編碼和附件格式
curContext.Response.ContentType="application/vnd.ms-excel";
curContext.Response.ContentEncoding=System.Text.Encoding.UTF8;
curContext.Response.Charset="";
//導(dǎo)出excel文件
strWriter=newSystem.IO.StringWriter();
htmlWriter=newSystem.Web.UI.HtmlTextWriter(strWriter);
//為了解決dgData中可能進(jìn)行了分頁(yè)的情況,需要重新定義一個(gè)無(wú)分頁(yè)的DataGrid
dgExport=newSystem.Web.UI.WebControls.DataGrid();
dgExport.DataSource=dtData.DefaultView;
dgExport.AllowPaging=false;
dgExport.DataBind();
//返回客戶(hù)端
dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
}
}
- .NET6導(dǎo)入和導(dǎo)出EXCEL
- Asp.Net Core實(shí)現(xiàn)Excel導(dǎo)出功能的實(shí)現(xiàn)方法
- ASP.NET Core 導(dǎo)入導(dǎo)出Excel xlsx 文件實(shí)例
- asp.net DataTable導(dǎo)出Excel自定義列名的方法
- ASP.NET使用GridView導(dǎo)出Excel實(shí)現(xiàn)方法
- Asp.Net使用Npoi導(dǎo)入導(dǎo)出Excel的方法
- asp.net導(dǎo)出excel的簡(jiǎn)單方法實(shí)例
- asp.net導(dǎo)出Excel類(lèi)庫(kù)代碼分享
- Asp.net中DataTable導(dǎo)出到Excel的方法介紹
- ASP.NET用DataSet導(dǎo)出到Excel的方法
- asp.net GridView導(dǎo)出到Excel代碼
- ASP.NET MVC把表格導(dǎo)出到Excel
相關(guān)文章
ASP.NET也像WinForm程序一樣運(yùn)行的實(shí)現(xiàn)方法
我們今天要談到的是讓ASP.NET的程序也像WinForm一樣的運(yùn)行,這樣就不需要安裝IIS或者Visual Studio這樣的特定環(huán)境了2012-01-01未能加載文件或程序集“XXX”或它的某一個(gè)依賴(lài)項(xiàng)。試圖加載格式不正確的程序。
如果你將應(yīng)用程序生成x86而不是Any CPU時(shí),在64位操作系統(tǒng)中不會(huì)出錯(cuò)錯(cuò)誤,而在32位操作系統(tǒng)中可能會(huì)出現(xiàn)以下錯(cuò)誤2012-11-11ASP.NET MVC5網(wǎng)站開(kāi)發(fā)用戶(hù)修改資料和密碼(六)
這篇文章主要介紹了ASP.NET MVC5網(wǎng)站開(kāi)發(fā)用戶(hù)修改資料和密碼,本文即將結(jié)束member區(qū)域的用戶(hù)部分,感興趣的小伙伴們可以參考一下2015-09-09在A(yíng)SP.NET Core中實(shí)現(xiàn)一個(gè)Token base的身份認(rèn)證實(shí)例
以前在web端的身份認(rèn)證都是基于Cookie | Session的身份認(rèn)證,本篇文章主要介紹了在A(yíng)SP.NET Core中實(shí)現(xiàn)一個(gè)Token base的身份認(rèn)證實(shí)例,有興趣的可以了解一下。2016-12-12asp.net中TextBox只能輸入數(shù)字的最簡(jiǎn)潔的兩種方法
這篇文章介紹了asp.net中TextBox只能輸入數(shù)字的最簡(jiǎn)潔的兩種方法,有需要的朋友可以參考一下2013-11-11深入Lumisoft.NET實(shí)現(xiàn)郵件發(fā)送功能的方法詳解
本篇文章對(duì)使用Lumisoft.NET實(shí)現(xiàn)郵件發(fā)送功能的方法機(jī)型了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05asp.net fileupload控件上傳文件與多文件上傳
這篇文章主要介紹了asp.net fileupload控件上傳文件的方法,fileupload控件多文件上傳,以及fileupload上傳時(shí)實(shí)現(xiàn)文件驗(yàn)證的方法,需要的朋友可以參考下2014-11-11asp.net基礎(chǔ)學(xué)習(xí)之控件的使用方法
這篇文章主要為大家詳細(xì)介紹了asp.net基礎(chǔ)學(xué)習(xí)之控件的使用方法,感興趣的小伙伴們可以參考一下2016-08-08