ASP.NET使用GridView導(dǎo)出Excel實(shí)現(xiàn)方法
本文實(shí)例講述了ASP.NET使用GridView導(dǎo)出Excel實(shí)現(xiàn)方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
/// 將DataTable數(shù)據(jù)導(dǎo)出到EXCEL,調(diào)用該方法后自動(dòng)返回可下載的文件流
/// </summary>
/// <param name="dtData">要導(dǎo)出的數(shù)據(jù)源</param>
public static void DataTable1Excel(System.Data.DataTable dtData)
{
System.Web.UI.WebControls.GridView gvExport = null;
// 當(dāng)前對(duì)話
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于導(dǎo)出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
if (dtData != null)
{
// 設(shè)置編碼和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
curContext.Response.Charset = "utf-8";
// 導(dǎo)出excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
// 為了解決gvData中可能進(jìn)行了分頁(yè)的情況,需要重新定義一個(gè)無(wú)分頁(yè)的GridView
gvExport = new System.Web.UI.WebControls.GridView();
gvExport.DataSource = dtData.DefaultView;
gvExport.AllowPaging = false;
gvExport.DataBind();
// 返回客戶端
gvExport.RenderControl(htmlWriter);
curContext.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" />" + strWriter.ToString());
curContext.Response.End();
}
}
/// <summary>
/// 直接輸出Excel
/// </summary>
/// <param name="dtData"></param>
public static void DataTable2Excel(System.Data.DataTable dtData)
{
System.Web.UI.WebControls.DataGrid dgExport = null;
// 當(dāng)前對(duì)話
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于導(dǎo)出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = 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 = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
// 為了解決dgData中可能進(jìn)行了分頁(yè)的情況,需要重新定義一個(gè)無(wú)分頁(yè)的DataGrid
dgExport = new System.Web.UI.WebControls.DataGrid();
dgExport.DataSource = dtData.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind();
// 返回客戶端
dgExport.RenderControl(htmlWriter);
curContext.Response.Write(strWriter.ToString());
curContext.Response.End();
}
}
希望本文所述對(duì)大家的asp.net程序設(shè)計(jì)有所幫助。
- .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使用Npoi導(dǎo)入導(dǎo)出Excel的方法
- asp.net導(dǎo)出excel的簡(jiǎn)單方法實(shí)例
- asp.net導(dǎo)出Excel類庫(kù)代碼分享
- ASP.NET導(dǎo)出數(shù)據(jù)到Excel的實(shí)現(xiàn)方法
- 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)文章
.net indexOf(String.indexOf 方法)
字符串的IndexOf()方法搜索在該字符串上是否出現(xiàn)了作為參數(shù)傳遞的字符串,如果找到字符串,則返回字符的起始位置 (0表示第一個(gè)字符,1表示第二個(gè)字符依此類推)如果說(shuō)沒(méi)有找到則返回 -12012-10-10Visual Studio 2017 ASP.NET Core開(kāi)發(fā)
這篇文章主要為大家詳細(xì)介紹了Visual Studio 2017 ASP.NET Core開(kāi)發(fā),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Visual Studio 2010崩潰重啟問(wèn)題(源文件編譯崩潰)
在使用Visual Studio 2010突然出現(xiàn)崩潰現(xiàn)象:源文件修改只要一編譯,馬上就崩潰.搜索了很多的方法也不見(jiàn)效果,經(jīng)過(guò)近1天的折騰,已經(jīng)決定重裝Windows 7了,遇到此問(wèn)題的朋友們可以看看哦,或許對(duì)你有所幫助2013-01-01安裝 VS2005 SP1 有關(guān)問(wèn)題的解決辦法
安裝 VS2005 SP1 有關(guān)問(wèn)題的解決辦法...2007-04-04.NET使用YARP根據(jù)域名轉(zhuǎn)發(fā)實(shí)現(xiàn)反向代理
這篇文章介紹了.NET使用YARP根據(jù)域名轉(zhuǎn)發(fā)實(shí)現(xiàn)反向代理的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09ASP.NET Core中間件計(jì)算Http請(qǐng)求時(shí)間示例詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET Core中間件計(jì)算Http請(qǐng)求時(shí)間的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06ASP.NET中實(shí)現(xiàn)把Json數(shù)據(jù)轉(zhuǎn)換為ADO.NET DataSet對(duì)象
這篇文章主要介紹了ASP.NET中實(shí)現(xiàn)把Json數(shù)據(jù)轉(zhuǎn)換為ADO.NET DataSet對(duì)象,本文講解設(shè)計(jì)及實(shí)現(xiàn)方法,相關(guān)代碼托管到GITHUB,需要的朋友可以參考下2015-03-03asp.net實(shí)現(xiàn)根據(jù)城市獲取天氣預(yù)報(bào)的方法
這篇文章主要介紹了asp.net實(shí)現(xiàn)根據(jù)城市獲取天氣預(yù)報(bào)的方法,涉及asp.net調(diào)用新浪接口獲取天氣預(yù)報(bào)信息的實(shí)現(xiàn)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-12-12.NET Core 2.1中HttpClientFactory的最佳實(shí)踐記錄
這篇文章主要給大家介紹了關(guān)于.NET Core 2.1中HttpClientFactory的最佳實(shí)踐記錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用.net core2.1具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧2018-08-08