婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > ASP.NET使用GridView導出Excel實現方法

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

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

本文實例講述了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程序設計有所幫助。

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

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

巨人網絡通訊聲明:本文標題《ASP.NET使用GridView導出Excel實現方法》,本文關鍵詞  ASP.NET,使用,GridView,導出,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《ASP.NET使用GridView導出Excel實現方法》相關的同類信息!
  • 本頁收集關于ASP.NET使用GridView導出Excel實現方法的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 平陆县| 高州市| 绥江县| 桐乡市| 吉林省| 贡觉县| 贺州市| 普兰店市| 德钦县| 绥棱县| 常德市| 台中市| 德庆县| 西平县| 临洮县| 兰州市| 淳安县| 焉耆| 噶尔县| 全南县| 绍兴市| 出国| 舞阳县| 丹棱县| 蕉岭县| 常德市| 清新县| 建始县| 卓资县| 永定县| 武安市| 石首市| 广平县| 普安县| 临桂县| 长丰县| 池州市| 轮台县| 容城县| 扎兰屯市| 邢台市|