Perfectly compatible with excel2003 and excel2007 reads, all Excel types are processed, depending on the package as follows:
Poi-3.10-fnal.jar
Poi-ooxml-3.10-fnal.jar
Poi-ooxml-schemas-3.10-fnal.jar
Xmlbeans-2.6.0.jar
Dom4j-1.6.1.jar
Not much to say, directly on the code.
ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.PushbackInputStream;ImportJava.text.SimpleDateFormat;Importjava.util.Arrays;Importjava.util.Date;Importorg.apache.poi.POIXMLDocument;ImportOrg.apache.poi.hssf.usermodel.HSSFDataFormat;ImportOrg.apache.poi.hssf.usermodel.HSSFDateUtil;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;Importorg.apache.poi.openxml4j.exceptions.InvalidFormatException;ImportOrg.apache.poi.openxml4j.opc.OPCPackage;ImportOrg.apache.poi.poifs.filesystem.POIFSFileSystem;ImportOrg.apache.poi.ss.usermodel.Cell;ImportOrg.apache.poi.ss.usermodel.Row;ImportOrg.apache.poi.ss.usermodel.Sheet;ImportOrg.apache.poi.ss.usermodel.Workbook;ImportOrg.apache.poi.xssf.usermodel.XSSFWorkbook; Public classUtils {/*** Compatible with 2003 or 2007, not according to suffix XLS or xlsx * creates the appropriate hssfworkbook/xssfworkbook from * the Give N InputStream. * Your input stream must either support mark/reset, or ' be wrapped as a {@linkpushbackinputstream}! */ Public StaticWorkbook Create (InputStream INP)throwsIOException, invalidformatexception {//If clearly doesn ' t do mark/reset, wrap up if(!inp.marksupported ()) {INP=NewPushbackinputstream (INP, 8); } if(Poifsfilesystem.haspoifsheader (INP)) {return NewHssfworkbook (INP); } if(Poixmldocument.hasooxmlheader (INP)) {return NewXssfworkbook (Opcpackage.open (INP)); } Throw NewIllegalArgumentException ("Your InputStream was neither a OLE2 stream, nor an OOXML stream"); } Public Static voidMain (string[] args) {Try{InputStream stream=NewFileInputStream ("C:/1.xlsx"); Workbook WB=Create (stream); Sheet Sheet= Wb.getsheetat (0); //number of rows (starting at 0, equivalent to the index of the last row), Number of columns intcount_row=Sheet.getlastrownum (); intCount_cell=sheet.getrow (0). Getphysicalnumberofcells (); System.out.println (Count_row+ " + " +Count_cell); string[][] Str=NewString[count_row][count_cell]; Cell Cell=NULL; Row Row=NULL; for(inti = 0; i < Count_row; i++) { for(intj = 0; J < Count_cell; J + +) {row=sheet.getrow (i+1); Cell=Row.getcell (j); String obj_content=""; if(Cell = =NULL) { Continue; } System.out.println (Cell.getcelltype ()+ "" + Cell.getcellstyle (). getDataFormat () + "" +Cell.getcellstyle (). getdataformatstring ()); //+ cell.getcellformula () + "" + cell.getcachedformularesulttype () if(Cell.getcelltype () = =cell. Cell_type_blank) {obj_content=""; }Else if(Cell.getcelltype () = =cell. Cell_type_numeric) {/*** YYYY-MM-DD-----yyyy year m D Day---31 yyyy m month-----------------58 m D Day hh:mm-----------h mm min-------32 * Date date = org.apache.poi.ss.usermodel.DateUtil.getJavaDate (value); */System.out.println (hssfdateutil.iscelldateformatted (cell)); if(hssfdateutil.iscelldateformatted (cell)) {Date Date=Cell.getdatecellvalue (); SimpleDateFormat DateFormat=NULL; if(Cell.getcellstyle (). getDataFormat () = = Hssfdataformat.getbuiltinformat ("M/d/yy") ) {DateFormat=NewSimpleDateFormat ("Yyyy-mm-dd"); } Else if(Cell.getcellstyle (). getDataFormat () = = Hssfdataformat.getbuiltinformat ("H:mm:ss") ) {DateFormat=NewSimpleDateFormat ("HH:mm:ss"); } obj_content=Dateformat.format (date); System.out.println (Date+ " " +Dateformat.format (date)); } Else{obj_content=(int) Cell.getnumericcellvalue () + ""; } } Else if(Cell.getcelltype () = =cell. cell_type_string) {obj_content=Cell.getstringcellvalue (); } Else if(Cell.getcelltype () = =cell. Cell_type_formula) {System.out.println ("============is fuc" +Cell.getcellformula ()); } Else if(Cell.getcelltype () = =cell. Cell_type_boolean) {obj_content=string.valueof (Cell.getbooleancellvalue ()); } Else if(Cell.getcelltype () = =cell. Cell_type_error) {} Str[i][j]=obj_content; }} System.out.println (Arrays.deeptostring (str)); } Catch(invalidformatexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
POI on Excel