This feature requires only one tool class: The contents of the class are as follows, and can be modified according to your own situation:
PackageCom.ai.core.util;ImportJava.io.BufferedInputStream;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.text.DecimalFormat;ImportJava.text.SimpleDateFormat;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.Date;Importjava.util.List;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFDateUtil;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;ImportOrg.apache.poi.poifs.filesystem.POIFSFileSystem;Importcom.ai.core.entity.Equipment; Public classExceloperate { Public Static BooleanIsfile (String Path)throwsexception{fileinputstream Fin=NewFileInputStream (path); byte[] by=New byte[1000]; intSize=fin.available (); if(size==0){ return false; }Else{ return true; } } //Import Data Public StaticList<equipment>getexport (String path) {List<Equipment> list=NewArraylist<equipment>(); File File=NewFile (path); String[][] result; String s= "0"; Try{result= getData (file, 0); intRowlength =result.length; for(inti=1;i<rowlength;i++) {Equipment E=Newequipment (); E.seteid (result[i][0]); E.setak (result[i][1]); E.setids (result[i][2]); E.settype (result[i][3]); E.setver (result[i][4]); if(Result[i][5].equals ("Not used") ) {s= "0"; }Else if(Result[i][5].equals ("in use") ) {s= "1"; }Else if(Result[i][5].equals ("corrupted") ) {s= "3"; } e.setstatus (s); E.setlease_time (result[i][6]); List.add (e); } } Catch(Exception e) {e.printstacktrace (); returnlist; } returnlist; } Public Static voidMain (string[] args)throwsException {String path= "c:\\users\\administrator\\desktop\\ new Microsoft Excel worksheet. xls"; System.out.println (Getexport (path). Size ()); } /*** Read the contents of Excel, the first-dimensional array stores the values of the columns in a row, and how many rows are stored in a two-dimensional array *@paramfile read data from source Excel *@paramignorerows The number of rows ignored by the read data, and the number of rows ignored is 1 *@returnread the contents of the data in Excel *@throwsFileNotFoundException *@throwsIOException*/ Public StaticString[][] GetData (file file,intignorerows)throwsFileNotFoundException, IOException {List<String[]> result =NewArraylist<string[]>(); introwsize = 0; Bufferedinputstream in=NewBufferedinputstream (Newfileinputstream (file)); //Open HssfworkbookPoifsfilesystem fs=NULL; Try{FS=NewPoifsfilesystem (in); }Catch(Exception e) {} Hssfworkbook WB=NewHssfworkbook (FS); Hssfcell Cell=NULL; for(intSheetindex = 0; Sheetindex < Wb.getnumberofsheets (); sheetindex++) {Hssfsheet St=Wb.getsheetat (Sheetindex); //first act title, not take for(intRowIndex = ignorerows; RowIndex <= st.getlastrownum (); rowindex++) {Hssfrow row=St.getrow (RowIndex); if(Row = =NULL) { Continue; } intTemprowsize = Row.getlastcellnum () + 1; if(Temprowsize >rowsize) {Rowsize=temprowsize; } string[] Values=NewString[rowsize]; Arrays.fill (values,""); BooleanHasValue =false; for( Shortcolumnindex = 0; ColumnIndex <= row.getlastcellnum (); columnindex++) {String value= ""; Cell=Row.getcell (columnindex); if(Cell! =NULL) { //Note: Must be set to this, or it may appear garbled//cell.setencoding (hssfcell.encoding_utf_16); Switch(Cell.getcelltype ()) { CaseHSSFCell.CELL_TYPE_STRING:value=Cell.getstringcellvalue (); Break; CaseHssfcell.cell_type_numeric:if(hssfdateutil.iscelldateformatted (cell)) {Date Date=Cell.getdatecellvalue (); if(Date! =NULL) {Value=NewSimpleDateFormat ("Yyyy-mm-dd"). Format (date); } Else{Value= ""; } } Else{Value=NewDecimalFormat ("0"). Format (cell. Getnumericcellvalue ()); } Break; CaseHssfcell.cell_type_formula://no value when data generated for a formula is imported if(!cell.getstringcellvalue (). Equals ("") ) {value=Cell.getstringcellvalue (); } Else{Value= Cell.getnumericcellvalue () + ""; } Break; CaseHssfcell.cell_type_blank: Break; CaseHSSFCell.CELL_TYPE_ERROR:value= ""; Break; CaseHSSFCell.CELL_TYPE_BOOLEAN:value= (Cell.getbooleancellvalue () = =true? Y : N); Break; default: Value= ""; } } if(ColumnIndex = = 0 && value.trim (). Equals ("")) { Break; } Values[columnindex]=Righttrim (value); HasValue=true; } if(hasValue) {Result.add (values); }}} in.close (); String[][] ReturnArray=Newstring[result.size ()][rowsize]; for(inti = 0; i < returnarray.length; i++) {Returnarray[i]=(string[]) result.get (i); } returnReturnArray; } /*** Remove the space to the right of the string *@paramstr to process the string *@returnthe string after processing*/ Public Staticstring Righttrim (String str) {if(str = =NULL) { return""; } intLength =str.length (); for(inti = length-1; I >= 0; i--) { if(Str.charat (i)! = 0x20) { Break; } length--; } returnStr.substring (0, length); }}
Java reads the data from the Excle file and generates the object