国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C#導(dǎo)出GridView數(shù)據(jù)到Excel文件類(lèi)實(shí)例

2020-01-24 02:06:11
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(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ì)話框指定默認(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ì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 河津市| 宁国市| 化州市| 长葛市| 方城县| 股票| 万安县| 元谋县| 若尔盖县| 唐海县| 富川| 遵义市| 休宁县| 博白县| 得荣县| 定襄县| 天祝| 海原县| 池州市| 大同县| 黎川县| 开远市| 庆城县| 亚东县| 大宁县| 金堂县| 岳阳县| 宿州市| 古田县| 新宁县| 江西省| 房山区| 镇雄县| 五河县| 温宿县| 安吉县| 紫阳县| 伊通| 巧家县| 石门县| 肥东县|