ASP.NET導(dǎo)出數(shù)據(jù)到Excel的實(shí)現(xiàn)方法
更新時間:2013年07月23日 11:02:29 作者:
在做asp.net程序時涉及到數(shù)據(jù)顯示的時候多數(shù)會要求打印,而網(wǎng)頁上的打印格式往往又不能滿足需求,經(jīng)常用的方法就是導(dǎo)入到Excel以后再進(jìn)行打印。(仿佛這已經(jīng)是老生常談)今天在網(wǎng)上搜了一段打印的代碼,覺得不錯,需要打印的朋友可以看看。
網(wǎng)上好些代碼的原理大致與此類似,同樣都存在一個問題,就是:
類型“GridView”的控件“ctl00_center_GridView1”必須放在具有 runat=server 的窗體標(biāo)記內(nèi)。 說明: 執(zhí)行當(dāng)前 Web 請求期間,出現(xiàn)未處理的異常。請檢查堆棧跟蹤信息,以了解有關(guān)該錯誤以及代碼中導(dǎo)致錯誤的出處的詳細(xì)信息。 異常詳細(xì)信息:System.Web.HttpException: 類型“GridView”的控件“ctl00_center_GridView1”必須放在具有 runat=server 的窗體標(biāo)記內(nèi)。
這段錯誤描述是我在注釋了這段程序是報(bào)的錯,
//publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
//{
// //base.VerifyRenderingInServerForm(control);
//}
雖然這個方法里的內(nèi)容也被注釋了,也就是說這是個空方法,但是如果沒有個方法,程序就會報(bào)上面那個錯誤。最初見到這段錯誤說明是想到了以前做ajax程序時報(bào)的一個錯誤很是類似。同樣是因?yàn)闆]有重寫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的摘要說明
///</summary>
publicclassExportHelper
{
publicstaticvoidExportToExcel(IListdataList,string[]fields,string[]headTexts,stringtitle)
{
GridViewgvw=newGridView();
intColCount,i;
//如果篩選的字段和對應(yīng)的列頭名稱個數(shù)相對的情況下只導(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">字段對應(yī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)前對話
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)行了分頁的情況,需要重新定義一個無分頁的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();
}
}
}
類型“GridView”的控件“ctl00_center_GridView1”必須放在具有 runat=server 的窗體標(biāo)記內(nèi)。 說明: 執(zhí)行當(dāng)前 Web 請求期間,出現(xiàn)未處理的異常。請檢查堆棧跟蹤信息,以了解有關(guān)該錯誤以及代碼中導(dǎo)致錯誤的出處的詳細(xì)信息。 異常詳細(xì)信息:System.Web.HttpException: 類型“GridView”的控件“ctl00_center_GridView1”必須放在具有 runat=server 的窗體標(biāo)記內(nèi)。
這段錯誤描述是我在注釋了這段程序是報(bào)的錯,
復(fù)制代碼 代碼如下:
//publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
//{
// //base.VerifyRenderingInServerForm(control);
//}
雖然這個方法里的內(nèi)容也被注釋了,也就是說這是個空方法,但是如果沒有個方法,程序就會報(bào)上面那個錯誤。最初見到這段錯誤說明是想到了以前做ajax程序時報(bào)的一個錯誤很是類似。同樣是因?yàn)闆]有重寫VerifyRenderingInServerForm方法所致。在此提醒使用的朋友注意,下面貼出導(dǎo)出到Excel的代碼
復(fù)制代碼 代碼如下:
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;
//如果篩選的字段和對應(yīng)的列頭名稱個數(shù)相對的情況下只導(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">字段對應(yī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)前對話
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)行了分頁的情況,需要重新定義一個無分頁的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();
}
}
}
您可能感興趣的文章:
- .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的簡單方法實(shí)例
- asp.net導(dǎo)出Excel類庫代碼分享
- 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)文章
IIS上部署Asp.net core Webapi的實(shí)現(xiàn)步驟
ASP.NET Core Web API是構(gòu)建RESTful應(yīng)用程序的理想平臺,本文主要介紹了IIS上部署Asp.net core Webapi的實(shí)現(xiàn)步驟,具有一定的參考價值,感興趣的可以了解一下2024-07-07
在ASP.NET中,設(shè)置Session的過期時間的方法
在ASP.NET中,設(shè)置Session的過期時間的方法,需要的朋友可以參考下2012-12-12
ASP.NET WebAPI2復(fù)雜請求跨域設(shè)置的方法介紹
這篇文章主要給大家介紹了關(guān)于ASP.NET WebAPI2復(fù)雜請求跨域設(shè)置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用ASP.NET具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
C#中遍歷各類數(shù)據(jù)集合的方法總結(jié)
C#中遍歷各類數(shù)據(jù)集合的方法,這里自己做下總結(jié):枚舉類型、遍歷ArrayList(Queue、Stack)、Winform窗體中的控件、HashTable哈希表等等,具體祥看下文2013-05-05
ASP.NET MVC5網(wǎng)站開發(fā)修改及刪除文章(十)
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC5網(wǎng)站開發(fā)修改及刪除文章,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-09-09
ASP.NET 清除模式窗口數(shù)據(jù)緩存的操作方式
模式窗口showModalDialog()彈出頁面在asp.net中經(jīng)常用到,接下來為大家介紹下清除模式窗口緩存數(shù)據(jù)的問題2013-04-04
擁有網(wǎng)頁版小U盤 ASP.NET實(shí)現(xiàn)文件上傳與下載功能
這篇文章主要為大家詳細(xì)介紹了ASP.NET實(shí)現(xiàn)文件上傳與下載功能,類似于U盤功能,具有一定的參考價值。感興趣的小伙伴們可以參考一下2016-08-08

