C#导出GridView数据到Excel文件类实例
家电修理 2023-07-16 19:17www.caominkang.com电器维修
本文实例讲述了C#导出GridVie数据到Excel文件类。分享给大家供大家参考。具体如下
这段C#代码自定义了一个封装类,用于将GridVie数据导出到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.No.Year.ToString() + DateTime.No.Month.ToString() + DateTime.No.Day.ToString() + DateTime.No.Hour.ToString() + DateTime.No.Minute.ToString() + DateTime.No.Second.ToString() + DateTime.No.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.EnableVieState = false; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls"); // 把文件流发送到客户端 HttpContext.Current.Response.Write(""); HttpContext.Current.Response.Write(strContent); HttpContext.Current.Response.Write(""); // 停止页面的执行 //Response.End(); } ////// 导出Excel /// /// public void ExportData(GridVie obj) { try { string style = ""; if (obj.Ros.Count > 0) { style = @".text { mso-number-format:@; } "; } else { style = "no data."; } HttpContext.Current.Response.ClearContent(); DateTime dt = DateTime.No; 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 s = ne StringWriter(); HtmlTextWriter ht = ne HtmlTextWriter(s); obj.RenderControl(ht); HttpContext.Current.Response.Write(style); HttpContext.Current.Response.Write(s.ToString()); HttpContext.Current.Response.End(); } catch { } } } }
电脑维修网希望本文所述对大家的C#程序设计有所帮助。
空调维修
- 温岭冰箱全国统一服务热线-全国统一人工【7X2
- 荆州速热热水器维修(荆州热水器维修)
- 昆山热水器故障码5ER-昆山热水器故障码26
- 温岭洗衣机24小时服务电话—(7X24小时)登记报
- 统帅热水器售后维修服务电话—— (7X24小时)登
- 阳江中央空调统一电话热线-阳江空调官方售后电
- 乌鲁木齐阳春燃气灶厂家服务热线
- 珠海许昌集成灶售后服务电话-全国统一人工【
- 乌鲁木齐中央空调维修服务专线-乌鲁木齐中央空
- 新沂热水器故障电话码维修-新沂热水器常见故障
- 诸城壁挂炉24小时服务热线电话
- 靖江空调24小时服务电话-——售后维修中心电话
- 空调室外滴水管维修(空调室外排水管维修)
- 九江壁挂炉400全国服务电话-(7X24小时)登记报修
- 热水器故障码f.22怎么解决-热水器f0故障解决方法
- 营口热水器售后维修服务电话—— 全国统一人工