//export input from t_seats to Excel Private voidButton3_Click (Objectsender, EventArgs e) { //1. Read stringsql ="SELECT * from T_seats"; using(SqlDataReader reader =sqlhelper.executereader (SQL, CommandType.Text)) { if(reader. HasRows) {//Create WorkbookIworkbook wk =NewHssfworkbook (); //Create sheetIsheet sheet = wk. Createsheet ("T_seats"); intRowIndex =0; #regionRead and create each row//read every piece of data while(reader. Read ()) {//cc_autoid, Cc_loginid, Cc_loginpassword, Cc_username, Cc_errortimes, Cc_lockdatetime, cc_testint intautoid = reader. GetInt32 (0); stringUID = reader. GetString (1); stringPWD = reader. GetString (2); stringname = Reader. GetString (3); intErrortimes = reader. GetInt32 (4); DateTime? Lockdate = reader. IsDBNull (5) ?NULL: (DateTime?) Reader. GetDateTime (5); int? Testint = reader. IsDBNull (6) ?NULL: (int?) Reader. GetInt32 (6); IRow Row=sheet. CreateRow (RowIndex); RowIndex++; //create cells in rows likeRow. Createcell (0). Setcellvalue (autoid); Row. Createcell (1). Setcellvalue (UID); Row. Createcell (2). Setcellvalue (PWD); Row. Createcell (3). Setcellvalue (name); Row. Createcell (4). Setcellvalue (Errortimes); //inserts empty content into a cell for null values in the databaseIcell celllockdate = row. Createcell (5); if(Lockdate = =NULL) { //sets the data type of the cell to blank, which indicates an empty cellCelllockdate.setcelltype (Celltype.blank); } Else{celllockdate.setcellvalue (DateTime) lockdate); //Create a cell format ObjectIcellstyle CellStyle =wk. Createcellstyle (); Cellstyle.dataformat= Hssfdataformat.getbuiltinformat ("m/d/yy h:mm"); //Set the current date this cell is the CellStyle propertyCelllockdate.cellstyle =CellStyle; } Icell celltestint= row. Createcell (6); if(Testint = =NULL) {celltestint.setcelltype (Celltype.blank); } Else{Celltestint.setcellvalue (int) testint); } } #endregion //writing Excel to a file using(FileStream fswrite = File.openwrite ("Tseats.xls") {wk. Write (Fswrite); }}} MessageBox.Show ("Operation complete! "); //2. Write Excel } //Import the contents of Excel into a database table T_seats Private voidButton4_Click (Objectsender, EventArgs e) { using(FileStream fsread = File.openread ("Tseats.xls")) { //1. Read ExcelIworkbook wk =NewHssfworkbook (fsread); Isheet sheet= wk. Getsheetat (0); stringSql_insert ="INSERT into t_seats values (@uid, @pwd, @uname, @errorTimes, @lockDate, @testint)"; //read each row in the sheet for(intR =0; R <= Sheet. Lastrownum; r++) { //read each lineIRow row =sheet. GetRow (R); //read the other columns in addition to the first column stringLoginId = row. Getcell (1). Stringcellvalue; stringPassword = row. Getcell (2). Stringcellvalue; stringUsername = row. Getcell (3). Stringcellvalue; intErrortimes = (int) row. Getcell (4). Numericcellvalue; Double? Lockdate =NULL; Icell celllockdate= row. Getcell (5); if(Celllockdate! =NULL&& Celllockdate.celltype! =celltype.blank) {lockdate= row. Getcell (5). Numericcellvalue; } Else { //lockdate = null; } int? Testint =NULL; Icell Celltestint= row. Getcell (6); if(Celltestint! =NULL&& Celltestint.celltype! =celltype.blank) {testint= (int) Celltestint.numericcellvalue; } Else { //testint = null;} sqlparameter[] PMS=Newsqlparameter[] {NewSqlParameter ("@uid", loginId),NewSqlParameter ("@pwd", password),NewSqlParameter ("@uname", username),NewSqlParameter ("@errorTimes", Errortimes),NewSqlParameter ("@lockDate", lockdate==NULL? DBNull.Value: (Object) Datetime.fromoadate (Double) (lockdate)) ,NewSqlParameter ("@testint", testint==NULL? DBNull.Value: (Object) testint),}; //Perform an insert operationsqlhelper.executenonquery (Sql_insert, CommandType.Text, PMS); }} MessageBox.Show ("OK"); //2. Execute INSERT statement to table T_seats } }
C # operations Nopi Import Export