b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

Excel教程Excel函数Excel表格制作Excel2010Excel实用技巧Excel视频教程

电脑杂谈  发布时间:2021-05-30 09:01:54  来源:网络整理

C#Realize GridView 导出 Excel 示例代码

更新时间:2017年3月31日11:08:18 作者:rush_me

c#gridview 导出excel_net gridview 导出excel_c#gridview导出excel

本文主要介绍了GridView导出Excel示例代码的C#实现,这里有详细的代码,很有实用价值,有需要的朋友可以参考一下。

c#gridview导出excel_c#gridview 导出excel_net gridview 导出excel

Export Excel 在很多项目中经常使用。介绍了GridView导出Excel示例代码的C#实现,自己留个学习笔记吧。

c#gridview导出excel_c#gridview 导出excel_net gridview 导出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 description for GridViewExport
 /// 
 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();
    }
   }
  }
  /// 
  /// Replace any of the contained controls with literals
  /// 
  /// 
  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);
    }
   }
  }
  /// 
  /// 导出Grid的数据(全部)到Excel
  /// 字段全部为BoundField类型时可用
  /// 要是字段为TemplateField模板型时就取不到数据
  /// 
  /// grid的ID
  /// 数据源
  /// 要导出Excel的文件名
  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("" + fileName + "");
   int count = grid.Columns.Count;
   s.Append("");
   s.AppendLine("");
   for (int i = 0; i < count; i++)
   {
    if (grid.Columns[i].GetType() == typeof(BoundField))
     s.Append("");
    //s.Append("");
   }
   s.Append("");
   foreach (DataRow dr in dt.Rows)
   {
    s.AppendLine("");
    for (int n = 0; n < count; n++)
    {
     if (grid.Columns[n].Visible && grid.Columns[n].GetType() == typeof(BoundField))
      s.Append("");
    }
    s.AppendLine("");
   }
   s.Append("
" + grid.Columns[i].HeaderText + "" + grid.Columns[i].HeaderText + "
" + dr[((BoundField)grid.Columns[n]).DataField].ToString() + "
"); s.Append(""); page.Response.BinaryWrite(System.Text.Encoding.GetEncoding("utf-8").GetBytes(s.ToString())); page.Response.End(); } } }

以上是本文的全部内容。希望对大家的学习有所帮助,也希望大家多多支持Scripthome。


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shumachanpin/article-379144-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...