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

首頁 > 編程 > .NET > 正文

ASP.NET使用GridView導出Excel實現方法

2024-07-10 13:28:33
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了ASP.NET使用GridView導出Excel實現方法,是asp.net操作office文件的一個典型應用,代碼中備有較為詳盡的注釋便于讀者理解,需要的朋友可以參考下
 
 

本文實例講述了ASP.NET使用GridView導出Excel實現方法。分享給大家供大家參考。具體實現方法如下:

 

復制代碼代碼如下:
/// <summary>  
/// 將DataTable數據導出到EXCEL,調用該方法后自動返回可下載的文件流  
/// </summary>  
/// <param name="dtData">要導出的數據源</param>  
public static void DataTable1Excel(System.Data.DataTable dtData)  
{  
        System.Web.UI.WebControls.GridView gvExport = null;  
        // 當前對話  
        System.Web.HttpContext curContext = System.Web.HttpContext.Current;  
        // IO用于導出并返回excel文件  
        System.IO.StringWriter strWriter = null;  
        System.Web.UI.HtmlTextWriter htmlWriter = null;  
  
        if (dtData != null)  
        {  
            // 設置編碼和附件格式  
            curContext.Response.ContentType = "application/vnd.ms-excel";  
            curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");  
            curContext.Response.Charset = "utf-8";  
  
            // 導出excel文件  
            strWriter = new System.IO.StringWriter();  
            htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);  
            // 為了解決gvData中可能進行了分頁的情況,需要重新定義一個無分頁的GridView  
            gvExport = new System.Web.UI.WebControls.GridView();  
            gvExport.DataSource = dtData.DefaultView;  
            gvExport.AllowPaging = false;  
            gvExport.DataBind();  
  
            // 返回客戶端  
            gvExport.RenderControl(htmlWriter);  
            curContext.Response.Write("<meta http-equiv=/"Content-Type/" content=/"text/html; charset=gb2312/" />" + strWriter.ToString());  
            curContext.Response.End();  
        }  
}  
  
/// <summary>  
/// 直接輸出Excel  
/// </summary>  
/// <param name="dtData"></param>  
public static void DataTable2Excel(System.Data.DataTable dtData)  
{  
          System.Web.UI.WebControls.DataGrid dgExport = null;  
      // 當前對話  
      System.Web.HttpContext curContext = System.Web.HttpContext.Current;  
      // IO用于導出并返回excel文件  
      System.IO.StringWriter strWriter = null;  
      System.Web.UI.HtmlTextWriter htmlWriter = null;  
  
      if (dtData != null)  
      {  
        // 設置編碼和附件格式  
        curContext.Response.ContentType = "application/vnd.ms-excel";  
        curContext.Response.ContentEncoding =System.Text.Encoding.UTF8;  
        curContext.Response.Charset = "";  
                  
        // 導出excel文件  
        strWriter = new System.IO.StringWriter();  
        htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);  
  
        // 為了解決dgData中可能進行了分頁的情況,需要重新定義一個無分頁的DataGrid  
        dgExport = new System.Web.UI.WebControls.DataGrid();           
        dgExport.DataSource = dtData.DefaultView;  
        dgExport.AllowPaging = false;  
        dgExport.DataBind();  
  
        // 返回客戶端  
        dgExport.RenderControl(htmlWriter);    
        curContext.Response.Write(strWriter.ToString());  
        curContext.Response.End();  
      }  
}

 

希望本文所述對大家的asp.net程序設計有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辽宁省| 星子县| 正蓝旗| 阿巴嘎旗| 双鸭山市| 额尔古纳市| 故城县| 德阳市| 江阴市| 华安县| 甘泉县| 乡宁县| 南澳县| 太白县| 平安县| 吉木萨尔县| 静乐县| 石林| 甘泉县| 平原县| 大安市| 托里县| 望城县| 万全县| 紫金县| 如皋市| 黄平县| 车致| 漳浦县| 昌都县| 明星| 邓州市| 红桥区| 杭锦旗| 响水县| 景宁| 永德县| 垫江县| 鄂州市| 米泉市| 绍兴县|