當前位置:首頁 » 文件管理 » aspnet上傳excel

aspnet上傳excel

發布時間: 2025-06-27 15:43:10

『壹』 ASPNET數據導出到excel文件給客戶端下載的幾種方法麻煩告訴我

ASP.NET數據導出到excel文件給客戶端下載的幾種方法麻煩告訴我?

Response.WriteFile(ASP.NET 數據導出到excel文件給客戶端下載的幾種方法

數據導出到excel文件給客戶端下載的幾種方法:

方法一:導出到csv文件,存放在伺服器端任一路徑,然後給客戶下載

優點:

1、可以進行身份認證後給客戶下載,如果放到非web目錄就沒有對應的url,客戶無法隨時下載。

2、也是因為生成了文件,所以佔用了伺服器的空間,但是可以把文件名存放到資料庫,再次給客戶下載的時候不需要重復生成文件。

3、csv文件是文本文件,逗號隔開欄位,回車隔開行,易於數據導入導出。

實現方法:

SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings);

SqlDataAdapter da=new SqlDataAdapter("select * from tb1",conn);

DataSet ds=new DataSet();

da.Fill(ds,"table1");

DataTable dt=ds.Tables;

string name=System.Configuration.ConfigurationSettings.AppSettings.ToString() DateTime.Today.ToString("yyyyMMdd") new Random(DateTime.Now.Millisecond).Next(10000).ToString() ".csv";//存放到web.config中downloarl指定的路徑,文件格式為當前日期 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 "," dr "," dr);}

sw.Close();

Response.AddHeader("Content-Disposition", "attachment; filename=" Server.UrlEncode(name));

Response.ContentType = "application/ms-excel";// 指定返回的是一個不能被客戶端讀取的流,必須被下載

Response.WriteFile(name); // 把文件流發送到客戶端

Response.End();

方法二:導出到csv文件,不存放到伺服器,直接給瀏覽器輸出文件流

優點:

1、隨時生成,不需要佔用資源

2、可以結合身份認證

3、同樣利於數據交換

實現方法:

SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings);

SqlDataAdapter da=new SqlDataAdapter("select * from tb1",conn);

DataSet ds=new DataSet();

da.Fill(ds,"table1");

DataTable dt=ds.Tables;

StringWriter sw=new StringWriter();

sw.WriteLine("自動編號,姓名,年齡");

foreach(DataRow dr in dt.Rows)

{sw.WriteLine(dr "," dr "," dr);}

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就可以了,不要用逗號

代碼修改如下:

sw.WriteLine("自動編號t姓名t年齡");

foreach(DataRow dr in dt.Rows)

{sw.WriteLine(dr "t" dr "t" dr);}

另外,修改輸出的文件擴展名為xls即可。

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

實現方法:

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

在這里說明一點:有的網友反映代碼出現「沒有dr」之類的錯誤,這個代碼是按照我的數據結構來寫的,到時候相關的欄位要換成你自己的才是。

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


『貳』 伺服器未安裝Excel.Application控制項,怎麼處理這個問題

首先,你的 excel 必須安裝完整版。
其次,你需要允許網頁程序創建 excel 組件。這一步,也就是說的 DCOMCNFG。你的伺服器端腳本執行時,會有一個用戶,所有網頁許可權都會限制在這個用戶下。但是很可惜,這個用戶不是 Everyone。
asp的用戶,我記得是 IUSR_xxx,IWAM_xxx。asp.net 的用戶,在 xp 下是 ASPNET,在 server 下士 Network Service。

熱點內容
奧維地圖伺服器地址用戶名密碼 發布:2025-06-27 19:35:07 瀏覽:18
263郵箱的pop伺服器地址 發布:2025-06-27 19:28:32 瀏覽:812
資料庫參數化 發布:2025-06-27 19:22:20 瀏覽:732
便攜電腦做列印機伺服器 發布:2025-06-27 19:07:28 瀏覽:545
科學計演算法的 發布:2025-06-27 19:05:20 瀏覽:577
php解析json字元串 發布:2025-06-27 18:58:49 瀏覽:676
net導入excel到資料庫 發布:2025-06-27 18:55:37 瀏覽:34
怎樣在電腦上開通ftp 發布:2025-06-27 18:48:22 瀏覽:884
盤古編程 發布:2025-06-27 18:45:03 瀏覽:372
青黴素注射液有什麼液體配置好 發布:2025-06-27 18:34:28 瀏覽:74