Today in the company need to do a import Excel file function, so the study, the reference on the Internet some of the information is finally done, in this record to prevent later forget how to get.
I use poi3.8, so I need the jar package as follows:
Poi-3.8.jar
Poi-excelant-3.8-20120326.jar
Poi-ooxml-3.8-20120326.jar
Poi-ooxml-schemas-3.8-20120326.jar
Poi-scratchpad-3.8-20120326.jar
Xmlbeans-2.3.0.jar
Attached to the share address of Baidu Cloud disk, jar package from Apache official: Http://pan.baidu.com/s/1bnzzheR
Attach method details, method from Csdn a blog, the specific address I forgot, it is not original, but on the basis of modified, the code is more readable, in order to let the first time to do this person to understand, so the comments part of the details of the written, the actual use of the time can be deleted, This method can be compatible with XLS and xlsx two formats, and the others have not been tested:
Public String Importexcel () {Try{inputstream InputStream = new FileInputStream ("c:\\users\\administrator\\desktop\\ Test.xls ");//Get Workbook (entire Excel file) Workbook Workbook = Workbookfactory.create (InputStream) based on input stream;// Get first worksheet (first sheet in Excel file) Sheet Sheet = workbook.getsheetat (0);//Get row iterator iterator<row> Rowiterator = Sheet.rowiterator (); while (Rowiterator.hasnext ()) {//Gets the current row row row = (row) rowiterator.next ();//Gets the cell iterator iterator< cell> coliterator = Row.celliterator (); while (Coliterator.hasnext ()) {//Gets the current cell cell cell = (cell) coliterator.next ();//print column number and line number, starting with 0, Excel A1 cell print out is 0:0, B2 cells are printed out as 1:1system.out.print (Cell.getrowindex () + ":" + cell.getcolumnindex () + "");//data switch based on the data type of the cell ( Cell.getcelltype ()) {Case Cell.CELL_TYPE_STRING:System.out.println (Cell.getrichstringcellvalue (). getString ()); Break;case Cell.CELL_TYPE_NUMERIC:if (dateutil.iscelldateformatted (Cell)) {System.out.println ( Cell.getdatecellvalue ());} Else{system.out.println (Cell.getnumericcellvalue ());} Break;case Cell.cell_type_booleAN:System.out.println (Cell.getbooleancellvalue ()); Break;case Cell.CELL_TYPE_FORMULA:System.out.println ( Cell.getcellformula ()); BREAK;DEFAULT:SYSTEM.OUT.PRINTLN ("null");}}} catch (Exception e) {e.printstacktrace ();} Return "";}
Java POI Import Excel file