ASP.NET中上傳并讀取Excel文件數(shù)據(jù)
ASP.NET中上傳并讀取Excel文件數(shù)據(jù)ASP.NET中上傳并讀取Excel文件數(shù)據(jù),Excel文件數(shù)據(jù),ASP.NET
在CSDN中,經(jīng)常有人問如何打開Excel數(shù)據(jù)庫文件。本文通過一個簡單的例子,實現(xiàn)讀取Excel數(shù)據(jù)文件。
首先,創(chuàng)建一個Web應(yīng)用程序項目,在Web頁中添加一個DataGrid控件、一個文件控件和一個按鈕控件。
在代碼視圖中首先導(dǎo)入OleDb命名空間:
using System.Data.OleDb;
在按鈕的單擊事件中輸入如下代碼:
string strPath="c:\\test\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
File1.PostedFile.SaveAs(strPath);
string mystring="Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = '"+ strPath +"';Extended Properties=Excel 8.0";
OleDbConnection cnnxls = new OleDbConnection (mystring);
OleDbDataAdapter myDa =new OleDbDataAdapter("select * from [Sheet1$]",cnnxls);
DataSet myDs =new DataSet();
myDa.Fill(myDs);
DataGrid1.DataSource=myDs.Tables[0];
DataGrid1.DataBind();
其中C:\test對ASPNET用戶要有讀寫的權(quán)限.
posted on 2011-01-10 10:53 aiaiwoo 閱讀(313) 評論(0) 編輯 收藏 所屬分類: ASP.NET