All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
在PHP中全面阻止SQL注入式攻击之二_php资料_编程技术
ASP.NET,2.0发送电子邮件中存在的问题_.net资料_编程技术
使用.NET自带的功能制作简单的注册码_.net资料_编程技术
.NET3.5和VS2008中的ASP.NET,AJAX_.net资料_编程技术
SQL循序渐进(17)JOIN子句_[SQL,Server教程]
SQL Server2000连接中三个最常见错误分析_[SQL Server教程]
ASP.NET中Cookie的使用介绍_[Asp.Net教程]
如何实现Asp与Asp.Net共享Session_[Asp.Net教程]
FCKeditor(asp.net)使用方法_[Asp.Net教程]
一个典型的PHP分页实例代码_[PHP教程]
Reading number is top 10 pictures
Summer is most suitable for young people to travel in China4
The Soviet union swimsuit exposure in the 70 year1
yy365网站上的美女3
生活中总有些低调的人,不经意间散发出土豪的气质
这才是真正的人体艺术4
BingBingFan apple dew point photo gallery3
日本小萝莉2
俄罗斯台球天后惊艳魅惑1
Get girl by your hand
Summer is most suitable for young people to travel in China5
Download software ranking
C++编程教程第三版
ASP.NET.2.0.XML.高级编程(第3版)
Ashlynn Video5
Boxer Classic video2
Sora aoi‘s film--Lust fan wall
C#程序员参考手册
1400篇各类破解文章
jdk1.6 for windows
软件工程思想
徐若瑄成名作“魔鬼天使”
归海一刀 published in(发表于) 2014/1/30 1:26:47 Edit(编辑)
datagrid导出excel文件给客户端下载的3种方法_[Asp.Net教程]

datagrid导出excel文件给客户端下载的3种方法_[Asp.Net教程]

datagrid导出excel文件给客户端下载的3种方法_[Asp.Net教程]

方法一:导出到csv文件,存放在服务器端任一路径,然后给客户下载

优点:
1、可以进行身份认证后给客户下载,如果放到非web目录就没有对应的url,客户无法随时下载。
2、也是因为生成了文件,所以占用了服务器的空间,但是可以把文件名存放到数据库,再次给客户下载的时候不需要重复生成文件。
3、csv文件是文本文件,逗号隔开字段,回车隔开行,易于数据导入导出。

实现方法:
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
SqlDataAdapter da=new SqlDataAdapter("select * from tb1",conn);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
DataTable dt=ds.Tables["table1"];
string name=System.Configuration.ConfigurationSettings.AppSettings["downloadurl"].ToString()+DateTime.Today.ToString("yyyyMMdd")+new Random(DateTime.Now.Millisecond).Next(10000).ToString()+".csv";//存放到web.config中downloadurl指定的路径,文件格式为当前日期+4位随机数
FileStream fs=new FileStream(name,FileMode.Create,FileAccess.Write);
StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.GetEncoding("gb2312"));
sw.WriteLine("自动编号,姓名,年龄");
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine(dr["ID"]+","+dr["vName"]+","+dr["iAge"]);
}
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(name));
Response.ContentType = "application/ms-excel";// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.WriteFile(name); // 把文件流发送到客户端
Response.End();


方法二:导出到csv文件,不存放到服务器,直接给浏览器输出文件流


优点:
1、随时生成,不需要占用资源
2、可以结合身份认证
3、同样利于数据交换

实现方法:
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
SqlDataAdapter da=new SqlDataAdapter("select * from tb1",conn);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
DataTable dt=ds.Tables["table1"];
StringWriter sw=new StringWriter();
sw.WriteLine("自动编号,姓名,年龄");
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine(dr["ID"]+","+dr["vName"]+","+dr["iAge"]);
}
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=test.csv");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();


对方法一,二补充一点,如果你希望导出的是xls文件分隔符用\t就可以了,不要用逗号


代码修改如下:
sw.WriteLine("自动编号\t姓名\t年龄");
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine(dr["ID"]+"\t"+dr["vName"]+"\t"+dr["iAge"]);
}
另外,修改输出的文件扩展名为xls即可。


方法三:从datagrid导出html代码,生成excel文件,给客户端下载

优点:
1、有固定的格式,样子好看(datagrid的样子)

局限性:
1、不适合数据交换,里面有html代码,比较乱,没有固定格式
2、datagrid不能有分页、排序等,否则出错

实现方法:
Response.Clear();
Response.Buffer= false;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=test.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"); Response.ContentType = "application/ms-excel"; this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();


在这里说明一点:有的网友反映代码出现“没有dr["id"]”之类的错误,这个代码是按照我的数据结构来写的,到时候相关的字段要换成你自己的才是。

还有就是如果文件名需要中文的话,这么修改Response.AddHeader("Content-Disposition", "attachment; filename="+System.Web.HttpUtility.UrlEncode("中文",System.Text.Encoding.UTF8)+".xls");


原文地址 http://chinaitbbs.com.cn/main/teach/TeachContent.aspx?id=4063


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.