1 using(FileStream fs = File.openread (@"d:\ performance management \ list. xls"))2 {3Iworkbook wk =NewHssfworkbook (FS);//Create workbook based on file stream4 if(wk. Numberofsheets >0)5 {6Isheet sheet = wk. Getsheetat (0);//Create a worksheet7 //School Number name gender birth month professional8 for(intR =1; R <= Sheet. Lastrownum; r++)//r=1, read from line 1th, not line No. 0, row No. 0 is column name
9 {Ten #regionGet Excel Data OneIRow row = sheet. GetRow (R); A intStuid; - if(Row. Getcell (0). Celltype==celltype.blank)//PRIMARY key Stuid does not allow null, and if empty, the row data is skipped - { the Continue; - } - Else - { +Stuid = (int) row. Getcell (0). Numericcellvalue; - } + stringname = row. Getcell (1) ==NULL?NULL: Row. Getcell (1). Stringcellvalue; A stringgender = row. Getcell (2)==NULL?NULL: Row. Getcell (2). Stringcellvalue; at stringBirth = row. Getcell (3) ==NULL?NULL: Row. Getcell (3). Stringcellvalue; - stringSpecialty = row. Getcell (4)==NULL?NULL: Row. Getcell (4). Stringcellvalue; - #endregion - - stringsql ="INSERT into Ceshi values (@stuid, @name, @birth, @gender, @specialty)"; -Sqlparameter[] Para =Newsqlparameter[]{ in NewSqlParameter ("@stuid", Stuid), - //Null value is NULL in C #, but inserting a null value into the database through an SQL statement should be DBNull.Value to NewSqlParameter ("@name", name==NULL? DBNull.Value: (Object) name), + NewSqlParameter ("@birth", birth==NULL? DBNull.Value: (Object) birth), - NewSqlParameter ("@gender", gender==NULL? DBNull.Value: (Object) gender), the NewSqlParameter ("@specialty", specialty==NULL? DBNull.Value: (Object) specialty), * }; $ using(cmd =NewSqlCommand (SQL, Getconn ()))Panax Notoginseng { - if(para!=NULL) the { + cmd. Parameters.addrange (para); A } the cmd. ExecuteNonQuery (); + } - } $MessageBox.Show ("Import succeeded"); $ } -}
Excel Data Import Database