Upload and read Excel file data in ASP. NET

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

InCodeFirst, import the oledb namespace in the View:
Using system. Data. oledb;

Enter the following code in the button clicking event:

StringStrpath = "C: \ test \" + datetime. Now. tostring ("yyyymmddhhmmss") + ". xls ";

File1.postedfile. saveas (strpath );

StringMystring = "provider = Microsoft. Jet. oledb.4.0; Data Source = '" + strpath + "'; extended properties = Excel 8.0 ";

Oledbconnection cnnxls =NewOledbconnection (mystring );

Oledbdataadapter myda =NewOledbdataadapter ("select * from [sheet1 $]", cnnxls );

Dataset myds =NewDataset ();

Myda. Fill (myds );

Datagrid1.datasource = myds. Tables [0];

Datagrid1.databind ();

C: \ test must have the read and write permissions on 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.