Front desk:
1<form id = "Import_form" >2<input id= "Import_file_name" name= "" value= "" type= "text"/>3<input id= "Import_file" onchange= "Showfilename ()" name= "file" value= "" type= "file"/>4<input type= "button" id = "Import_box_sure" value= "OK"/>5</form>6 7<script>8 functionShowfilename () {9$ ("#import_file_name"). Val ($ ("#import_file"). Val ());Ten } One A$(function(){ - -$ (' #import_box_sure '). Live (' click ',function() { the if(!$ ("Import_file"). Val ()) { -Alert ("Please import file"); - return; - } + }); - +$ ("#import_form"). Ajaxsubmit ({ AType: ' Post ', atURL: "./importusers", -Successfunction(data) { - if(Data && data.code== "200"){ -Alert ("Import succeeded"); - //pop-up window hide - } in } - }); to + } - the</script>
Background:
1@RequestMapping (value= "/importusers", method =requestmethod.post)2 @ResponseBody3 PublicObject importusers (@RequestParam multipartfile file) {4 exceltouserlist (IS);5 }6 7 Public StaticList<user>exceltouserlist (InputStream is) {8 FinalArraylist<user> resultlist =NewArraylist<user>();9Xssfworkbook WB =NewXssworkbook (IS);Ten OneXssfsheet sheet = wb.getsheetat (0); A - for(intCurrentrownum=0;currentrownum<sheet.getlastrownum (); currentrownum++){ -Xssfrow row =Sheet.getrow (currentrownum); the - if(NULL==row) { - //exit when you encounter a real blank line - Break; + } - //The first line is the header, the second row is the table header, and the third row is the data + if(currentrownum<2){ A Continue; at } - -Map<string,object> resp =rowisempty (row); - intCode = (int) Resp.get ("code"); - //equals 1 indicates that there is no valid data in the current row and is considered a blank line - if(code==1) { in Break; - } to + //This is where we start to really traverse the cell data for each row Todo, method reference Rowisempty method - the } * $ Panax Notoginseng - } the + A the + //determines whether there is valid data in the current row. There is no valid data (such as an empty string "") in each cell in the current row, the current row is considered to have no data - Private StaticMap<string,object>Rowisempty (xssfrow row) { $hashmap<string, object> map =NewHashmap<string,object>(); $ intCellnum =row.getlastcellnum (); - for(intj=0;j<cellnum;j++){ -Xsscell cell =Row.getcell (j); the if(cell==NULL|| "". Equals (Cell.getstringcellvalue ())) { - Continue;Wuyi}Else{ the //This line is not empty as long as one cell is not satisfied -Map.put ("code", 0); Wu returnmap; - } About } $ //There are no valid data for all cells in the current row -Map.put ("Code", 1); - returnmap; - } A}
Java Import into Excel