天天看點

datagrid資料導出到excel檔案給用戶端下載下傳的幾種方法 (轉)

?

?

?規?涓?锛?瀵煎?哄??sv??浠訛?瀛??懼?ㄦ???″?ㄧ??浠諱?璺?寰?锛??跺??缁?瀹㈡?蜂?杞?

浼??癸?

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";//瀛??懼??eb.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();

?規?浜?锛?瀵煎?哄??sv??浠訛?涓?瀛??懼?版???″????存?ョ?娴?瑙??ㄨ??烘??浠舵?

浼??癸?

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灏卞??浠ヤ?锛?涓?瑕??ㄩ????

浠g??淇??瑰?涓?锛?

sw.WriteLine("???ㄧ???t濮???\t骞撮?");

???foreach(DataRow dr in dt.Rows)

???{

????sw.WriteLine(dr["ID"]+"\t"+dr["vName"]+"\t"+dr["iAge"]);

???}

???锛?淇??矽??虹????浠舵?╁???涓?ls?沖????

?規?涓?锛?浠?datagrid瀵煎??tml浠g??锛?????excel??浠訛?缁?瀹㈡?風??涓?杞?

浼??癸?

1?????哄?????煎?锛??峰??濂界??锛?datagrid???峰??锛?

灞????э?

1??涓??????版??浜ゆ??????㈡??html浠g??锛?姣?杈?涔憋?娌℃???哄???煎?

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();

?ㄨ???璇存??涓??癸?????缃???????浠g???虹?扳??娌℃??dr["id"]??涔?绫葷????璇?锛?杩?涓?浠g???????ф?????版??缁????ュ????锛??版?跺???稿?崇??瀛?娈佃??㈡??浣???宸辯????????

杩???灏辨??濡?????浠跺????瑕?涓?????璇?锛?杩?涔?淇???esponse.AddHeader("Content-Disposition", "attachment; filename="+System.Web.HttpUtility.UrlEncode("涓???",System.Text.Encoding.UTF8)+".xls");

????锛?http://blog.csdn.net/lovecherry/archive/2005/03/12/317790.aspx