成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

Datagrid數(shù)據(jù)導(dǎo)出到excel文件的三種方法

2010-08-28 10:50:35來(lái)源:西部e網(wǎng)作者:

方法一:導(dǎo)出到csv文件,存放在服務(wù)器端任一路徑,然后給客戶下載

優(yōu)點(diǎn):
1、可以進(jìn)行身份認(rèn)證后給客戶下載,如果放到非web目錄就沒(méi)有對(duì)應(yīng)的url,客戶無(wú)法隨時(shí)下載。
2、也是因?yàn)樯闪宋募,所以占用了服?wù)器的空間,但是可以把文件名存放到數(shù)據(jù)庫(kù),再次給客戶下載的時(shí)候不需要重復(fù)生成文件。
3、csv文件是文本文件,逗號(hào)隔開(kāi)字段,回車(chē)隔開(kāi)行,易于數(shù)據(jù)導(dǎo)入導(dǎo)出。

實(shí)現(xiàn)方法:
 
  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指定的路徑,文件格式為當(dāng)前日期+4位隨機(jī)數(shù)
   FileStream fs=new FileStream(name,FileMode.Create,FileAccess.Write);
   StreamWriter sw=new StreamWriter(fs,System.Text.Encoding.GetEncoding("gb2312"));
   sw.WriteLine("自動(dòng)編號(hào),姓名,年齡");
   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";// 指定返回的是一個(gè)不能被客戶端讀取的流,必須被下載
   Response.WriteFile(name); // 把文件流發(fā)送到客戶端
   Response.End();

方法二:導(dǎo)出到csv文件,不存放到服務(wù)器,直接給瀏覽器輸出文件流

優(yōu)點(diǎn):
1、隨時(shí)生成,不需要占用資源
2、可以結(jié)合身份認(rèn)證
3、同樣利于數(shù)據(jù)交換

實(shí)現(xiàn)方法:
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("自動(dòng)編號(hào),姓名,年齡");
   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();

對(duì)方法一,二補(bǔ)充一點(diǎn),如果你希望導(dǎo)出的是xls文件分隔符用\t就可以了,不要用逗號(hào)

代碼修改如下:
sw.WriteLine("自動(dòng)編號(hào)\t姓名\t年齡");
   foreach(DataRow dr in dt.Rows)
   {
    sw.WriteLine(dr["ID"]+"\t"+dr["vName"]+"\t"+dr["iAge"]);
   }
另外,修改輸出的文件擴(kuò)展名為xls即可。


方法三:從datagrid導(dǎo)出html代碼,生成excel文件,給客戶端下載

優(yōu)點(diǎn):
1、有固定的格式,樣子好看(datagrid的樣子)

局限性:
1、不適合數(shù)據(jù)交換,里面有html代碼,比較亂,沒(méi)有固定格式
2、datagrid不能有分頁(yè)、排序等,否則出錯(cuò)

實(shí)現(xiàn)方法:
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();

在這里說(shuō)明一點(diǎn):有的網(wǎng)友反映代碼出現(xiàn)“沒(méi)有dr["id"]”之類(lèi)的錯(cuò)誤,這個(gè)代碼是按照我的數(shù)據(jù)結(jié)構(gòu)來(lái)寫(xiě)的,到時(shí)候相關(guān)的字段要換成你自己的才是。

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

原文:http://www.cnblogs.com/lovecherry/archive/2005/03/25/125519.aspx
關(guān)鍵詞:ASP.NET
主站蜘蛛池模板: 昭觉县| 蒙阴县| 怀宁县| 和政县| 资阳市| 高碑店市| 油尖旺区| 顺昌县| 绥江县| 新和县| 南开区| 台湾省| 商南县| 全州县| 娱乐| 赫章县| 和田市| 常熟市| 大港区| 深圳市| 嵊泗县| 若羌县| 夏津县| 昌宁县| 繁峙县| 班戈县| 伊宁县| 威远县| 西乡县| 石台县| 尼木县| 北海市| 秀山| 仁怀市| 宜君县| 贡觉县| 扶沟县| 隆林| 余江县| 沽源县| 邯郸县|