Get the App
SLTechnology News&Howtos  ›  Development  › 

A tutorial on how to upload and read Excel File data in ASP.NET

Shulou Source: shulou.com Published: 2022-06-03 08:10:28 10月20日 Update

This article mainly explains "ASP.NET upload and read Excel file data method tutorial," interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's take you to learn "ASP.NET upload and read Excel file data method tutorial" bar!

In CSDN, people often ask how to open Excel database files. This article through a simple example, realize reading 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.

The copy code is as follows:

First import the OleDb namespace in Code View:

using System.Data.OleDb;

Enter the following code in the button click event:

The copy code is as follows:

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 read and write permissions for ASPNET users.

At this point, I believe that everyone on the "ASP.NET upload and read Excel file data method tutorial" has a deeper understanding, may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Tags: Files code data methods tutorials controls content buttons learning practical deeper events examples interests practicality practical applications simple operation database more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS Shulou Technology Huawei Shulou Information