GridView在C#中如何导出Excel

发布时间:2021-01-29 15:57:52 作者:小新
来源:亿速云 阅读:234

小编给大家分享一下GridView在C#中如何导出Excel,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

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>
  /// 导出Grid的数据(全部)到Excel
  /// 字段全部为BoundField类型时可用
  /// 要是字段为TemplateField模板型时就取不到数据
  /// </summary>
  /// <param name="grid">grid的ID</param>
  /// <param name="dt">数据源</param>
  /// <param name="excelFileName">要导出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();
  }
 }
}

以上是“GridView在C#中如何导出Excel”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. c# gridview checkbox实现单选
  2. 怎么在C#中导出Excel文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

gridview excel

上一篇:使用tempfile库怎么创建一个python进程中的临时文件

下一篇:如何在win10环境中安装pytorch-gpu

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》