方法一:導(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)字段,回車隔開(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"]”之類的錯(cuò)誤,這個(gè)代碼是按照我的數(shù)據(jù)結(jié)構(gòu)來(lái)寫的,到時(shí)候相關(guān)的字段要換成你自己的才是。
還有就是如果文件名需要中文的話,這么修改Response.AddHeader("Content-Disposition", "attachment; filename="+System.Web.HttpUtility.UrlEncode("中文",System.Text.Encoding.UTF8)+".xls");
|