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

Reading number is top 10 articles
PHP将MYSQL内容读到二维数组并按指定列输出_[PHP教程]
ASP.NET性能最佳实践_[Asp.Net教程]
C#程序的50种方法
关于.NET脏字过滤的算法代码_.net资料_编程技术
.net控件开发(六)之深究控件样式_[Asp.Net教程]
ASP.NET实例:在GridView使用HyperLinkField,属性的链接_[Asp.Net教程]
Ajax基础必读-AJAX中的一些关键技术_[AJAX教程]
PHP对GB编码动态转UTF-8编码的几种方法评测_[PHP教程]
SQL,Server,2008在商务智能上的三个优化_mssql学习_编程技术
PHP中使用crypt()实现用户身份验证_php资料_编程技术
Reading number is top 10 pictures
Discharge accidentally Actresses by the breast3
赵惟依写真3
全球十大灵异酒店
9.3阅兵全景图4-陸海空现代化兵种方阵梯队
Send some Valentine's day cartoon
A man's favorite things2
男人帮杂志里的惹火性感美女2
中国文革时期的色情图片1
这年头,找个靠谱的妹子太难了
Ashlynn Brooke photograph of a group1
Download software ranking
网页特效实例大全
C#与.NET技术平台实战演练
传奇私服架设教程-chm
Photoshop 8.0图象编辑软件
Tram sex maniac 2 (H) rar bag8
Boxer's Top ten classic battle6
Sora aoi - one of more PK
C#程序员参考手册
Unix video tutorial6
jdk1.6 for windows
delv published in(发表于) 2014/1/6 9:13:28 Edit(编辑)
把WebForm数据导出到Excel中_[Asp.Net教程]

把WebForm数据导出到Excel中_[Asp.Net教程]

把WebForm数据导出到Excel中_[Asp.Net教程]

上午给系统的订单管理部分添加了一个功能,把查询到的订单信息导出到Excel,供管理员分析用。以前写的代码如下:


Response.Clear();
Response.BufferOutput = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename = FileName.xls");
Response.ContentEncoding = Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
System.Globalization.CultureInfo myinfo = new System.Globalization.CultureInfo("ZH-CN", true);
StringWriter osw = new StringWriter(myinfo);
HtmlTextWriter ohtw = new HtmlTextWriter(osw);
dgQueryResult.RenderControl(ohtw);
Response.Write(osw);
Response.End();
我的查询用了分页,客户要求把所有查询到的数据都导出到Excel,所以不能象以前那样通过把控件的内容导出到Excel来实现此功能,于是把代码改成这样:
Response.Clear();
Response.BufferOutput = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename = 订单.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
string strExcelHeader = string.Empty;
string strExcelItems;
if(ViewState["SQL"] != null) // 取前面查询用的SQL语句
{
// 取得各列标题,各标题之间以\t分割,最后一个列标题后加回车符
strExcelHeader = "订单号\t经销商\t地区\t小类\t商品\t实付金额\t下单时间\t有效状态\t处理状态\t\n";
// 向HTTP输出流中写入取得的数据信息
Response.Write(strExcelHeader);

// 逐行处理查询结果数据
ITDBHandle itDbHandle = new ITDBHandle();
itDbHandle.QueryString = ViewState["SQL"].ToString();
SqlDataReader reader = itDbHandle.ExecuteDataReader();
while(reader.Read())
{
strExcelItems = string.Empty;
strExcelItems += reader["OrderID"].ToString() + "\t";
strExcelItems += reader["DealerName"].ToString() + "\t";
strExcelItems += reader["City"].ToString() + "\t";
strExcelItems += reader["SmallClassName"].ToString() + "\t";
strExcelItems += reader["BrandName"].ToString() + reader["Model"].ToString() + "\t";
strExcelItems += reader["TotalPrice"].ToString() + "\t";
strExcelItems += reader["OrderDate"].ToString() + "\t";
strExcelItems += reader["IsValid"].ToString() + "\t";
strExcelItems += reader["DealState"].ToString() +"\n";
Response.Write(strExcelItems);
}
reader.Close();
Response.End();
}
我这里只是简单的把查询到的数据以Excel的形式Write出来(当然也可以是其他格式,比如XML),对简单的需求足矣了,当然,用.NET直接去操作Excel文件也是很简单的,有很多这样的例子可以参考。







添加到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.