1

I need to store excel cell data to an array list. I've managed to get the cell data as 2 strings (data of two columns). Now I need to store the string data to array list (or two). My code is as follows:

XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sh = wb.getSheetAt(0);

int  i;
for (i=0; i <= sh.getLastRowNum(); i++)

    {

XSSFRow r = sh.getRow(i);

//Data from first column.
String col1 = r.getCell(0).getStringCellValue();

//Data from second column.
double col2 = r.getCell(1).getNumericCellValue();
}

Now I need to store them (col1 and col2) into array list. How can this be done? I need to use this array to write the data into seperate sheet later.

Thanks for your help.

4
  • 1
    Your second column data looks to be a Double data type. Do you want to keep that data type within the ArrayList? Commented Sep 3, 2018 at 10:49
  • Yes col1 is a String and col2 is Double. Commented Sep 3, 2018 at 10:57
  • 1
    You could do it this way: Above the for loop: List<List<Object>> listOfRows = new ArrayList<>(); List<Object> listOfColValues; Then inside the for loop: listOfColValues = new ArrayList<>(); listOfColValues.add(col1); listOfColValues.add(col2); listOfRows.add(listOfColValues); And to obtain the data from the 2D List Interface Array: for (int i = 0; i < listOfRows.size(); i++) { String col1Data = listOfRows.get(i).get(0).toString(); double col2Data = (double) listOfRows.get(i).get(1); System.out.println(col2Data + " - " + col2Data); } Commented Sep 3, 2018 at 11:13
  • 1
    There is a mistake in the code within the above comment. The System.out.println(col2Data + " - " + col2Data); should really be: System.out.println(col1Data + " - " + col2Data);. :/ Commented Sep 3, 2018 at 11:23

1 Answer 1

1

You can use List data = new ArrayList(), but when you'll be trying to get data from this list you'll need to cast each object to String or double which can be unsafe, if you'll be inserting new values you'll get a warning about raw type. In your case maybe consider using Map<String, Double>, it'll be more safe and natural.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.