C # saves Excel with SQL statements

Source: Internet
Author: User

        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

Related Article

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.