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 ...
随机推荐
- Metasploit
1.启动Metasploit 声明:本次渗透测试的主机是我自己在自己的攻击主机上搭建的另一个操作系统,为了真实性设置了常见的IP地址,如有重合但绝对不是任何实体公司或者单位的IP地址. 所以不承担任何 ...
- 第七周学习总结-C#
2018年8月26日 这个周二突然得知另一位老师留的暑假作业,群文件里早就上传了,我居然一直没翻到那里,要不是同学问作业做完没,我可能开学就要“真●裸考”了
- 使用CSS选择器定位页面元素
摘录:http://blog.csdn.net/defectfinder/article/details/51734690 CSS选择器也是一个非常好用的定位元素的方法,甚至比Xpath强大.在自动化 ...
- python之字符编码
1.以什么编码存的就以什么编码取出; 内存固定使用unicode编码; 我们可以控制的编码是往硬盘存放或者基于网络传输选择编码. 2.数据是最先产生于内存中,是unicode格式,要想传输需要转成by ...
- tempalte模板
tempalte模板层: 功能:为了更有逻辑的将数据库中的数据渲染到模板中: 模拟数据源: DB = [ {"hostname":"c1.com"," ...
- 小程序报错:request:fail错误(含https解决方案)(真机预览问题)
问题描述:域名已经备案,我全部都有,也在后台配置了,但是手机预览,还是请求失败, PC端是可以请求数据出来的 新版开发者工具增加了https检查功能:可使用此功能直接本地避开ssl协议版本检查,但是此 ...
- Kubeadm安装的K8S集群1年证书过期问题的解决思路
这个问题,很多使用使用kubeadm的用户都会遇到. 网上也有类似的帖子,从源代码编译这种思路, 在生产环境,有些不现实. 还是使用kubeadm的命令操作,比较自然一点. 当然,自行生成一套证书,也 ...
- WebClient 支持 gzip, deflate
低调偷偷的下别人数据 发现下出来乱码- 用F12看看请求,原来人家是用了gzip压缩的- 试着自己加个Heading wc.Headers.Add("Accept-Encoding" ...
- P3403 跳楼机
题解: 据说是最短路经典题 考虑mod c一意义下 我们会发现mod c相同的话我们一定会用最少步数到达,剩余的都用c转移 由于转移图有环所以我们用spfa来dp(其实也可以理解成最短路) wa了好多 ...
- Flink的广播变量
Flink支持广播变量,就是将数据广播到具体的taskmanager上,数据存储在内存中,这样可以减缓大量的shuffle操作: 比如在数据join阶段,不可避免的就是大量的shuffle操作,我们可 ...