One, file asynchronous upload
The traditional form submission file upload refreshes the page and uses the Ocupload plugin to implement asynchronous uploads.
(1) Page section
(2) Action
@Controller @scope ("prototype") public class Regionaction extends baseaction<region>{@Autowiredprivate Regionservice regionservice;private File myfile;private string Myfilefilename;public string Importexcel () throws Ioexception{if (MyFile! = null) {//Is Excelif (Myfilefilename.matches ("^.+\\. i) ((xls) | ( xlsx)) {String flag = "1"; Try{regionservice.importexcel (Myfile,myfilefilename);} catch (Exception e) {e.printstacktrace (); flag = "0";} Servletactioncontext.getresponse (). setContentType ("Text/html;charset=utf-8"); Servletactioncontext.getresponse (). Getwriter (). print (flag);}} return NONE;} public void Setmyfile (File myFile) {this.myfile = MyFile;} Public String Getmyfilefilename () {return myfilefilename;} public void Setmyfilefilename (String myfilefilename) {this.myfilefilename = Myfilefilename;}}
(3) POI Read Excel
@Service @transactionalpublic class Regionserviceimpl implements regionservice{@Autowiredprivate Regiondao Regiondao; @Overridepublic void Importexcel (File myfile,string fileName) throws exception{list<region> List = new arraylist& Lt Region> (); Boolean is03excel = Filename.matches ("^.+\\. i) (XLS) $ "); FileInputStream InputStream = new FileInputStream (myFile);//1, read workbook workbook workbook = Is03excel? New Hssfworkbook (InputStream): New Xssfworkbook (InputStream);//2, read the first sheet sheet sheet = workbook.getsheetat (0);// Traversal gets each line for (row row:sheet) {//Gets the line number int num = Row.getrownum ();//The first row is not saved to the database if (num! = 0) {Row.getcell (0). Setcelltype ( cell.cell_type_string); Row.getcell (1). Setcelltype (cell.cell_type_string); Row.getcell (2). SetCellType (Cell.CELL_ type_string); Row.getcell (3). Setcelltype (cell.cell_type_string); Row.getcell (4). Setcelltype (Cell.CELL_TYPE_ string);//Gets the province and county string id = Row.getcell (0). Getstringcellvalue (); String province = Row.getcell (1). Getstringcellvalue (); String City = Row.getcell (2). GETstringcellvalue (); String district = Row.getcell (3). Getstringcellvalue (); String postcode = Row.getcell (4). Getstringcellvalue (); Region region = new Region (id,province,city,district,postcode,null,null,null); list.add;}} Regiondao.savebatch (list);}}
@Repositorypublic class Regiondaoimpl extends basedaoimpl<region> implements regiondao{@Overridepublic void Savebatch (list<region> List) {for [region Region:list] {saveorupdate (region);}}}
Business operating System (v)