欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ASP.NET中上傳并讀取Excel文件數(shù)據(jù)示例

 更新時間:2014年05月07日 10:46:07   作者:  
如何打開Excel數(shù)據(jù)庫文件,想必有很多朋友都不清楚吧,下面通過一個簡單的例子,實現(xiàn)讀取Excel數(shù)據(jù)文件
在CSDN中,經(jīng)常有人問如何打開Excel數(shù)據(jù)庫文件。本文通過一個簡單的例子,實現(xiàn)讀取Excel數(shù)據(jù)文件。

首先,創(chuàng)建一個Web應(yīng)用程序項目,在Web頁中添加一個DataGrid控件、一個文件控件和一個按鈕控件。
復(fù)制代碼 代碼如下:

<INPUT id="File1" type="file" name="File1" runat="server">
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>

在代碼視圖中首先導(dǎo)入OleDb命名空間:
using System.Data.OleDb;

在按鈕的單擊事件中輸入如下代碼:
復(fù)制代碼 代碼如下:

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)限.

相關(guān)文章

最新評論