In csdn, someone often asks how to open an Excel database file. This article uses a simple example to read Excel data files.
First, create a web applicationProgramProject: Add a DataGrid Control, a file control, and a button control to the web page.
<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>
InCodeFirst, import the oledb namespace in the View:
Using system. Data. oledb;
Enter the following code in the button clicking event:
StringStrpath = "C: \ test \" + datetime. Now. tostring ("yyyymmddhhmmss") + ". xls ";
File1.postedfile. saveas (strpath );
StringMystring = "provider = Microsoft. Jet. oledb.4.0; Data Source = '" + strpath + "'; extended properties = Excel 8.0 ";
Oledbconnection cnnxls =NewOledbconnection (mystring );
Oledbdataadapter myda =NewOledbdataadapter ("select * from [sheet1 $]", cnnxls );
Dataset myds =NewDataset ();
Myda. Fill (myds );
Datagrid1.datasource = myds. Tables [0];
Datagrid1.databind ();
C: \ test must have the read and write permissions on the ASPNET user.