Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

read excel file in java and store into arraylist

package testing;

import javax.swing.JFileChooser;
import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcelDemo 
{
    ArrayList<Data> list = new ArrayList<>();
    String path;

   public ReadExcelDemo(String path)
   {
       this.path = path;

        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);

             System.out.println("");

            //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 Cell.CELL_TYPE_NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "	");

                            break;
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(cell.getStringCellValue() + "	");
                            break;
                    }
                }


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

PREVIOUS NEXT
Code Example
Java :: how to disable text field java 
Java :: java xml element get attribute value 
Java :: fullscreen libgdx 
Java :: binary number input in int java 
Java :: declare id in java 
Java :: https://www.baeldung.com/hibernate-inheritance 
Java :: convert base64 to pdf object for pdf reader in android studio 
Java :: java binary search 
Java :: how to generate random large string in java 
Java :: convert arraylist of integers to array primitive 
Java :: types of methods in java 
Java :: return class name java 
Java :: Implement the static keyword – static variable, static block, static function and static class with following conditions 
Java :: java string stringbuilder array list tostring 
Java :: how to convert a arraylist to array in java 
Java :: expression régulière téléphone java 
Java :: spring boot embedded tomcat port already in use 
Java :: java long data type 
Java :: how to sort the arraylist without changing the original arraylist 
Java :: Palindrome Program in Java. 
Java :: java math random 
Java :: greatest of three in java 
Java :: regex java 
Java :: how to multiply a number by itself using for loop in java 
Java :: Set icon for toolbar android 
Java :: java finally block 
Java :: set decimal point in java 
Java :: How to launch app in your android mobile phone 
Java :: what is encapsulation in java 
Java :: java constructor example 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =