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 ...
随机推荐
- POJ.1552 Doubles(水)
POJ.1552 Doubles(水) 题意分析 暴力 代码总览 #include <cstdio> #include <stdio.h> #define nmax 100 u ...
- UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用)
UVA.12096 The SetStack Computer ( 好题 栈 STL混合应用) 题意分析 绝对的好题. 先说做完此题的收获: 1.对数据结构又有了宏观的上的认识; 2.熟悉了常用STL ...
- android getpost代码
GetPostUtil public class GetPostUtil { /** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param par ...
- PowerDesigner 技巧【1】
Name与Code同步的问题: PowerDesigner中,修改了某个字段的name,其code也跟着修改,这个问题很讨厌,因为一般来说,name是中文的,code是字段名. 解决方法如下: 1.选 ...
- Java中的String为什么是不可变的? — String源码分析
原文地址:http://www.importnew.com/16817.html 什么是不可变对象? 众所周知, 在Java中, String类是不可变的.那么到底什么是不可变的对象呢? 可以这样认为 ...
- 状压DP的总结
状压dp的标志 ①数据小 ②通过题目所给出的条件以后得到的特征集合小 一:CF259div2 D: 题目大意:保证b[i]中每个数互质,给出a[i],然后求1~n的abs(a[i]-b[i])最小.a ...
- 【Foreign】画方框 [主席树]
画方框 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output 输出一行一个整数,表示 CD 最多可能画了几个方框. Sam ...
- 【游记】NOIP 2017
时间:2017.11.11~2017.11.12 地点:广东省广州市第六中学 Day1 T1:看到题目,心想这种题目也能放在T1? 这个结论我之前遇到过至少3次,自己也简单证明过.初见是NOIP200 ...
- 【LibreOJ】#538. 「LibreOJ NOIP Round #1」数列递推
[题意]LibreOJ [算法]乱搞 [题解]容易发现数列最后一定单调,最后单调递增则最大值赋为最后一个,反之最小值赋为最后一个,然后处理一些细节就可以AC,要注意以下几点: 1.数列连续三项以及数列 ...
- .NET FrameWork 中的 CTS
CTS:Common Type System 通用类型系统. 1.不仅可以把C#编译成.Net IL,还支持Basic.Python.Ruby等语言,甚至还支持Java.不同语言中的数据类型定义是不一 ...