最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實現(xiàn)GridView導(dǎo)出Excel實例代碼

 更新時間:2017年03月31日 11:08:18   作者:rush_me  
本篇文章主要介紹了C#實現(xiàn)GridView導(dǎo)出Excel實例代碼,這里整理了詳細的代碼,非常具有實用價值,需要的朋友可以參考下。

導(dǎo)出Excel在很多項目中經(jīng)常用到,本人介紹了C#實現(xiàn)GridView導(dǎo)出Excel實例代碼,也全當給自己留下個學(xué)習(xí)筆記了。

using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;

namespace DotNet.Utilities
{
 /// <summary>
 /// Summary description for GridViewExport
 /// </summary>
 public class GridViewExport
 {
  public GridViewExport()
  {
   //
   // TODO: Add constructor logic here
   //
  }

  public static void Export(string fileName, GridView gv)
  {
   HttpContext.Current.Response.Clear();
   HttpContext.Current.Response.AddHeader(
    "content-disposition", string.Format("attachment; filename={0}", fileName));
   HttpContext.Current.Response.ContentType = "application/ms-excel";
   //HttpContext.Current.Response.Charset = "utf-8";


   using (StringWriter sw = new StringWriter())
   {
    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
    {
     // Create a form to contain the grid
     Table table = new Table();
     table.GridLines = GridLines.Both; //單元格之間添加實線

     // add the header row to the table
     if (gv.HeaderRow != null)
     {
      PrepareControlForExport(gv.HeaderRow);
      table.Rows.Add(gv.HeaderRow);
     }

     // add each of the data rows to the table
     foreach (GridViewRow row in gv.Rows)
     {
      PrepareControlForExport(row);
      table.Rows.Add(row);
     }

     // add the footer row to the table
     if (gv.FooterRow != null)
     {
      PrepareControlForExport(gv.FooterRow);
      table.Rows.Add(gv.FooterRow);
     }

     // render the table into the htmlwriter
     table.RenderControl(htw);

     // render the htmlwriter into the response
     HttpContext.Current.Response.Write(sw.ToString());
     HttpContext.Current.Response.End();
    }
   }
  }

  /// <summary>
  /// Replace any of the contained controls with literals
  /// </summary>
  /// <param name="control"></param>
  private static void PrepareControlForExport(Control control)
  {
   for (int i = 0; i < control.Controls.Count; i++)
   {
    Control current = control.Controls[i];
    if (current is LinkButton)
    {
     control.Controls.Remove(current);
     control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
    }
    else if (current is ImageButton)
    {
     control.Controls.Remove(current);
     control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
    }
    else if (current is HyperLink)
    {
     control.Controls.Remove(current);
     control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
    }
    else if (current is DropDownList)
    {
     control.Controls.Remove(current);
     control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
    }
    else if (current is CheckBox)
    {
     control.Controls.Remove(current);
     control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
    }

    if (current.HasControls())
    {
     PrepareControlForExport(current);
    }
   }
  }


  /// <summary>
  /// 導(dǎo)出Grid的數(shù)據(jù)(全部)到Excel
  /// 字段全部為BoundField類型時可用
  /// 要是字段為TemplateField模板型時就取不到數(shù)據(jù)
  /// </summary>
  /// <param name="grid">grid的ID</param>
  /// <param name="dt">數(shù)據(jù)源</param>
  /// <param name="excelFileName">要導(dǎo)出Excel的文件名</param>
  public static void OutputExcel(GridView grid, DataTable dt, string excelFileName)
  {
   Page page = (Page)HttpContext.Current.Handler;
   page.Response.Clear();
   string fileName = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(excelFileName));
   page.Response.AddHeader("Content-Disposition", "attachment:filename=" + fileName + ".xls");
   page.Response.ContentType = "application/vnd.ms-excel";
   page.Response.Charset = "utf-8";

   StringBuilder s = new StringBuilder();
   s.Append("<HTML><HEAD><TITLE>" + fileName + "</TITLE><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>");

   int count = grid.Columns.Count;

