asp.net|excel| Upload | Data in csdn, people often ask how to open an Excel database file. This article through a simple example, the realization reads the Excel data file.
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>
Import the OLE DB namespace first in Code view:
Using System.Data.OleDb;
Enter the following code in the Click event of the button:
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 ();
Where C:\test has read and write permission to the ASPNET user.