首先,創建一個web應用程序項目,在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>
在代碼視圖中首先導入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用戶要有讀寫的權限.
新聞熱點
疑難解答
圖片精選