classImport {/// <summary> ///import Excel into the dataset/// </summary> /// <param name= "Strfilesourse" >the file's path and full file name, with extension</param> /// <returns></returns> PublicDataSet Exceltodataset (stringstrfilesourse) {DataSet DS=NewDataSet (); //Excel Data source (compatible with 03/10) stringstrconn ="Provider=microsoft.ace.oledb.12.0;data source="+ Strfilesourse +"; Extended properties=\ "Excel 12.0; Hdr=yes;imex=1\ ""; //ConnectionOleDbConnection conn =NewOleDbConnection (strconn); Try{Conn. Open (); //get all the sheet in ExcelDataTable Dtsheet = conn. GetOleDbSchemaTable (OleDbSchemaGuid.Tables,New Object[] {NULL,NULL,NULL,"TABLE" }); //Read all the sheet data in Excel into a table for(inti =0; i < DtSheet.Rows.Count; i++) { stringstrSheetName = dtsheet.rows[i]["table_name"]. ToString (); OleDbDataAdapter Oleda=NewOleDbDataAdapter ("select * FROM ["+ strSheetName +"]", conn); Oleda.fill (DS,"TableName"); Conn. Close (); } } Catch(Exception ex) {MessageBox.Show (ex). Message); } finally { if(Conn. State! =connectionstate.closed) {conn. Close (); } } returnds; } }
C # import Excel into the dataset