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.
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); }System.out.println(col2Data + " - " + col2Data);should really be:System.out.println(col1Data + " - " + col2Data);. :/