C# GridViewExportUtil
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的更多相关文章
- ASP.NET常用导出Excel方法汇总
本文转载:http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html http://geekswithblogs.net/a ...
随机推荐
- Python基础之封装
一.什么是封装 在程序设计中,封装(Encapsulation)是对具体对象的一种抽象,即将某些部分隐藏起来,在程序外部看不到,其 含义是其他程序无法调用. 要了解封装,离不开“私有化”,就是将类或者 ...
- 剑指offer 二叉搜索树和双向链表
剑指offer 牛客网 二叉搜索树和双向链表 # -*- coding: utf-8 -*- """ Created on Tue Apr 9 18:58:36 2019 ...
- java Properties (属性集)
加载Properties Properties downloadLog = new Properties(); try { //加载logFile文件 downloadLog.load(new Fil ...
- 安装AngularJS Batarang遇到的问题
AngularJS Batarang是AngularJS在谷歌浏览器上的一个调试工具,因为国内目前无法访问谷歌浏览器应用商店,所以Batarang只能离线安装.不过在安装这个插件的过程中遇到了一些麻烦 ...
- 调试阶段 获取微信小程序openid
wx.login({ success: function(res) { //首先获取用户code //res.code wx.request({ url: 'https://api.weixin.qq ...
- ubuntu下配置反向代理
1. 环境 ubuntu:Ubuntu 13.04 x86-64 apache2: 2.2.22-6ubuntu5.1 amd64 2. 配置 2.1 配置应用 增加监听端口 打开/etc/apac ...
- 步步为营-81-HttpModule(再谈Session)
说明:session用于记录数据信息并存放在服务器内存中,但是存在一些问题.例如当使用服务器集群是会出现session丢失等情况.虽然微软提供了一些解决方案(Session进程外存储,或者存到数据库中 ...
- Pymysql-总结
背景:工作需要大量链接数据库进行一些操作查询,但是也会有出现异常情况 1.添加字段 1 ALTER TABLE app01_student ADD COLUMN Relation VARCHAR(25 ...
- windows解压.tar00文件
通常是单个文件太大分拆出来的,例如data.tar00, data.tar01, data.tar02等 cmd命令行进入几个tar0x文件所在目录,执行: copy /b data.tar0* da ...
- Windows 添加永久静态路由
route add -p 10.10.0.0 mask 255.255.0.0 10.10.6.1 -p 参数: p 即 persistent 的意思 -p 表示将路由表项永久加入系统注册表