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 application project and 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>
In the Code view, first import the OleDb namespace:
Using System. Data. OleDb;
Enter the following code in the button clicking event:
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 must have the read and write permissions on the ASPNET user.