1, in the use of the process, has been error throw new ClassNotFoundException (name); Cause: Xmlbeans-2.6.0.jar package is not imported, it is recommended that all packages are imported into the project when using POI.
2. Case Source
ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.util.Iterator;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;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;Importorg.junit.Test; Public classTestexcels { Public StaticString FilePath = Testexcel.class. GetResource ("/test.xlsx"). GetPath (); @Test Public Static voidMain (string[] args) {readXml (FilePath); System.out.println ("-------------"); } Public Static voidreadXml (String fileName) {BooleanisE2007 =false;//determine if the format is excel2007 if(Filename.endswith ("xlsx")) isE2007=true; Try{InputStream input=NewFileInputStream (FileName);//Create an input streamWorkbook WB =NULL; //Initialize according to file format (2003 or 2007) if(isE2007) WB=NewXssfworkbook (input); ElseWB=NewHssfworkbook (input); Sheet Sheet= Wb.getsheetat (0);//get the first formiterator<row> rows = Sheet.rowiterator ();//the iterator that gets the first form while(Rows.hasnext ()) {row row= Rows.next ();//Get Row DataSystem.out.println ("Row #" + row.getrownum ());//get line number starting from 0Iterator<cell> cells = Row.celliterator ();//get the first line of Iterators while(Cells.hasnext ()) {cell cell=Cells.next (); System.out.println ("Cell #" +Cell.getcolumnindex ()); Switch(Cell.getcelltype ()) {//output data According to the type in the cell CaseHSSFCell.CELL_TYPE_NUMERIC:System.out.println (Cell.getnumericcellvalue ()); Break; CaseHSSFCell.CELL_TYPE_STRING:System.out.println (Cell.getstringcellvalue ()); Break; CaseHSSFCell.CELL_TYPE_BOOLEAN:System.out.println (Cell.getbooleancellvalue ()); Break; CaseHSSFCell.CELL_TYPE_FORMULA:System.out.println (Cell.getcellformula ()); Break; default: System.out.println ("Unsuported Sell Type"); Break; } } } } Catch(IOException ex) {ex.printstacktrace (); } }}
Related code comments can refer to Java using POI to read Excel2003
Java uses poi.3.10 to read more than version of Excel 2007 (xlsx format)