read all the data of an excel document

0

I would like to know how I can read all the data of an excel document in java I did an algorithm but when I read a value of type int the program ends.

package com.veliz;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;

public class Main {

    public static void main(String[] args) {
        String nombreArchivo = "juan.xlsx";
        String rutaArchivo = "C:\Users\veliz\Documents\tareas\9 periodo\programacion orientada a objetos\" + nombreArchivo;
        System.out.println(rutaArchivo);
        String hoja = "Sheet1";

        try (FileInputStream file = new FileInputStream(new File(rutaArchivo))) {
            // leer archivo excel
            XSSFWorkbook worbook = new XSSFWorkbook(file);
            //obtener la hoja que se va leer
            XSSFSheet sheet = worbook.getSheetAt(0);
            //obtener todas las filas de la hoja excel
            Iterator<Row> rowIterator = sheet.iterator();

            Row row;
            // se recorre cada fila hasta el final
            while (rowIterator.hasNext()) {
                row = rowIterator.next();
                //se obtiene las celdas por fila
                Iterator<Cell> cellIterator = row.cellIterator();
                Cell cell;
                //se recorre cada celda
                while (cellIterator.hasNext()) {
                    // se obtiene la celda en espec�fico y se la imprime
                   //en esta parte cuando ya imprime los valores me deja de 
                  //funcionar no me obtiene el valor de tipo int solo String 
                  //inclusivamente cambio los valores en exel a String y no me 
                  //funciona hay algun metodo para convertir todo a string en 
                  // java
                    cell = cellIterator.next();
                    System.out.print(cell.getStringCellValue() + " | ");
                }
                System.out.println();
            }
        } catch (Exception e) {
            e.getMessage();
        }
    }
}
    
asked by jveliz 25.11.2017 в 22:38
source

2 answers

1

To know the type of data that is in the cell you should use something like the following:

......
switch(celda.getCellType()) {

        case Cell.CELL_TYPE_NUMERIC:

            if( DateUtil.isCellDateFormatted(celda) ){

               System.out.println(celda.getDateCellValue());

            }else{

               System.out.println(celda.getNumericCellValue());

            }

            break;

        case Cell.CELL_TYPE_STRING:

            System.out.println(celda.getStringCellValue());

            break;

        case Cell.CELL_TYPE_BOOLEAN:

            System.out.println(celda.getBooleanCellValue());

            break;

        }
.....
    
answered by 26.11.2017 в 23:24
0

the manual says:

  

Cells can be numeric, formula-based or string-based (text). The cell   type specifies this. String cells can not conatin numbers and numeric   cells can not contain strings (at least according to our model).

Cells can be numeric, based on a formula or text. The cell type specifies this. The string cells can not contain numbers and vice versa (depending on the model of this class).

Then, before printing, you have to use: CellType to know what type of cell you have, and after that look for the value according to the cell type.

Source: ACA

    
answered by 26.11.2017 в 00:17