Value of columns per row of excel java

1

I'm trying to get the values of the cells that form a row of an excel document. The point is that I recover all of them in a row but I would like to recover the value of a specific cell.

I put the code here. Thanks in advance.

public void leerExcel(File excel){

    List cellData = new ArrayList();

    try{

        FileInputStream fis = new FileInputStream(excel);
        XSSFWorkbook workBook = new XSSFWorkbook(fis);

        XSSFSheet hoja = workBook.getSheetAt(0);
        Iterator rowIterator = hoja.rowIterator();
        while (rowIterator.hasNext()){
            XSSFRow row = (XSSFRow) rowIterator.next();
            Iterator iterator = row.cellIterator();
            List cellTemp = new ArrayList();
                while(iterator.hasNext()){
                    XSSFCell cell = (XSSFCell) iterator.next();
                    cellTemp.add(cell);
                }
            cellData.add(cellTemp);
        }

    }catch (Exception ex){
        System.out.println(ex.getMessage());
    }
    obtenerDatos(cellData);
}

public void obtenerDatos (List cellList){

    for(int i=0; i<cellList.size(); i++){
        List listado = (List) cellList.get(i);
        for(int j=0;j<listado.size();j++){
            XSSFCell celdas = (XSSFCell) listado.get(j);
            String valor = celdas.toString();
            System.out.print(valor+" ");
        }
        System.out.println();
    }

}
    
asked by Oscar Marés Barrera 12.06.2018 в 01:28
source

1 answer

0

You are getting the value of each cell within the obtenerDatos method, the problem is that you are printing it all in concatenated form. What you could do to get them separated is the following:

public List obtenerDatos(List cellList){

    List results = new ArrayList();

    for(int i=0; i<cellList.size(); i++){
        List listado = (List) cellList.get(i);
        for(int j=0;j<listado.size();j++){
            XSSFCell celdas = (XSSFCell) listado.get(j);
            String valor = celdas.toString();
            results.add(valor);
        }
   }

   return results;
}

In this way, your method would return a list with one element for each cell that exists.

    
answered by 12.06.2018 в 21:06