Asp.net网页中DataGridView数据导出到Excel
经过上网找资料,终于找到一种可以直接将GridView中数据导出到Excel文件的方法,归纳方法如下:
1、 注:其中的字符集格式若改为“GB2312”,导出的部分数据可能为乱码;
导出之前需要关闭分页部分,若分页只导出首页的数据;
/// <summary>
/// 下载数据
/// </summary>
/// <param name="FileType">文件类型</param>
/// <param name="FileName">Excel表名</param>
private void Excel(string FileType, string FileName)
{
try
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
//返回与指定代码页关联的数据
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
//attachment表示作为附件下载,filename指定输出文件名称
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
//指定文件类型
Response.ContentType = FileType;
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
//定义一输入流
StringWriter tw = new StringWriter(myCItrad);
HtmlTextWriter hw = new HtmlTextWriter(tw);
this.gvJstList.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
catch (Exception err)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "js", "showInfo('ctl00_Contentplaceholder2_ValidationSummary1',1,'发生错误:" + err.Message.Replace("\r\n", "\\r\\n").Replace("'", "‘") + "')", true);
return;
}
}
如上代码如果处理一般的GridView导出应该是没有问题的,但是如果GridView的AutoGenerateDeleteButton,AutoGenerateEditButton,AutoGenerateSelectButton有的设置为True了,或者GridView中有HyperLinkField类型的字段,那么就会抛出形如“类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。”的异常!
那么他的解决方案是:对WebForm窗体的VerifyRenderingInServerForm方法进行Override! 代码如下:
public override void VerifyRenderingInServerForm(Control control)
{
//OverRide 为了使导出成Excel可行!
}
2、/*如导出的表中有某些列为编号、身份证号之类的纯数字字符串,如不进行处理,则导出的数据会默认为数字,例如原字符串"0010"则会变为数字10,字符串"1245787888"则会变为科学计数法1.236+E9,这样便达不到我们想要的结果,所以需要在导出前对相应列添加格式化的数据类型,以下为格式化为字符串型*/ 在上面的代码中添加以下代码:
foreach (GridViewRow dg in this.gridview1.Rows)
{
dg.Cells[].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
dg.Cells[].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
dg.Cells[].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
dg.Cells[].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
}
或者是在DataGridView事件RowDataBound中添加以下代码: if(e.Row.RowType == DataControlRowType.DataRow )
{
e.Row.Cells[].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
e.Row.Cells[].Attributes.Add("style", "vnd.ms-excel.numberformat:@"); }
参考资料如下:
http://www.cnblogs.com/stewen/archive/2008/03/26/1122778.html
http://www.soaspx.com/dotnet/asp.net/Control/control_20100322_3371.html
Asp.net网页中DataGridView数据导出到Excel的更多相关文章
- 将Datagridview中的数据导出至Excel中
首先添加一个模块ImportToExcel,并添加引用 然后导入命名空间: Imports Microsoft.Office.Interop Imports System.Da ...
- 学习笔记 DataGridView数据导出为Excel
DataGridView数据导出为Excel 怎样把WinForm下的“DGV”里的绑定数据库后的数据导出到Excel中. 比如:在窗体里有个一“DGV”,DataGridView1,绑定了数据源 ...
- vb.net-三种将datagridview数据导出为excel文件的函数
第一种方法较慢,但是数据格式都比较好,需要引用excel的 Microsoft.Office.Interop.Excel.dll office.dll #Region "导出excel函数 ...
- 机房收费系统——在VB中将MSHFlexGrid控件中的数据导出到Excel
机房收费系统中,好多查询的窗体都包含同一个功能:将数据库中查询到的数据显示在MSHFlexGrid控件中,然后再把MSHFlexGrid控件中的数据导出到Excel表格中. 虽然之前做过学生信息管理系 ...
- Qt中将QTableView中的数据导出为Excel文件
如果你在做一个报表类的程序,可能将内容导出为Excel文件是一项必须的功能.之前使用MFC的时候我就写过一个类,用于将grid中的数据导出为Excel文件.在使用了QtSql模块后,我很容易的将这个类 ...
- WPF-将DataGrid控件中的数据导出到Excel
原文:WPF-将DataGrid控件中的数据导出到Excel 导出至Excel是非常常见,我们可以用很多类库,例如Aspose.NOPI.Interop,在这里我们使用微软自家的工具.我的WPF绑定的 ...
- 如何将存储在MongoDB数据库中的数据导出到Excel中?
将MongoDB数据库中的数据导出到Excel中,只需以下几个步骤: (1)首先,打开MongoDB安装目录下的bin文件夹,(C:\Program Files (x86)\MongoDB\Serve ...
- asp.net 将repeater上数据导出到excel
1,首先得到一个DataTable public DataTable GetTable(string sql) { SqlConnnection con=new SqlConnection(Confi ...
- C#将数据集DataSet中的数据导出到EXCEL文件的几种方法
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.W ...
随机推荐
- MySQL基础原创笔记(一)
对表的增删改操作: 创建表: create table student ( id int primary key auto_increment, name ...
- sourcemap总结
sourcemap在线上压缩文件调试中很重要,在此总结如下: 1. 开启sourcemap (1). 浏览器要开启source-map支持(2). 压缩文件底部要有source-map的URL,压缩要 ...
- bzoj1690:[Usaco2007 Dec]奶牛的旅行(分数规划+spfa判负环)
PS:此题数组名皆引用:戳我 题目大意:有n个点m条有向边的图,边上有花费,点上有收益,点可以多次经过,但是收益不叠加,边也可以多次经过,但是费用叠加.求一个环使得收益和/花费和最大,输出这个比值. ...
- js正则:两边字符固定,中间任意字符
求些一个js正则!两边字符固定,中间任意字符.在一个长字符串里面匹配一小段,这一小段字符串开头和结尾都是固定的字符,就是中间是任意长度的字符.怎么写? /aa.+aa/ aa是你的固定字符,如果是反斜 ...
- Codeforces Round #514 (Div. 2) C. Sequence Transformation(递归)
C. Sequence Transformation 题目链接:https://codeforces.com/contest/1059/problem/C 题意: 现在有1~n共n个数,然后执行下面操 ...
- mapper的前后缀
1.<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=&quo ...
- POJ 2391 二分+最大流
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19066 Accepted: 4 ...
- oracle 国外网站【转载】
[转自]:http://www.2cto.com/database/201406/306615.html 1. http://www.oratechinfo.co.uk/ http://www.ora ...
- ssm项目,web容器无法初始化项目
在web.xml中配置加载spring时,发现项目无法运行:而去掉spring的配置时,项目可以被初始化. 此时应考虑到spring的配置文件中存在错误,以至于web容器无法对项目成功初始化,在web ...
- 通过psexec实现远程安装软件包
1.在管理机上下载和安装psexec https://docs.microsoft.com/en-us/sysinternals/downloads/psexec 2.在管理机上编写bat脚本,存放在 ...