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)行了分頁的情況,需要重新定義一個(gè)無分頁的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)行了分頁的情況,需要重新定義一個(gè)無分頁的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類庫代碼分享
- 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)文章
總結(jié)Visual Studio下ASP.NET模板化控件中的數(shù)據(jù)綁定
.NET框架中提供了很多數(shù)據(jù)綁定的組件,這里我們就來總結(jié)Visual Studio下ASP.NET模板化控件中的數(shù)據(jù)綁定,需要的朋友可以參考下2016-06-06
ASP.NET Core 數(shù)據(jù)保護(hù)(Data Protection 集群場(chǎng)景)下篇
這篇文章主要為大家再一次介紹了ASP.NET Core 數(shù)據(jù)保護(hù)(Data Protection),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
使用Hangfire+.NET?6實(shí)現(xiàn)定時(shí)任務(wù)管理(推薦)
這篇文章主要介紹了使用Hangfire+.NET?6實(shí)現(xiàn)定時(shí)任務(wù)管理,通過引入Hangfire相關(guān)的Nuget包并對(duì)Hangfire進(jìn)行服務(wù)配置,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
ASP.NET Core 1.0 部署 HTTPS(.NET Core 1.0)
這篇文章主要為大家詳細(xì)介紹了ASP.NET Core 1.0 部署 HTTPS(.NET Core 1.0),感興趣的小伙伴們可以參考一下2016-07-07
.NET Core 中實(shí)現(xiàn)異步編程并提升性能的操作方法
在.net core中異步編程主要通過async和await關(guān)鍵字來實(shí)現(xiàn),結(jié)合Task類進(jìn)行異步操作的管理,這篇文章主要介紹了.NET Core 中實(shí)現(xiàn)異步編程并提升性能的操作方法,需要的朋友可以參考下2024-12-12
ASP.NET MVC下拉框聯(lián)動(dòng)實(shí)例解析
這篇文章主要為大家詳細(xì)介紹了ASP.NET MVC下拉框聯(lián)動(dòng)實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2016-07-07
ASP.NET用戶注冊(cè)實(shí)戰(zhàn)(第11節(jié))
這篇文章主要介紹了ASP.NET用戶注冊(cè)實(shí)戰(zhàn),鞏固前10小節(jié)所學(xué)的全部知識(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-08-08
asp.net Repeater分頁實(shí)例(PageDataSource的使用)
Asp.net提供了三個(gè)功能強(qiáng)大的列表控件:DataGrid、DataList和Repeater控件,但其中只有DataGrid控件提供分頁功能。相對(duì)DataGrid,DataList和Repeater控件具有更高的樣式自定義性,所以很多時(shí)候我們喜歡使用DataList或Repeater控件來顯示數(shù)據(jù)2013-04-04

