1 Public classreadexecl {2 3 //private final String XLSX = ". XLSX";//2007 or later4 //private final String XLS = ". XLS";//Version 20035 6 @Test7 Public voidReadexecltest ()throwsexception{8File File =NewFile ("F:/execl/readtest2.xlsx"); 9InputStream is =NewFileInputStream (file);//Read ExeclTen One //String suffix = file.tostring (). substring (file.tostring (). LastIndexOf ("."), file.tostring (). Length ());//the suffix of the intercepted file A //Workbook wb = null;//Create a workbook - //if (xlsx.equals (suffix))//judging the suffix of execl is that version - //wb = new Xssfworkbook (is); the //Else if (xls.equals (suffix)) - //wb = new Hssfworkbook (is); - //Else - //throw new RuntimeException ("It seems this is not the suffix of execl, please re-check oh ^_^"); //if it's not execl, throw the anomaly. + -Workbook WB = Workbookfactory.create (IS);//Universal processing version of the problem, if not the accuracy of the above comments created, you can consider this method (in fact, the bottom is also determined) + ASheet Sheet = wb.getsheetat (0);//get a execl inside the sheet default starting from 0, you can also get Wb.getsheet (String name) by name; atFormulaevaluator evaluator = Wb.getcreationhelper (). Createformulaevaluator ();//Create a formula calculator (to determine what type of data is obtained in EXECL) - - intFirstRow = Sheet.getfirstrownum ();//get first row - intLastRow = Sheet.getlastrownum ();//get last row - - for(inti = FirstRow; I <= lastrow; i++) { inRow row = Sheet.getrow (i);//gets the current row - intFirstCell = Row.getfirstcellnum ();//Get first cell to intLastcell = Row.getlastcellnum ();//Get last cell + for(intj = FirstCell; J <= Lastcell; J + +){ -Cell cell = Row.getcell (j);//Get cell theCellvalue Cellvalue = evaluator.evaluate (cell);//calculates what data type the current cell is * if(Cellvalue = =NULL)//continue the Next loop if the Execl cell is empty $ Continue;Panax Notoginseng Switch(Cellvalue.getcelltype ()) { - CaseCell.cell_type_numeric: the if(dateutil.iscelldateformatted (cell)) {//the date type here is converted to a numeric type, which needs to be distinguished after processing + Try { A NewSimpleDateFormat ("yyyy mm month DD Day"). Format (NewSimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", locale.uk). Parse (Cell.getdatecellvalue (). toString ())); theSYSTEM.OUT.PRINTLN ("Numeric transfer date ..."); +}Catch(ParseException e) { - e.printstacktrace (); $ } $}Else { -System.out.println ("Numeric Type ..."); - } the Break; - CaseCell.cell_type_boolean:WuyiSystem.out.println ("Boolean type ..."); the Break; - Casecell.cell_type_string: WuSystem.out.println ("String type ..."); - Break; About CaseCell.cell_type_formula: $SYSTEM.OUT.PRINTLN ("Formula type ..."); - Break; - CaseCell.cell_type_blank: -System.out.println ("Blank type ..."); A Break; + CaseCell.cell_type_error: theSystem.out.println ("Error Type ..."); - Break; $ default: the Break; the } the } the } - inIs.close ();//remember to close the stream and release the resources the } the}
Java uses POI to execl simple _ read _ operation