Sample Excel file data uploaded and read in ES0en. NET

  • 2020-12-21 18:01:06
  • OfStack

In CSDN, people often ask how to open the Excel database file. This article uses a simple example to read the Excel data file.

First, create an Web application project and add an 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 click 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 has access to ASPNET users.

Related articles: