Read the following two forms of Excel: *.xls and *.xlsx
Using the Apache POI API, you need to use the HSSF and XSSF class libraries
HSSF is the POI Project's pure Java implementation of the "Excel" ( -2007) (. xls) file format.
XSSF is the POI Project's pure Java implementation of the Excel OOXML (. xlsx) file format.
These 4 JARs is needed to read Excel:
Add the four jar packages to the Java Build path.
Java code to read Excel:
Package Com.file.properties;import Java.io.*;import Java.util.iterator;import Org.apache.poi.hssf.usermodel.hssfsheet;import Org.apache.poi.hssf.usermodel.hssfworkbook;import Org.apache.poi.ss.usermodel.cell;import Org.apache.poi.ss.usermodel.row;import Org.apache.poi.xssf.usermodel.xssfsheet;import org.apache.poi.xssf.usermodel.xssfworkbook;class ReadExcel {static void Readxlsx (File inputfile) {try {//Get the workbook instance for XLSX File xssfworkbook wb = new XSSFWO Rkbook (New FileInputStream (Inputfile)); Get first sheet from the workbook xssfsheet sheet = wb.getsheetat (0); Row row; Cell cell; Iterate through each rows from first sheet iterator<row> rowiterator = Sheet.iterator (); while (Rowiterator.hasnext ()) {row = Rowiterator.next (); For each row, the iterate through each columns iterator<cell> Celliterator = Row.celliterator (); while (Celliterator.hasnext ()) {cell = Celliterator.next (); Switch (Cell.getcelltype ()) {case Cell.CELL_TYPE_BOOLEAN:System.ou T.println (Cell.getbooleancellvalue ()); Break Case Cell.CELL_TYPE_NUMERIC:System.out.println (Cell.getnumericcellvalue ()); Break Case Cell.CELL_TYPE_STRING:System.out.println (Cell.getstringcellvalue ()); Break Case Cell.CELL_TYPE_BLANK:System.out.println (""); Break Default:System.out.println (cell); }}}}catch (Exception e) {System.err.println ("Exception:" + e.getmessage ());}} static void Readxls (File inputfile) {try {//Get the workbook instance for XLS file Hssfworkbook workbook = new Hssfworkbook (new FileInputStream (Inputfile)); Get first sheet from the workbook hssfsheet sheet = workbook.getsheetat (0); Cell cell; Row row; Iterate through each rows from first sheet iterator<row> rowiterator = Sheet.iterator (); while (Rowiterator.hasnext ()) {row = Rowiterator.next (); For each row, the iterate through each columns iterator<cell> Celliterator = Row.celliterator (); while (Celliterator.hasnext ()) {cell = Celliterator.next (); Switch (Cell.getcelltype ()) {case Cell.cell_type_boolean: System.out.println (Cell.getbooleancellvalue ()); Break Case Cell.CELL_TYPE_NUMERIC:System.out.println (Cell.getnumericcellvalue ()); Break Case Cell.CELL_TYPE_STRING:System.out.println (Cell.getstringcellvalue ()); Break Case Cell.CELL_TYPE_BLANK:System.out.println (""); Break Default:System.out.println (cell); }}}} catch (FileNotFoundException e) {System.err.println ("Exception" + e.getmessage ());} catch (IOException e) {System.err.println ("Exception" + e.getmessage ());}} public static void Main (string[] args) {file Inputfile = new File ("D:\\soapuistudy\\input.xls"); File InputFile2 = new file ("D:\\soapuistudy\\input.xlsx"); Readxls (Inputfile); READXLSX (InputFile2);}}
Result:
User in XLS
Password in XLS
U1
P1
U2
P2
User in xlsx
Password in xlsx
User1
Password1
User2
Password2
User3
Password3
[Java] Read Excel File Using Apache POI API