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 ...
随机推荐
- js instanceof 检测某个对象是否是另一个对象的实例
例如File对象 $("#file").change(function(){ console.log($(this)[0].files[0]) console.log($(this ...
- 史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)
在上一篇文章,讲了服务的注册和发现.在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式,一种是ribbon+r ...
- epoll函数
函数epoll 1. 函数epoll_creat: 该函数生成一个epoll专用的文件描述符 int epoll_creae(int size); 分析: size:epoll上能关注的最大描述符数 ...
- [转] webpack之前端性能优化(史上最全,不断更新中。。。)
最近在用webpack优化首屏加载性能,通过几种插件之后我们上线前后的速度快了一倍,在此就简单的分享下吧,先上个优化前后首屏渲染的对比图. 可以看到总下载时间从3800ms缩短到1600ms. 我们在 ...
- 【SpringBoot】常用注解
@EnableAutoConfiguration 启动自动装载:使用了这个注解之后,所有引入的jar的starters都会被自动注入.这个类的设计就是为starter工作的. @RestControl ...
- 易企秀H5 json配置文件解密分析
最近需要参考下易企秀H5的json配置文件,发现已经做了加密,其实前端的加密分析起来只是麻烦点. 抓包分析 先看一个H5: https://h5.eqxiu.com/s/XvEn30op F12可以看 ...
- C#搭建Oauth2.0认证流程以及代码示例
我认为对于一个普遍问题,必有对应的一个简洁优美的解决方案.当然这也许只是我的一厢情愿,因为根据宇宙法则,所有事物总归趋于混沌,而OAuth协议就是混沌中的产物,不管是1...0a还是2.,单看版本号就 ...
- Codeforces 316G3 Good Substrings 字符串 SAM
原文链接http://www.cnblogs.com/zhouzhendong/p/9010851.html 题目传送门 - Codeforces 316G3 题意 给定一个母串$s$,问母串$s$有 ...
- java生成二维码并融合模板工具类
二维码融合模板 二维码融合图片 import java.awt.AlphaComposite; import java.awt.Graphics2D; import java.awt.Image; i ...
- 040 Http与RPC
一直在说,但是没有系统的文档理解过. 1.本质 目前有很多Java的RPC框架,有基于Json的,有基于XML,也有基于二进制对象的. 他们最本质的区别,就是RPC主要是基于TCP/IP协议的,而HT ...