Java uses POI to execl simple _ read _ operation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.