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控件、一個文件控件和一個按鈕控件。
<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;
在按鈕的單擊事件中輸入如下代碼:
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)限.
首先,創(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)文章
.NET Core應(yīng)用類型(Portable apps & Self-contained apps)
這篇文章主要介紹了.NET Core應(yīng)用類型,通常類型用來描述一個特定的執(zhí)行模型或者基于此的應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04.NET中利用js讓子窗體向父頁面?zhèn)髦档膶崿F(xiàn)方法
.NET中利用js讓子窗體向父頁面?zhèn)髦档膶崿F(xiàn)方法,需要的朋友可以參考一下2013-02-02詳解.Net Core 權(quán)限驗證與授權(quán)(AuthorizeFilter、ActionFilterAttribute)
這篇文章主要介紹了.Net Core 權(quán)限驗證與授權(quán)(AuthorizeFilter、ActionFilterAttribute),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04使用Visual Studio創(chuàng)建ASP.NET Web API項目
這篇文章介紹了使用Visual Studio創(chuàng)建ASP.NET Web API項目的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03