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 ...
随机推荐
- nxlog以syslog方式发送日志
1.nxlog简介 nxlog是个跨平台日志传输插件,支持linux.windows平台,支持window及linux内置的大部分系统日志及常见的web日志,支持tcp.udp.http(s)等协议传 ...
- 那些常用的JS命令
window.location.reload()刷新当前页面. parent.location.reload()刷新父亲对象(用于框架) opener.location.reload()刷新父窗口对象 ...
- 【树形DP】【UVA10859】 Placing Lampposts
传送门 Description 给定一个\(n\)个点\(m\)条边的无向无环图,选择尽量少的节点,使得所有边都至少有一个顶点被选择.在这个基础上,要求有两个顶点被选择的边数尽可能大 Input 多组 ...
- C/C++中二维数组和指针关系分析
在C/c++中,数组和指针有着密切的关系,有很多地方说数组就是指针式错误的一种说法.这两者是不同的数据结构.其实,在C/c++中没有所谓的二维数组,书面表达就是数组的数组.我猜想是为了表述方便才叫它二 ...
- Linux IO Scheduler
一直都对linux的io调度算法不理解,这段时间一直都在看这方面的内容,下面是总结和整理的网络上面的内容.生产上如何建议自己压一下.以实际为准. 每个块设备或者块设备的分区,都对应有自身的请求队列(r ...
- HDU 2094 拓扑排序
产生冠军 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- kali wireless driver install
acer-gateway nv47h94c 1.lspci -nn |grep 0280 get pci-id root@silee:/# lspci -nn |grep 028003:00.0 Ne ...
- HDU4027 线段树
Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K ...
- linux查看内存cpu占用
linux查看内存cpu占用top 命令 按q退出 可以添加额外选项选择按进程或按用户查看如: top -u gitu PID:进程idPR:进程的优先级别,越小越优先被执行NInice:值VIRT ...
- You can't specify target table 'table' for update in FROM clause
delete from table1 where ID not in(select max(ID) ID from table1 group by row1) and row1 ) # 出现错误 # ...