Asp. NET, upload and read Excel file data

Source: Internet
Author: User
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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.