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