Read an execl from ado.net

Source: Internet
Author: User

Public datatable getdatatablefromexcel (string URL ){
// Create connection string variable. Modify the "Data Source"
// Parameter as appropriate for your environment.
String sconnectionstring = "provider = Microsoft. Jet. oledb.4.0;" +
"Data Source =" + URL + ";" + "extended properties = Excel 8.0 ;";

// Create connection object by using the preceding connection string.
Oledbconnection objconn = new oledbconnection (sconnectionstring );

// Open connection with the database.
Objconn. open ();

// The code to follow uses a SQL SELECT command to display the data from the worksheet.

// Create new oledbcommand to return data from worksheet.
Oledbcommand objcmdselect = new oledbcommand ("select * from [sheet1 $]", objconn );

// Create new oledbdataadapter that is used to build a dataset
// Based on the preceding SQL SELECT statement.
Oledbdataadapter objadapter1 = new oledbdataadapter ();

// Pass the SELECT command to the adapter.
Objadapter1.selectcommand = objcmdselect;

// Create new dataset to hold information from the worksheet.
Dataset objdataset1 = new dataset ();

// Fill the dataset with the information from the worksheet.
Objadapter1.fill (objdataset1, "xldata ");

Datatable dt = objdataset1.tables ["xldata"];

// Clean up objects.
Objconn. Close ();
Return DT;

}

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.