Convert the content in Excel to datatable

Source: Internet
Author: User

Using system; using system. collections. generic; using system. data; using system. data. oledb; using system. LINQ; using system. text; namespace excelread {class excelhelper {Private Static string excelconstr; private oledbconnection conn = NULL; // operation database private oledbdataadapter Ada = NULL; // fill dataset public excelhelper (string path) {excelconstr = "provider = Microsoft. ace. oledb.12.0; Data Source = "+ path +"; extended properties = Excel 12.0 "; if (conn = NULL | Conn. state = connectionstate. closed) {conn = new oledbconnection (excelconstr) ;}} public datatable getdatasource (string sheetname) {datatable dt = new datatable (); string SQL = string. empty; SQL = "select * from [" + sheetname + "]"; dt = getdt (SQL); Return DT ;} /// <summary> /// obtain Excel Data /// </Summary> /// <Param name = "SQL"> SQL used for query </param> // /<returns> </returns> Public datatable getdt (string SQL) {dataset DS = new dataset (); try {If (Conn. state = connectionstate. closed) {Conn. open ();} Ada = new oledbdataadapter (SQL, Conn); Ada. fill (DS);} catch (exception e) {Throw E;} finally {Conn. close ();} return Ds. tables [0] ;}}

 
// Obtain sheet1 data in the Excel table
// Call excelhelper _ excelhelper = new excelhelper ("Excel file path"); // name of the sheet in Excel, followed by $ symbol _ excelhelper. getdatasource ("sheet1 $ ");

 

Convert the content in Excel to 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.