How to read values from excel file and store in Array?
Asked Answered
O

3

7

I want to read excel sheet values and store those values in an array in Java.

I have code ready to read excel sheet but i am not able to customize it to store those values in Array.

Here is my code to read an excel sheet :

package com.core.testscripts;

import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class NewExcel 
{

    private String inputFile;

    public void setInputFile(String inputFile) 
    {
        this.inputFile = inputFile;
    }

    public void read() throws IOException  
    {
        File inputWorkbook = new File(inputFile);
        Workbook w;
        try 
        {
            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet
            Sheet sheet = w.getSheet(0);
            // Loop over first 10 column and lines

            for (int j = 0; j < sheet.getColumns(); j++) 
            {
                for (int i = 0; i < sheet.getRows(); i++) 
                {
                    Cell cell = sheet.getCell(j, i);
                    System.out.println(cell.getContents());
                }
            }
        } 
        catch (BiffException e) 
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException 
    {
        NewExcel test = new NewExcel();
        test.setInputFile("D:/hellohowareyou.xls");
        test.read();
    }

}
Ocam answered 26/3, 2012 at 11:15 Comment(1)
How do you want to read ? All cells of all rows in a single array ? Or all cells per row in a two dimensional array ??Nyctaginaceous
S
3
import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class NewExcel 
{

    private String inputFile;
    String[][] data = null;
    public void setInputFile(String inputFile) 
    {
        this.inputFile = inputFile;
    }

    public String[][] read() throws IOException  
    {
        File inputWorkbook = new File(inputFile);
        Workbook w;

        try 
        {
            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet


            Sheet sheet = w.getSheet(0);
            data = new String[sheet.getColumns()][sheet.getRows()];
            // Loop over first 10 column and lines
       //     System.out.println(sheet.getColumns() +  " " +sheet.getRows());
            for (int j = 0; j <sheet.getColumns(); j++) 
            {
                for (int i = 0; i < sheet.getRows(); i++) 
                {
                    Cell cell = sheet.getCell(j, i);
                    data[j][i] = cell.getContents();
                  //  System.out.println(cell.getContents());
                }
            }

         /*   for (int j = 0; j < data.length; j++) 
            {
                for (int i = 0; i <data[j].length; i++) 
                {

                    System.out.println(data[j][i]);
                }
            } */

        } 
        catch (BiffException e) 
        {
            e.printStackTrace();
        }
    return data;
    }


}
Symphony answered 24/4, 2013 at 18:5 Comment(0)
M
0

If you really want an array, you are going to have to know how many elements you want in the array when you allocate storage for it. You can do that at runtime -- it does not have to be known at compile time -- but you have to do that before you can use the array.

Somewhere in a declarations section:

String[] dataArray = null;

and then somewhere in the code

dataArray = new String[numberOfElements];

You can make a two (or more) dimensional array on the same principle. After that, you can assign a string to any element of the array at an index less than numberOfElements.

Moly answered 26/3, 2012 at 11:27 Comment(1)
I am a QA and I want to use this code for my testing purpose. I have an Excel sheet having 5 values. And I have Selenium command of Google search in another class : selenium.type("css=#gbqfq", "Hello"); now in this command I want to Use those values from excel sheet and run this command using those values instead of "Hello".Ocam
S
-1
package Utilities;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

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.XSSFWorkbook;

public class ExcellReading {

    // public Workbook workbook= null;
    // public Sheet firstSheet= null;

    public String INPUT_XLS = "/ExcelManipulation/TestExecution.xlsx";

    public ExcellReading() {
    }

    public ExcellReading(String filepath) {
        INPUT_XLS = filepath;
    }

    public Map<Integer, List<String>> ReadExcel() throws IOException {

        FileInputStream inputStream = new FileInputStream(new File("TestExecution.xlsx"));

        Map<Integer, List<String>> data = new HashMap<Integer, List<String>>();

        Workbook workbook = new XSSFWorkbook(inputStream);

        Sheet firstSheet = workbook.getSheetAt(5);

        Iterator<Row> iterator = firstSheet.iterator();

        // Test test=new Test();
        int rowCnt = 0;

        while (iterator.hasNext()) {
            Row nextRow = iterator.next();

            Iterator<Cell> cellIterator = nextRow.cellIterator();
            List<String> obj = new ArrayList<String>();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();

                String cellobj = cell.getStringCellValue();

                if ("".equals(cell.getStringCellValue())) {
                    obj.add("Missing");

                } else if (cellobj.equals(null)) {
                    obj.add("");

                } else {
                    obj.add(cell.getStringCellValue());
                }

            }

            data.put(rowCnt, obj);
            rowCnt++;

        }
        return data;
    }

}
Sobriquet answered 10/12, 2015 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.