C#導(dǎo)出GridView數(shù)據(jù)到Excel文件類(lèi)實(shí)例
本文實(shí)例講述了C#導(dǎo)出GridView數(shù)據(jù)到Excel文件類(lèi)。分享給大家供大家參考。具體如下:
這段C#代碼自定義了一個(gè)封裝類(lèi),用于將GridView數(shù)據(jù)導(dǎo)出到Excel文件
using System; using System.Web; using System.Web.UI; using System.IO; using System.Web.UI.WebControls; namespace DotNet.Utilities { public class ExportExcel { protected void ExportData(string strContent, string FileName) { FileName = FileName + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString(); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Charset = "gb2312"; HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; //this.Page.EnableViewState = false; // 添加頭信息,為"文件下載/另存為"對(duì)話(huà)框指定默認(rèn)文件名 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls"); // 把文件流發(fā)送到客戶(hù)端 HttpContext.Current.Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">"); HttpContext.Current.Response.Write(strContent); HttpContext.Current.Response.Write("</body></html>"); // 停止頁(yè)面的執(zhí)行 //Response.End(); } /// <summary> /// 導(dǎo)出Excel /// </summary> /// <param name="obj"></param> public void ExportData(GridView obj) { try { string style = ""; if (obj.Rows.Count > 0) { style = @"<style> .text { mso-number-format:\@; } </script> "; } else { style = "no data."; } HttpContext.Current.Response.ClearContent(); DateTime dt = DateTime.Now; string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString(); HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=ExportData" + filename + ".xls"); HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); obj.RenderControl(htw); HttpContext.Current.Response.Write(style); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); } catch { } } } }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C#導(dǎo)入導(dǎo)出EXCEL文件的代碼實(shí)例
- C#數(shù)據(jù)導(dǎo)入/導(dǎo)出Excel文件及winForm導(dǎo)出Execl總結(jié)
- C#導(dǎo)出數(shù)據(jù)到Excel文件的方法
- C#導(dǎo)出數(shù)據(jù)到CSV文件的通用類(lèi)實(shí)例
- C#實(shí)現(xiàn)pdf導(dǎo)出 .Net導(dǎo)出pdf文件
- C#導(dǎo)出生成excel文件的方法小結(jié)(xml,html方式)
- C#實(shí)現(xiàn)導(dǎo)出List數(shù)據(jù)到xml文件的方法【附demo源碼下載】
- C#如何使用SHBrowseForFolder導(dǎo)出中文文件夾詳解
相關(guān)文章
C#不重復(fù)輸出一個(gè)數(shù)組中所有元素的方法
這篇文章主要介紹了C#不重復(fù)輸出一個(gè)數(shù)組中所有元素的方法,涉及C#針對(duì)數(shù)組的遍歷、校驗(yàn)及排序等操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08利用C#編寫(xiě)Linux守護(hù)進(jìn)程實(shí)例代碼
如今的編程是一場(chǎng)程序員和上帝的競(jìng)賽,程序員要開(kāi)發(fā)出更大更好、傻瓜都會(huì)用到軟件,下面這篇文章主要給大家介紹了關(guān)于利用C#編寫(xiě)Linux守護(hù)進(jìn)程的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2018-01-01C#多線(xiàn)程與跨線(xiàn)程訪(fǎng)問(wèn)界面控件的方法
這篇文章主要介紹了C#多線(xiàn)程與跨線(xiàn)程訪(fǎng)問(wèn)界面控件的方法,實(shí)例分析了C#多線(xiàn)程與跨線(xiàn)程訪(fǎng)問(wèn)空間的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03C# TextBox控件實(shí)現(xiàn)只能輸入數(shù)字的方法
這篇文章主要介紹了C# TextBox控件實(shí)現(xiàn)只能輸入數(shù)字的方法,本文使用TextBox的keypress事件實(shí)現(xiàn)這個(gè)需求,需要的朋友可以參考下2015-06-06C# Dynamic關(guān)鍵字之:dynamic為什么比反射快的詳解
本篇文章是對(duì)C#中dynamic為什么比反射快進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05DevExpress實(shí)現(xiàn)GridControl同步列頭checkbox與列中checkbox狀態(tài)
這篇文章主要介紹了DevExpress實(shí)現(xiàn)GridControl同步列頭checkbox與列中checkbox狀態(tài),需要的朋友可以參考下2014-08-08C#中的矩形數(shù)組(多維數(shù)組)和鋸齒數(shù)組的實(shí)現(xiàn)
本文主要介紹了C#中的矩形數(shù)組(多維數(shù)組)和鋸齒數(shù)組的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04