把服务器端数据库的DATASET数据写入客户端EXCEL文件

发表时间:2007-04-11 15:47:00 浏览次数:1002
关键字:DATASET EXCEL


最近做出版发行系统用到这块.
所以张罗了一个简单解决方案。
说它简单,是因为未对Excel做什么处理。
public void datasetToExcel1(string fileName)
        
{
            DataSet ds 
= (DataSet)this.Cache["DataSet"];
            DataTable dt
=ds.Tables[0];

            System.IO.StringWriter sw
=new System.IO.StringWriter(); 
            sw.WriteLine(
"姓名\t公司\t投递地址\t邮编\t份数\t手机\t电话"); 
            
foreach(DataRow dr in dt.Rows) 
            

                sw.WriteLine(dr[
"pSubscriberName"]+"\t"+dr["pSubscriberCompany"]+"\t"+dr["pSubscriberAddress"]+"\t"+dr["pSubscriberPost"]+"\t"+dr["pMagazineShareNum"]+"\t"+dr["pSubscriberPhone"]+"\t"+dr["pSubscriberTel"]); 
            }
 
            sw.Close(); 
            Response.AddHeader(
"Content-Disposition""attachment; filename="+System.Web.HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)+".xls"); 
            Response.ContentType 
= "application/ms-excel"
            Response.ContentEncoding
=System.Text.Encoding.GetEncoding("GB2312"); 
            Response.Write(sw); 
            Response.End();
        }
这里要注意的是fileName 必须进行转换,不然下载的文件名是乱码。
来源:http://www.cnblogs.com/ricozhao/archive/2007/04/10/706747.html

FeedBack


还没有任何评论...
Title:
 
Name:
 
URL:

Comments:
 

Because of the cache,you may see your comments several minutes later.


关于我们 关于希丁 广告合作 业务范围 网站地图 工作机会
copyright @ 2003 - 2005 xding.com  蜀icp备05022311号
tel:(028)66380789 email:xdings#gmail.com(请将#修改为@)
my photo v1 v2 v3 Valid XHTML 1.0 Transitional