Private Static voidSaveexcel (stringFilepath,dataset DT) { BOOLHasheaders =false; stringHDR = hasheaders?"Yes":"No"; stringstrconn; if(Filepath.substring (Filepath.lastindexof ('.')). ToLower () = =". xlsx") strconn="Provider=microsoft.ace.oledb.12.0;data source="+ FilePath +"; Extended properties=\ "Excel 12.0; Hdr="+ HDR +"; imex=0\ ""; Elsestrconn="Provider=Microsoft.Jet.OLEDB.4.0;Data source="+ FilePath +"; Extended properties=\ "Excel 8.0; Hdr="+ HDR +"; imex=0\ ""; OleDbConnection cn=NewOleDbConnection (strconn); cn. Open (); stringsqlcreate; OleDbCommand cmd; //Shift -time-toilet paper-lower toilet paper-conductive layer-upper surface-lower surface-both sides-bottom-elastic girdle-overlay-waterproof film-Qian Yao patch-left and right waist sticker-Magic buckle-Sqlcreate ="CREATE TABLE Break ([TIME] varchar,[team) integer,[toilet paper] integer,[toilet paper] integer,[conductive layer] integer,[upper surface] integer,[ Side] integer,[Bottom] integer,[elastic waist] integer,[cover layer] integer,[waterproof film] integer,[qian yao paste] integer,[around waist paste] integer,[magic buckle] INTEGER)";//Create a worksheetcmd =NewOleDbCommand (Sqlcreate, CN); Cmd. ExecuteNonQuery (); Sqlcreate="CREATE TABLE Connector ([TIME] varchar,[track) integer,[toilet paper] integer,[toilet paper] integer,[conductive layer] integer,[upper surface] integer,[ Side] integer,[Bottom] integer,[elastic waist] integer,[cover layer] integer,[waterproof film] integer,[qian yao paste] integer,[around waist paste] integer,[magic buckle] INTEGER)";//Create a worksheetcmd =NewOleDbCommand (Sqlcreate, CN); Cmd. ExecuteNonQuery (); Sqlcreate="CREATE TABLE Feed ([TIME] varchar,[team] integer,[toilet paper] integer,[toilet paper] integer,[conductive layer] integer,[upper surface] integer,[ Side] integer,[Bottom] integer,[elastic waist] integer,[cover layer] integer,[waterproof film] integer,[qian yao paste] integer,[around waist paste] integer,[magic buckle] INTEGER)";//Create a worksheetcmd =NewOleDbCommand (Sqlcreate, CN); Cmd. ExecuteNonQuery (); foreach(DataRow rowinchDt. tables[0]. Rows) {//Add Data inti =2; Cmd.commandtext=string. Format ("INSERT into Break VALUES (' {$} ', {+}, {0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12})", Row. itemarray[0+i], row. Itemarray[i +1], row. Itemarray[i +2], row. Itemarray[i +3], row. Itemarray[i +4], row. Itemarray[i +5], row. Itemarray[i +6], row. Itemarray[i +7], row. Itemarray[i +8], row. Itemarray[i +9], row. Itemarray[i +Ten], row. Itemarray[i + One], row. Itemarray[i + A], row. itemarray[0], row. itemarray[1]); Cmd. ExecuteNonQuery (); //Add Datai =2+ +; Cmd.commandtext=string. Format ("INSERT into connector VALUES (' {$} ', {+}, {0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12})", Row. itemarray[0+ i], row. Itemarray[i +1], row. Itemarray[i +2], row. Itemarray[i +3], row. Itemarray[i +4], row. Itemarray[i +5], row. Itemarray[i +6], row. Itemarray[i +7], row. Itemarray[i +8], row. Itemarray[i +9], row. Itemarray[i +Ten], row. Itemarray[i + One], row. Itemarray[i + A], row. itemarray[0], row. itemarray[1]); Cmd. ExecuteNonQuery (); I=2+ ++ +; Cmd.commandtext=string. Format ("INSERT into receive VALUES (' {$} ', {+}, {0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12})", Row. itemarray[0+ i], row. Itemarray[i +1], row. Itemarray[i +2], row. Itemarray[i +3], row. Itemarray[i +4], row. Itemarray[i +5], row. Itemarray[i +6], row. Itemarray[i +7], row. Itemarray[i +8], row. Itemarray[i +9], row. Itemarray[i +Ten], row. Itemarray[i + One], row. Itemarray[i + A], row. itemarray[0], row. itemarray[1]); Cmd. ExecuteNonQuery (); } CN. Close (); }
C # saves Excel with SQL statements