程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#導出GridView數據到Excel文件類實例

C#導出GridView數據到Excel文件類實例

編輯:C#入門知識

C#導出GridView數據到Excel文件類實例。本站提示廣大學習愛好者:(C#導出GridView數據到Excel文件類實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#導出GridView數據到Excel文件類實例正文


本文實例講述了C#導出GridView數據到Excel文件類。分享給年夜家供年夜家參考。詳細以下:

這段C#代碼自界說了一個封裝類,用於將GridView數據導出到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;
      // 添加頭信息,為"文件下載/另存為"對話框指定默許文件名
      HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls");
      // 把文件流發送到客戶端
      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>");
      // 停滯頁面的履行
      //Response.End();
    }
    /// <summary>
    /// 導出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#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved