GridView导出到EXCEL
来源:互联网 发布:淘宝汽车坐垫四季通用 编辑:程序博客网 时间:2024/06/05 07:38
这个是老生常谈了,网上有很多博文介绍。这里自己也写下来方便以后查看。
先贴代码
public static void ExportGridView(GridView gv, string fileName) { string attachment = "attachment; filename=" + fileName + ".xls"; HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.AddHeader("content-disposition", attachment); HttpContext.Current.Response.ContentType = "application/ms-excel"; for (int i = 0; i < gv.Rows.Count; i++) { GridViewRow row = gv.Rows[i]; row.Cells[0].Attributes.Add("class", "textmode"); } StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); gv.RenderControl(htw); string style = @"<style> .textmode { mso-number-format:\#; } </style>"; HttpContext.Current.Response.Write(style); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); }
说明:
1,导出的是.xls的,暂时没找到导出.xlsx的。以后找到再记。
string attachment = "attachment; filename=" + fileName + ".xlsx";
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
没有解决问题,导出的文件打不开。
2,fileName如果包含中文,在IE下导出后会是乱码。需要编码。
3,如果导出的某一行的数字显示成“1.222E+11”的形式,可以添加样式{ mso-number-format:\#; }。
遍历GridView的行
GridViewRow row = gv.Rows[i];
给每一行的单元格添加样式。
row.Cells[0].Attributes.Add("class", "textmode");
4,需要重写事件,否则会报错,说“GridView 需要放在runat=server的容器里”。
public override void VerifyRenderingInServerForm(System.Web.UI.Control control) { }
- GridView导出到Excel
- GridView导出到Excel
- GridView导出到Excel
- GridView导出到Excel
- gridview 导出到excel
- 导出GridView到Excel
- 导出GridView到Excel
- GridView导出到Excel
- GridView导出到Excel
- GridView 导出到EXCEL
- GridView导出到Excel
- GridView 导出到 Excel
- Gridview导出到excel
- GridView 导出到excel
- 导出Gridview到excel
- GridView导出到Excel
- GridView导出到Excel
- GridView导出到excel
- 红黑树
- 如何查看表空间使用情况
- 写文件
- Android MediaProvider数据库模式
- ASP.NET用户登录后跳转到 登录前 的页面 url 跳转
- GridView导出到EXCEL
- c语言文件读写方式
- jsp指令
- CIO们需要从战略层面出发考虑云计算
- oracle 时间函数(sysdate)
- RPC-RMI
- poj 2761 treap
- Linux下使用system()函数一定要谨慎
- Android设备与VMware Ubuntu系统利用无线实现Socket通信