亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

首先,創(chuàng)建一個(gè)Web應(yīng)用程序項(xiàng)目,在Web頁(yè)中添加一個(gè)DataGrid控件、一個(gè)文件控件和一個(gè)按鈕控件。
復(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對(duì)ASPNET用戶(hù)要有讀寫(xiě)的權(quán)限.

相關(guān)文章

最新評(píng)論