1. Upload:
PublicString uploadfile (commonsmultipartfile file,string uploadpath,string realuploadpath) {InputStream is=NULL; OutputStream OS=NULL; Calendar Calendar= Calendar.getinstance ();//Get Time LongExcelname =calendar.gettime (). GetTime (); Try{ is=File.getinputstream (); String des= Realuploadpath + "/" +long.tostring (excelname) +File.getoriginalfilename (); OS=NewFileOutputStream (DES); byte[] buffer =New byte[1024]; intLen = 0; while(len = is.read (buffer)) >0) {os.write (buffer); } } Catch(Exception e) {e.printstacktrace (); }finally{ if(is!=NULL){ Try{is.close (); }Catch(Exception E2) {e2.printstacktrace (); } } if(os!=NULL){ Try{os.close (); }Catch(Exception E2) {e2.printstacktrace (); } } } //return path returnUploadpath + "/" +long.tostring (excelname) +File.getoriginalfilename (); }
2. Analysis:
There are two common ways to parse Excel Jxl,poi
JXL is relatively simple to use, but only supports the Excel2003 version, which means that JXL cannot parse the. xlsx Excel file, and the POI will recognize the Excel version for parsing, so most people are more inclined to poi
JXL need to import Jxl.jar package in Project when using
Poi needs to be imported
Poi-3.14-20160307.jar
Poi-ooxml-3.14-20160307.jar
Poi-ooxml-schemas-3.14-20160307.jar
Commons-io-1.4.jar
Commons-fileupload-1.2.1.jar
2.1 JXL Parsing code:
PublicString Readexcel (commonsmultipartfile file,httpservletrequest request)throwsIOException, writeexception{stringbuffer SB=NewStringBuffer ();//depositing the read content into the StringBuffer Try{Workbook Book=Workbook.getworkbook (File.getinputstream ()); Try{Sheet Sheet= Book.getsheet (0); for(inti = 0; I < 3; i++) {//I represents the number of rows for(intj = 0; J < 4; J + +) {//j = Number of columnsSb.append (Sheet.getcell (J, i). getcontents () + "\ T"); } sb.append ("\ n"); } System.out.println (SB); }finally{ if(Book! =NULL) {book.close (); } } } Catch(biffexception e) {System.err.println (e+""); } Catch(IOException e) {System.err.println (e+ "File read error"); } return""; }
2.2 POI Parsing Code:
PrivatePoifsfilesystem FS; PrivateHssfworkbook WB; PrivateHssfsheet sheet; PrivateHssfrow Row; /*Read header Excel first line content*/ Publicstring[] Readexceltitle (InputStream is) {Try{FS=NewPoifsfilesystem (IS); WB=NewHssfworkbook (FS); } Catch(IOException e) {e.printstacktrace (); } sheet= Wb.getsheetat (0); Row= Sheet.getrow (0); //total number of header columns intColnum =Row.getphysicalnumberofcells (); System.out.println ("Colnum:" +colnum); String[] Title=NewString[colnum]; for(inti = 0; i < Colnum; i++) { //Title[i] = Getstringcellvalue (Row.getcell ((short) i)); Title[i] = Getcellformatvalue (Row.getcell ( Short) (i)); } returntitle; } /*Read Content*/ Public voidreadexcelcontent (InputStream is) {Map<integer, modelcourse> content =NewHashmap<integer, modelcourse>(); Modelcourse Model=NewModelcourse (); Try{FS=NewPoifsfilesystem (IS); WB=NewHssfworkbook (FS); } Catch(IOException e) {e.printstacktrace (); } sheet= Wb.getsheetat (0); //get total number of rows intRowNum =Sheet.getlastrownum (); Row= Sheet.getrow (0); intColnum =Row.getphysicalnumberofcells (); //The body content starts with the second line and the header of the first behavior table header for(inti = 1; I <=rowNum; i++) {row=Sheet.getrow (i); intj = 0; while(J <colnum) { if(j==1) {model.setcourse_id (Getcellformatvalue (Row.getcell ( Short( j))); } Else if(j==2) {model.setcourse_name (Getcellformatvalue (Row.getcell ( Short( j))); } Else if(j==3) {model.setcourse_time (Getcellformatvalue (Row.getcell ( Short( j))); } Else if(j==4) {model.setcourse_place (Getcellformatvalue (Row.getcell ( Short( j))); } J++; } content.put (I, model); Addcourse (model); } }
Java uploads Excel and parses