using System.Data;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; /// <summary>
/// 导出EXCEL
/// </summary>
public class GridViewExportUtil
{
/// <summary>
/// 将GRIDVIEW导出excel
/// </summary>
/// <param name="fileName">excel文件名</param>
/// <param name="gv">GridView</param>
public static void Export(string fileName, GridView gv)
{
HttpContext.Current.Response.Clear();
//HttpContext.Current.Response.Charset = "GB2312";
//HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; 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 = gv.GridLines;
//table.BackColor = gv.BackColor;
// add the header row to the table
if (gv.HeaderRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
table.Rows[].BackColor = System.Drawing.Color.Black;
table.Rows[].ForeColor = System.Drawing.Color.White;
table.Rows[].Height = Unit.Pixel();
} // add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
GridViewExportUtil.PrepareControlForExport(row);
row.Height = Unit.Pixel();
table.Rows.Add(row);
} // add the footer row to the table
if (gv.FooterRow != null)
{
GridViewExportUtil.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();
}
}
} public static void Export(string fileName, DataTable dt)
{
using (GridView gv = new GridView())
{
gv.DataSource = dt;
gv.DataBind(); Export(fileName, gv);
}
} /// <summary>
/// Replace any of the contained controls with literals
/// </summary>
/// <param name="control"></param>
private static void PrepareControlForExport(Control control)
{
for (int i = ; 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())
{
GridViewExportUtil.PrepareControlForExport(current);
}
}
}
}

C# GridViewExportUtil的更多相关文章

  1. ASP.NET常用导出Excel方法汇总

    本文转载:http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html http://geekswithblogs.net/a ...

随机推荐

  1. Netty简单聊天室

    1.创建maven项目,在pom.xml中引入netty的jar包 <project xmlns="http://maven.apache.org/POM/4.0.0" xm ...

  2. Linux下vsftpd的安装,Java上传文件实现。

    首先我们需要查看是否已经安装vsftpd,输入命令 :vsftpd  -v.如果出现以下信息,那么就说明已经安装vsftpd 如果没有安装,那么输入命令   : yum  install vsftpd ...

  3. HTML&javaSkcript&CSS&jQuery&ajax-XSS

    一.CSS  标题隐藏 1. <sytle>h1.hidden {visibility: hidden;} </style> <body> <h1>这是 ...

  4. python+selenium十四:xpath和contains模糊匹配

    xpath可以以标签定位,也可以@任意属性: 如:以input标签定位:driver.find_element_by_xpath("//input[@id='kw']") 如:@t ...

  5. Fiddler导出Jmeter脚本

    版本:V4.4 用途:将fiddler抓取的请求,导出为jmx格式,方便jmeter直接调用 新增功能: 1.在测试计划下,新增[HTTP请求默认值],内容为空,后续需将站点的IP和端口填下在这个下面 ...

  6. truncate table时存在外键约束的解决办法

    以前在使用truncate命令时遇到表存在外键引用时无法执行命令的情况都是用delete来代替,今天又遇到这个问题,于是在网上搜了一把,可以通过如下方式解决: 1.基本思路:先关闭mysql的外键约束 ...

  7. 如何让谷歌浏览器支持跨域访问(AJAX) AJAX调试跨域接口

    以谷歌最新版本为例(2018) 1.在电脑上新建一个目录,例如:C:\MyChromeDevUserData 2.在属性页面中的目标输入框里加上   --disable-web-security -- ...

  8. Python-数据类型之数字

    一:数字类型概述 数字提供了标量存储和直接访问,属于不可变数据类型,所谓不可变,我们可以认为,更改数字的值会生成一个新的对象 # id可以唯一表示一个对象 age =18 print(id(age)) ...

  9. Java利用POI读取Excel

    官网直接下载POI  http://poi.apache.org/ package com.CommonUtil; import java.io.File; import java.io.FileIn ...

  10. StackService.Redis 应用

    如今StackService.Redis已经转向商业版本.4.0以下的低版本依然免费和开源. 吴双,Redis系列命令拾遗分享 http://www.cnblogs.com/tdws/tag/NoSq ...