欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2015年03月25日 15:07:53   作者:lele  
這篇文章主要介紹了C#導(dǎo)出GridView數(shù)據(jù)到Excel文件類(lèi),實(shí)例分析了C#使用GridView及Excel的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(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ì)有所幫助。

相關(guān)文章

最新評(píng)論