C#導(dǎo)出GridView數(shù)據(jù)到Excel文件類實例
更新時間:2015年03月25日 15:07:53 作者:lele
這篇文章主要介紹了C#導(dǎo)出GridView數(shù)據(jù)到Excel文件類,實例分析了C#使用GridView及Excel的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C#導(dǎo)出GridView數(shù)據(jù)到Excel文件類。分享給大家供大家參考。具體如下:
這段C#代碼自定義了一個封裝類,用于將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; // 添加頭信息,為"文件下載/另存為"對話框指定默認(rèn)文件名 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls"); // 把文件流發(fā)送到客戶端 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>"); // 停止頁面的執(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 { } } } }
希望本文所述對大家的C#程序設(shè)計有所幫助。
您可能感興趣的文章:
- C#導(dǎo)入導(dǎo)出EXCEL文件的代碼實例
- 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文件的通用類實例
- C#實現(xiàn)pdf導(dǎo)出 .Net導(dǎo)出pdf文件
- C#導(dǎo)出生成excel文件的方法小結(jié)(xml,html方式)
- C#實現(xiàn)導(dǎo)出List數(shù)據(jù)到xml文件的方法【附demo源碼下載】
- C#如何使用SHBrowseForFolder導(dǎo)出中文文件夾詳解
相關(guān)文章
C# TextBox控件實現(xiàn)只能輸入數(shù)字的方法
這篇文章主要介紹了C# TextBox控件實現(xiàn)只能輸入數(shù)字的方法,本文使用TextBox的keypress事件實現(xiàn)這個需求,需要的朋友可以參考下2015-06-06C# Dynamic關(guān)鍵字之:dynamic為什么比反射快的詳解
本篇文章是對C#中dynamic為什么比反射快進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05DevExpress實現(xiàn)GridControl同步列頭checkbox與列中checkbox狀態(tài)
這篇文章主要介紹了DevExpress實現(xiàn)GridControl同步列頭checkbox與列中checkbox狀態(tài),需要的朋友可以參考下2014-08-08C#中的矩形數(shù)組(多維數(shù)組)和鋸齒數(shù)組的實現(xiàn)
本文主要介紹了C#中的矩形數(shù)組(多維數(shù)組)和鋸齒數(shù)組的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04