Java read excel file

Java read excel file

Java how to read excel file and store into arraylist?
we can use Apache POI API.
Apache POI provides some Java libraries for reading and writing files in Microsoft Office formats, such as Word, PowerPoint and Excel.

Download

Download:Apache POI

https://poi.apache.org/download.html

Download POI Jar

https://poi.apache.org/download.html

or

https://mavenlibs.com/jar/file/org.apache.poi/poi
https://mavenlibs.com/jar/file/org.apache.poi/poi-ooxml

Download dependency Jars

https://mavenlibs.com/jar/file/org.apache.commons/commons-collections4
https://logging.apache.org/log4j/2.x/download.html
https://commons.apache.org/proper/commons-compress/download_compress.cgi
https://mavenlibs.com/jar/file/org.apache.poi/ooxml-schemas

poi jar and dependency jars

refer ‮l:ot‬autturi.com
poi-5.2.3
    |-\lib
    |-\ooxml-lib
    |-\poi-5.2.3.jar
    |-\poi-ooxml-5.2.3.jar
    |-\poi-ooxml-full-5.2.3.jar
    |-\lib\commons-codec-1.15.jar
    |-\lib\commons-collections4-4.4.jar
    |-\lib\commons-io-2.11.0.jar
    |-\lib\commons-math3-3.6.1.jar
    |-\lib\log4j-api-2.18.0.jar
    |-\lib\SparseBitSet-1.2.jar
    |-\ooxml-lib\commons-compress-1.21.jar
    |-\ooxml-lib\commons-logging-1.2.jar
    |-\ooxml-lib\curvesapi-1.07.jar
    |-\ooxml-lib\jakarta.activation-2.0.1.jar
    |-\ooxml-lib\jakarta.xml.bind-api-3.0.1.jar
    |-\ooxml-lib\slf4j-api-1.7.36.jar
    |-\ooxml-lib\xmlbeans-5.1.1.jar

reading excel file in java

/**
 * @author lautturi.com 
 * Java example:  Java read excel file
 */

import java.util.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Lautturi {
    
	public static void main(String[] args) {
		String path = "excel2016.xlsx";
		
        try
        {
            FileInputStream file = new FileInputStream(new File(path));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook = new XSSFWorkbook(file);

            //Get first/desired sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(0);

            //Iterate through each rows one by one
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) 
            {
                Row row = rowIterator.next();
                //For each row, iterate through all the columns
                Iterator<Cell> cellIterator = row.cellIterator();

                while (cellIterator.hasNext()) 
                {
                    Cell cell = cellIterator.next();
                    //Check the cell type and format accordingly
                    switch (cell.getCellType()) 
                    { 
                        case NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "\t");

                            break;
                        case STRING:
                            System.out.print(cell.getStringCellValue() + "\t");
                            break;
                            
						default:
							break;
                    }
                }

                System.out.println("");
            }
            workbook.close();
            file.close();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
	}
}

output:

ITEM	STORE	CATEGORY	QTY	UNIT	UNIT PRICE	TOTAL	
Oranges	Grocery	Produce	2.0	lbs	2.99	
Apples	Orchard	Produce	3.0	lbs	1.99	
Bananas	Grocery	Produce	1.0	bunch	3.99	
Lettuce	MarketLautturi.com	Produce	2.0	head	2.29	
Tomatoes LautturiMarket	Produce	4.0	lbs	3.49
Created Time:2017-10-07 11:22:15  Author:lautturi