-
C#教程之C#使用RenderControl将GridView控件导出到EXC
本文实例展示了C#使用RenderControl将GridView控件导出到EXCEL的方法,是非常实用的一个功能,分享给大家供大家参考。具体如下:
主要功能代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
// 把GridView输出到Excel文件 private void ExportExcel(GridView gridView, string title, string title2, string fileName) { int nHideCols = 0; //如果不想输出出某列,将Visible设为false即可 for ( int i = 0; i < gridView.Columns.Count; i++) { if (gridView.Columns[i].HeaderText == "设备状态" ) { gridView.Columns[i].Visible = false ; gridView.Columns[i].ControlStyle.Width = 0; nHideCols = 1; break ; } } //设定显示字符集 Response.Charset = "utf-8" ; //设定内容字符集 Response.ContentEncoding = Encoding.GetEncoding( "utf-8" ); //设定文件名 Response.AppendHeader( "Content-Disposition" , "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).Replace( '+' , '_' ).Replace( '-' , '_' )); //设定文件类型 也可以是application/ms-word,也可以是text/html(字符集设为gb2312) Response.ContentType = "application/ms-excel" ; this .EnableViewState = false ; using (StringWriter tw = new StringWriter()) { using (HtmlTextWriter hell = new HtmlTextWriter(tw)) { gridView.AllowPaging = false ; gridView.RenderControl(hell); string s = tw.ToString(); s = s.Replace( "\r\n" , "" ); int index = s.IndexOf( "<tr" ); //可以自定义Excel文件的标题 string head = "<tr><td colspan=\"" + (gridView.Columns.Count - nHideCols).ToString() + "\" style=\"text-align: center; height: 42px; font-size: 24px; font-weight: bolder; color: #000000;\">" + title + "</td></tr>" + "<tr><td colspan=\"" + (gridView.Columns.Count - nHideCols).ToString() + "\" style=\"text-align: center; height: 24px; font-size: 12px; color: #000000;\">" + title2 + "</td></tr>" ; //使用Index来判断是否存在数据,当然也可以用gridView.Rows.Count来判断 if (index != -1) { //有数据的 s = s.Insert(index, head); } else { //没有数据的时候 s = "<table cellspacing=\"0\" cellpadding=\"3\" rules=\"rows\" border=\"1\" id=\"" + gridView.ID + "\" style=\"background-color:White;border-color:#E7E7FF;border-width:1px;border-style:None;border-collapse:collapse;\">" + head + "</table>" ; } Response.Write(s); Response.End(); } } } //同时vs2005,vs2003会报错“类型“ExGridView”的控件“GridViewMaster”必须放在具有 runat=server 的窗体标记内 //需要添加下面取消对GridViewMaster 控件验证的方法 public override void VerifyRenderingInServerForm(Control control) { if (!control.GetType().Equals(gridView.GetType())) { base .VerifyRenderingInServerForm(control); } } |
本文实例代码备有较为详尽的注释,应该不难理解。希望本文实例对大家C#程序设计有所帮助。
栏目列表
最新更新
求1000阶乘的结果末尾有多少个0
详解MyBatis延迟加载是如何实现的
IDEA 控制台中文乱码4种解决方案
SpringBoot中版本兼容性处理的实现示例
Spring的IOC解决程序耦合的实现
详解Spring多数据源如何切换
Java报错:UnsupportedOperationException in Col
使用Spring Batch实现批处理任务的详细教程
java中怎么将多个音频文件拼接合成一个
SpringBoot整合ES多个精确值查询 terms功能实
数据库审计与智能监控:从日志分析到异
SQL Server 中的数据类型隐式转换问题
SQL Server中T-SQL 数据类型转换详解
sqlserver 数据类型转换小实验
SQL Server数据类型转换方法
SQL Server 2017无法连接到服务器的问题解决
SQLServer地址搜索性能优化
Sql Server查询性能优化之不可小觑的书签查
SQL Server数据库的高性能优化经验总结
SQL SERVER性能优化综述(很好的总结,不要错
uniapp/H5 获取手机桌面壁纸 (静态壁纸)
[前端] DNS解析与优化
为什么在js中需要添加addEventListener()?
JS模块化系统
js通过Object.defineProperty() 定义和控制对象
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比