C # reads Excel data into a DataTable

Source: Internet
Author: User

1DataTable dtgbpatient =NewDataTable ();2 3             stringstrconn;stringExcelname;4 5 //Note: Consider an Excel file as a database, a sheet as a table. Syntax "SELECT * FROM [sheet1$]", forms to use "[]" and "$"6 7             //1, HDR indicates to be the first row as data or column name, as the data with Hdr=no, as the column name with Hdr=yes;8             //2, through the imex=1 to the mixed type as text-based reading, to avoid null values. 9strconn =@"Provider=Microsoft.Jet.OLEDB.4.0;Data source= ' {0} '; Extended properties= ' Excel 8.0; Hdr=yes;imex=1 ';";Ten             stringstrconnection =string. Format (strconn, excelpath); OneOleDbConnection conn =NewOleDbConnection (strconnection); A Conn. Open (); -OleDbDataAdapter Oada =NewOleDbDataAdapter ("select * FROM ["+ Excelname +"$]", strconnection); -  theDtgbpatient.tablename ="Gbpatientinfo"; -Oada. Fill (dtgbpatient);//Get a DataTable -Conn. Close ();

Transferred from: http://blog.sina.com.cn/s/blog_62d1205301010llt.html

C # reads Excel data into a DataTable

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.