//----------------------32-bit machine//Comment Description//Exclepath for Excel path lot number refers to a column in an Excel file that is required Public StaticDataSet getdatatableforexcel (String exclepath) {stringStrcon =String.Empty; Strcon="Provider=Microsoft.Jet.OLEDB.4.0;Data source="+ Exclepath +"; Extended properties= ' Excel 8.0;imex=1 ';"; OleDbConnection Olecon=NewOleDbConnection (Strcon); Olecon. Open (); DataTable DT= Olecon. GetOleDbSchemaTable (OleDbSchemaGuid.Tables,NULL); stringtableName = dt. rows[0][2]. ToString (). Trim (); OleDbDataAdapter Myda=NewOleDbDataAdapter ("SELECT * FROM ["+ TableName +"] Where lot number is not null", Strcon);//Where conditions are changed according to the actual situationDataSet myds =NewDataSet (); Try{Myda. Fill (myds); } Catch{myds =NULL; } Olecon. Close (); returnmyds; } //-----------------------------64-bit machine /// <summary> ///convert data from Excel to DataTable, apply referenced COM component: Microsoft.Office.Interop.Excel.dll read Excel file/// </summary> /// <param name= "Filenameurl" >Physical Path</param> /// <param name= "Sheetindex" >Index of the sheet name</param> /// <param name= "Splitstr" >if the column already exists, the custom added string</param> /// <returns></returns> Public StaticDataTable Execletodataset (stringFilenameurl,intSheetindex,stringsplitstr) { //Microsoft.Office.Interop.Excel.Workbook WB =NULL; Microsoft.Office.Interop.Excel.Worksheet ws=NULL; BOOLIsEqual =false;//Not EqualArrayList Columnarr =NewArrayList ();//column Field TableDataSet MyDs =NewDataSet (); DataTable xlstable= MYDS.TABLES.ADD ("Show"); ObjectMissing =System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Application Excel=NewMicrosoft.Office.Interop.Excel.Application ();//lauch Excel Application if(Excel! =NULL) {Excel. Visible=false; Excel. UserControl=true; //open an Excel file in read-only formatWB = Excel. Workbooks.Open (Filenameurl, Missing,true, missing, missing, missing, missing, missing, missing,true, missing, missing, missing, missing, missing); //get the first job bookWS =(Microsoft.Office.Interop.Excel.Worksheet) WB. Worksheets.get_item (Sheetindex); //Total record rows obtained (including header column) intRowsint = ws. UsedRange.Cells.Rows.Count;//get the number of rows intColumnsint = ws. UsedRange.Cells.Columns.Count;//get the number of columnsDataRow Dr; for(inti =1; I <= columnsint; i++) { //determine if columns are the same if(I >=2) { intR =0; for(intK =1; K <= I-1; k++)//columns are compared from the first column to the i-1 column traversal { if((Microsoft.Office.Interop.Excel.Range) ws. cells[1, I]). text.tostring () = = ((Microsoft.Office.Interop.Excel.Range) ws. cells[1, K]). Text.tostring ()) {//if the value of the column equals the value of a column in the preceding columnXLSTABLE.COLUMNS.ADD ((Microsoft.Office.Interop.Excel.Range) ws. cells[1, I]). Text.tostring () + Splitstr + (r +1). ToString (),typeof(string)); Columnarr.add ((Microsoft.Office.Interop.Excel.Range) ws. cells[1, I]). Text.tostring () + Splitstr + (r +1). ToString ()); IsEqual=true; R++; Break; } Else{isequal=false; Continue; } } if(!isequal) {XLSTABLE.COLUMNS.ADD ((Microsoft.Office.Interop.Excel.Range) ws. cells[1, I]). Text.tostring (),typeof(string)); Columnarr.add ((Microsoft.Office.Interop.Excel.Range) ws. cells[1, I]). Text.tostring ()); } } Else{xlsTable.Columns.Add ((Microsoft.Office.Interop.Excel.Range) ws. cells[1, I]). Text.tostring (),typeof(string)); Columnarr.add ((Microsoft.Office.Interop.Excel.Range) ws. cells[1, I]). Text.tostring ()); } } for(inti =2; I <= rowsint; i++) {Dr=Xlstable.newrow (); for(intj =1; J <= Columnsint; J + +) {dr[columnarr[j-1]. ToString ()] =((Microsoft.Office.Interop.Excel.Range) ws. Cells[i, J]). Text.tostring (); } xlsTable.Rows.Add (DR); }} Excel. Quit (); Excel=NULL; Dispose (WS, WB); returnxlstable; }
C # 32-bit machines and 64-bit machines read Excel content to a dataset