濮阳杆衣贸易有限公司

主頁 > 知識庫 > ASP.NET使用GridView導出Excel實現(xiàn)方法

ASP.NET使用GridView導出Excel實現(xiàn)方法

熱門標簽:外呼電銷機器人軟件 400電話辦理最優(yōu)質 河南語音外呼系統(tǒng)公司 威海電銷 400免費電話怎么辦理 寧夏機器人電銷 t3出行地圖標注怎么做 河北網絡回撥外呼系統(tǒng) 關于宗地圖標注技術規(guī)范

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

復制代碼 代碼如下:
/// summary> 
/// 將DataTable數(shù)據(jù)導出到EXCEL,調用該方法后自動返回可下載的文件流 
/// /summary> 
/// param name="dtData">要導出的數(shù)據(jù)源/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程序設計有所幫助。

您可能感興趣的文章:
  • ASP.NET4 GridView的四種排序樣式詳解
  • asp.net中的GridView分頁問題
  • asp.net gridview分頁:第一頁 下一頁 1 2 3 4 上一頁 最末頁
  • ASP.NET數(shù)據(jù)綁定之GridView控件
  • ASP.NET數(shù)據(jù)綁定GridView控件使用技巧
  • 在ASP.NET 2.0中操作數(shù)據(jù)之十一:基于數(shù)據(jù)的自定義格式化
  • 在ASP.NET 2.0中操作數(shù)據(jù)之十二:在GridView控件中使用TemplateField
  • 在ASP.NET 2.0中操作數(shù)據(jù)之十三:在DetailsView控件中使用TemplateField
  • 在ASP.NET 2.0中操作數(shù)據(jù)之十四:使用FormView 的模板
  • 在ASP.NET 2.0中操作數(shù)據(jù)之十五:在GridView的頁腳中顯示統(tǒng)計信息

標簽:咸寧 賀州 廣元 淮北 樂山 固原 吉林 池州

巨人網絡通訊聲明:本文標題《ASP.NET使用GridView導出Excel實現(xiàn)方法》,本文關鍵詞  ASP.NET,使用,GridView,導出,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《ASP.NET使用GridView導出Excel實現(xiàn)方法》相關的同類信息!
  • 本頁收集關于ASP.NET使用GridView導出Excel實現(xiàn)方法的相關信息資訊供網民參考!
  • 推薦文章
    永和县| 蒙自县| 榆林市| 广宁县| 周至县| 南华县| 乌恰县| 阿合奇县| 古浪县| 台中市| 德昌县| 仪陇县| 成武县| 哈密市| 禹城市| 甘泉县| 无棣县| 金塔县| 林西县| 开封县| 渝中区| 西和县| 广丰县| 建湖县| 确山县| 从化市| 柳江县| 永登县| 南华县| 阿勒泰市| 巩留县| 万全县| 乐都县| 麻栗坡县| 革吉县| 绍兴市| 卫辉市| 新绛县| 龙里县| 曲沃县| 蒙阴县|