   s.Append("<table border=1>");
   s.AppendLine("<tr>");
   for (int i = 0; i < count; i++)
   {

    if (grid.Columns[i].GetType() == typeof(BoundField))
     s.Append("<td>" + grid.Columns[i].HeaderText + "</td>");

    //s.Append("<td>" + grid.Columns[i].HeaderText + "</td>");

   }
   s.Append("</tr>");

   foreach (DataRow dr in dt.Rows)
   {
    s.AppendLine("<tr>");
    for (int n = 0; n < count; n++)
    {
     if (grid.Columns[n].Visible && grid.Columns[n].GetType() == typeof(BoundField))
      s.Append("<td>" + dr[((BoundField)grid.Columns[n]).DataField].ToString() + "</td>");

    }
    s.AppendLine("</tr>");
   }

   s.Append("</table>");
   s.Append("</body></html>");

   page.Response.BinaryWrite(System.Text.Encoding.GetEncoding("utf-8").GetBytes(s.ToString()));
   page.Response.End();
  }

 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#實現(xiàn)DataTable數(shù)據(jù)行列轉(zhuǎn)換

    C#實現(xiàn)DataTable數(shù)據(jù)行列轉(zhuǎn)換

    這篇文章介紹了C#實現(xiàn)DataTable數(shù)據(jù)行列轉(zhuǎn)換的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#項目中跨文件調(diào)用公共類的實例方法

    C#項目中跨文件調(diào)用公共類的實例方法

    在本篇文章里小編給大家整理的是關(guān)于C#項目中如何跨文件調(diào)用公共類的知識點內(nèi)容,需要的朋友們學(xué)習(xí)下。
    2019-08-08
  • 詳解C# Lazy Loading(延遲加載)

    詳解C# Lazy Loading(延遲加載)

    這篇文章主要介紹了C# Lazy Loading(延遲加載)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • C# SortedList排序列表的實現(xiàn)

    C# SortedList排序列表的實現(xiàn)

    本文主要介紹了C# SortedList排序列表的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • c# 如何實現(xiàn)獲取二維數(shù)組的列數(shù)

    c# 如何實現(xiàn)獲取二維數(shù)組的列數(shù)

    這篇文章主要介紹了c# 實現(xiàn)獲取二維數(shù)組的列數(shù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • C#實現(xiàn)登錄窗口(不用隱藏)

    C#實現(xiàn)登錄窗口(不用隱藏)

    C#登錄窗口的實現(xiàn),特點就是不用隱藏,感興趣的朋友不要錯過
    2013-11-11
  • WinForm中Application.Idle方法詳解

    WinForm中Application.Idle方法詳解

    本文詳細講解了WinForm中的Application.Idle方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • C# 批量生成隨機密碼必須包含數(shù)字和字母并用加密算法加密

    C# 批量生成隨機密碼必須包含數(shù)字和字母并用加密算法加密

    這篇文章主要介紹了C# 批量生成隨機密碼必須包含數(shù)字和字母并用加密算法加密,需要的朋友參考下
    2017-01-01
  • WPF實現(xiàn)頁面的切換的示例代碼

    WPF實現(xiàn)頁面的切換的示例代碼

    本文主要介紹了WPF實現(xiàn)頁面的切換的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • C#微信開發(fā)之啟用開發(fā)者模式

    C#微信開發(fā)之啟用開發(fā)者模式

    本文主要介紹了C#微信開發(fā)中啟用開發(fā)者模式的步驟與方法,具有一定的參考價值,下面跟著小編一起來看下吧
    2017-02-02

最新評論

通海县| 东丽区| 杭锦后旗| 保德县| 噶尔县| 长治市| 德格县| 库伦旗| 溆浦县| 白朗县| 四川省| 岳普湖县| 神农架林区| 宁蒗| 宁城县| 无极县| 基隆市| 馆陶县| 景东| 曲阜市| 宜君县| 两当县| 天水市| 渭源县| 永嘉县| 图木舒克市| 会理县| 成武县| 义乌市| 卢龙县| 鄱阳县| 乌鲁木齐县| 全州县| 新泰市| 于田县| 林州市| 台东市| 伊吾县| 洞头县| 广宗县| 龙川县|