[Java] Read Excel File Using Apache POI API

Source: Internet
Author: User

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

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.