Example usage for org.apache.poi.ss.usermodel Cell getStringCellValue

List of usage examples for org.apache.poi.ss.usermodel Cell getStringCellValue

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Cell getStringCellValue.

Prototype

String getStringCellValue();

Source Link

Document

Get the value of the cell as a string

For numeric cells we throw an exception.

Usage

From source file:com.femsa.kof.csi.util.XlsAnalizer.java

/**
 * Mtodo encargado de la lectura y anlisis de una hoja del archivo excel
 * cargado en la interfaz grfica correspondiente a flota
 *
 *
 * @param rowIterator lista de renglones contenidos en la hoja de excel
 * @param usuario usuario que realiza el anlisis
 * @param sheetName nombre de la hoja de excel
 * @return Regresa una lista con los registros a ser almacenados en base de
 * datos// w  ww  .  j  a  v  a 2s. co m
 */
private List<XtmpinddlFlota> analizeSheetFlota(Iterator<Row> rowIterator, DcsUsuario usuario, String sheetName,
        List<DcsCatPais> paises) throws DCSException {
    int numRow = 0;
    List<XtmpinddlFlota> cargas = new ArrayList<XtmpinddlFlota>();
    XtmpinddlFlota flota;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
    Calendar calendario = Calendar.getInstance();
    end: while (rowIterator != null && rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Cell cell;
        if (numRow == 0) {

        } else {
            flota = new XtmpinddlFlota();
            cell = row.getCell(0);
            if (cell != null && cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                try {
                    calendario.setTime(sdf.parse(((int) cell.getNumericCellValue()) + ""));
                } catch (ParseException ex) {
                    Logger.getLogger(XlsAnalizer.class.getName()).log(Level.SEVERE, null, ex);
                    errors.add("Approximately " + Character.toString((char) (65 + 0)) + "" + (numRow + 1)
                            + " cell in " + sheetName + " sheet have a invalid value [" + cell + "].");
                    cargas.clear();
                    break;
                }
                flota.setAnio(calendario.get(Calendar.YEAR));
            } else if (cell != null && cell.getCellType() == Cell.CELL_TYPE_STRING) {
                try {
                    calendario.setTime(sdf.parse(cell.getStringCellValue().trim()));
                } catch (ParseException ex) {
                    Logger.getLogger(XlsAnalizer.class.getName()).log(Level.SEVERE, null, ex);
                    errors.add("Approximately " + Character.toString((char) (65 + 0)) + "" + (numRow + 1)
                            + " cell in " + sheetName + " sheet have a invalid value [" + cell + "].");
                    cargas.clear();
                    break;
                }
                flota.setAnio(calendario.get(Calendar.YEAR));
            } else {
                numRow++;
                continue;
            }
            cell = row.getCell(1);
            if (cell == null || cell.getCellType() == Cell.CELL_TYPE_STRING) {
                if ("KOF".equalsIgnoreCase(cell != null ? cell.getStringCellValue().trim() : "") || paises
                        .contains(new DcsCatPais(cell != null ? cell.getStringCellValue().trim() : ""))) {
                    flota.setPais(cell != null ? cell.getStringCellValue().trim() : null);
                } else {
                    errors.add("Approximately " + Character.toString((char) (65 + 1)) + "" + (numRow + 1)
                            + " cell in " + sheetName + " sheet have a invalid value [" + cell + "].");
                    cargas.clear();
                    break;
                }
                if ("User".equalsIgnoreCase(usuario.getFkIdRol().getRol())
                        && (!usuario.getPais().equalsIgnoreCase(flota.getPais()))) {
                    throw new DCSException("Error: you can not load information from other country");
                }
            } else {
                errors.add("Approximately " + Character.toString((char) (65 + 1)) + "" + (numRow + 1)
                        + " cell in " + sheetName + " sheet have a invalid value [" + cell + "].");
                cargas.clear();
                break;
            }
            cell = row.getCell(2);
            if (cell == null || cell.getCellType() == Cell.CELL_TYPE_STRING) {
                flota.setTipo(cell != null ? cell.getStringCellValue().trim() : null);
            } else {
                errors.add("Approximately " + Character.toString((char) (65 + 2)) + "" + (numRow + 1)
                        + " cell in " + sheetName + " sheet have a invalid value [" + cell + "].");
                cargas.clear();
                break;
            }
            cell = row.getCell(3);
            if (cell == null || cell.getCellType() == Cell.CELL_TYPE_STRING) {
                flota.setEdad(cell != null ? cell.getStringCellValue().trim() : null);
            } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                flota.setEdad((int) cell.getNumericCellValue() + "");
            } else {
                errors.add("Approximately " + Character.toString((char) (65 + 3)) + "" + (numRow + 1)
                        + " cell in " + sheetName + " sheet have a invalid value [" + cell + "].");
                cargas.clear();
                break;
            }
            cell = row.getCell(4);
            if (cell == null || cell.getCellType() == Cell.CELL_TYPE_STRING) {
                flota.setCantidad(cell != null ? Integer.parseInt(cell.getStringCellValue().trim()) : null);
            } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                flota.setCantidad((int) cell.getNumericCellValue());
            } else {
                errors.add("Approximately " + Character.toString((char) (65 + 4)) + "" + (numRow + 1)
                        + " cell in " + sheetName + " sheet have a invalid value [" + cell + "].");
                cargas.clear();
                break;
            }
            cargas.add(flota);
        }
        numRow++;
    }
    return cargas;
}

From source file:com.framework.common.ExcelSpreadsheet.java

public List<String> getSpreadSheetAsArray() {

    List<String> cellValue = new ArrayList<String>();

    // Iterate through each rows from first sheet
    Iterator<Row> rowIterator = requiredWorksheet.iterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();/*from  w  w w  .j a  va2s  .c om*/

        // For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        while (cellIterator.hasNext()) {

            Cell cell = cellIterator.next();

            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                //                        System.out.print(cell.getBooleanCellValue() + "\t\t");
                if (cell.getBooleanCellValue()) {
                    cellValue.add("TRUE");
                } else {
                    cellValue.add("FALSE");
                }

                break;
            case Cell.CELL_TYPE_NUMERIC:
                //                        System.out.print(cell.getNumericCellValue() + "\t\t");

                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    Format formatter = new SimpleDateFormat("yyyy-MM-dd");
                    String s = formatter.format(cell.getDateCellValue());
                    cellValue.add(s);
                } else {
                    double value = cell.getNumericCellValue();
                    cellValue.add(Double.toString(value));
                }

                break;
            case Cell.CELL_TYPE_STRING:
                //                        System.out.print(cell.getStringCellValue() + "\t\t");
                cellValue.add(cell.getStringCellValue());
                break;
            }
        }
        //            System.out.println("");
    }
    return cellValue;
}

From source file:com.FuntionLibrary.java

public Boolean isCellContentPresent(XSSFSheet sheet, String content) {
    Boo = false;//from  w  w w.  ja v  a 2 s . c om
    try {
        String CellContent = null;
        // Get iterator to all the rows in current sheet
        Iterator<Row> rowIterator = sheet.iterator();
        // Traversing over each row of XLSX file
        while (rowIterator.hasNext()) {
            if (Boo == true)
                break;
            Row row = rowIterator.next();
            // For each row, iterate through each columns
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                if (Boo == true)
                    break;
                Cell cell = cellIterator.next();

                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "\t");
                    CellContent = cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t");
                    CellContent = cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue() + "\t");
                    CellContent = cell.getStringCellValue();
                    break;
                default:
                    //CellContent = null;
                }
                if (CellContent.equalsIgnoreCase(content)) {
                    if (MainGui.EditExistingButtonClicked == true) {
                        break;

                    }
                    Boo = true;
                    break;
                    //else {
                    //                         throw new Exception("Data Already exists");
                    //                            }
                } else {
                    Boo = false;
                }
            }
            System.out.println("");
            if (MainGui.EditExistingButtonClicked == true && CellContent.equalsIgnoreCase(content)) {
                break;
            }
        }

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e.getMessage());
    }
    return Boo;
}

From source file:com.funtl.framework.smoke.core.commons.excel.ImportExcel.java

License:Apache License

/**
 * ??//from  ww  w . j  a  va  2  s  .c o  m
 *
 * @param row    ?
 * @param column ???
 * @return ?
 */
@SuppressWarnings("deprecation")
public Object getCellValue(Row row, int column) {
    Object val = "";
    try {
        Cell cell = row.getCell(column);
        if (cell != null) {
            if (cell.getCellTypeEnum() == CellType.NUMERIC) {
                val = cell.getNumericCellValue();
            } else if (cell.getCellTypeEnum() == CellType.STRING) {
                val = cell.getStringCellValue();
            } else if (cell.getCellTypeEnum() == CellType.FORMULA) {
                val = cell.getCellFormula();
            } else if (cell.getCellTypeEnum() == CellType.BOOLEAN) {
                val = cell.getBooleanCellValue();
            } else if (cell.getCellTypeEnum() == CellType.ERROR) {
                val = cell.getErrorCellValue();
            }
        }
    } catch (Exception e) {
        return val;
    }
    return val;
}

From source file:com.github.camaral.sheeco.exceptions.SpreadsheetViolation.java

License:Apache License

private static Object getCellValue(final Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue();
        }/* w w w  .j av a  2s  .  c o m*/
        return cell.getNumericCellValue();
    case Cell.CELL_TYPE_STRING:
        return cell.getStringCellValue();
    case Cell.CELL_TYPE_BLANK:
        return null;
    case Cell.CELL_TYPE_BOOLEAN:
        return cell.getBooleanCellValue();
    case Cell.CELL_TYPE_ERROR:
        return cell.getErrorCellValue();
    case Cell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    default:
        throw new UnsupportedOperationException("CellType " + cell.getCellType() + " is invalid");
    }
}

From source file:com.github.camaral.sheeco.SheecoTest.java

License:Apache License

private void assertHeader(Cell cell, String headerName) {
    Assert.assertNotNull(cell, headerName + " header must be created");
    Assert.assertEquals(cell.getStringCellValue(), headerName);
}

From source file:com.github.crab2died.handler.SheetTemplateHandler.java

License:Open Source License

/**
 * ???//  w w w . j a  v a2s.  com
 */
private static void initModuleConfig(SheetTemplate template) {

    for (Row row : template.sheet) {
        for (Cell c : row) {
            if (c.getCellTypeEnum() != CellType.STRING)
                continue;
            String str = c.getStringCellValue().trim().toLowerCase();
            // ??
            if (HandlerConstant.SERIAL_NUMBER.equals(str)) {
                template.serialNumberColumnIndex = c.getColumnIndex();
            }
            // ?
            if (HandlerConstant.DATA_INIT_INDEX.equals(str)) {
                template.initColumnIndex = c.getColumnIndex();
                template.initRowIndex = row.getRowNum();
                template.rowHeight = row.getHeightInPoints();
            }
            // ???
            initStyles(template, c, str);
        }
    }
}

From source file:com.github.crab2died.handler.SheetTemplateHandler.java

License:Open Source License

/**
 * ?map??Map??#//from  w w  w.  j  a v  a2s  .co m
 *
 * @param data ?
 */
public static void extendData(SheetTemplate template, Map<String, String> data) {
    if (data == null)
        return;
    for (Row row : template.sheet) {
        for (Cell c : row) {
            if (c.getCellTypeEnum() != CellType.STRING)
                continue;
            String str = c.getStringCellValue().trim();
            if (str.startsWith("#") && data.containsKey(str.substring(1))) {
                c.setCellValue(data.get(str.substring(1)));
            }
        }
    }
}

From source file:com.github.crab2died.utils.Utils.java

License:Open Source License

/**
 * ?excel/*from w  ww . j  a v a  2s  .com*/
 *
 * @param titleRow excel
 * @param clz      
 * @return ExcelHeader?
 * @throws Excel4JException 
 */
public static Map<Integer, ExcelHeader> getHeaderMap(Row titleRow, Class<?> clz) throws Excel4JException {

    List<ExcelHeader> headers = getHeaderList(clz);
    Map<Integer, ExcelHeader> maps = new HashMap<>();
    for (Cell c : titleRow) {
        String title = c.getStringCellValue();
        for (ExcelHeader eh : headers) {
            if (eh.getTitle().equals(title.trim())) {
                maps.put(c.getColumnIndex(), eh);
                break;
            }
        }
    }
    return maps;
}

From source file:com.github.crab2died.utils.Utils.java

License:Open Source License

/**
 * ??/*  w w w  . j av  a  2  s . c  o  m*/
 *
 * @param c ?
 * @return ?
 */
public static String getCellValue(Cell c) {
    String o;
    switch (c.getCellTypeEnum()) {
    case BLANK:
        o = "";
        break;
    case BOOLEAN:
        o = String.valueOf(c.getBooleanCellValue());
        break;
    case FORMULA:
        o = calculationFormula(c);
        break;
    case NUMERIC:
        if (DateUtil.isCellDateFormatted(c)) {
            o = DateUtils.date2Str(c.getDateCellValue());
        } else {
            o = String.valueOf(c.getNumericCellValue());
            o = matchDoneBigDecimal(o);
            o = RegularUtils.converNumByReg(o);
        }
        break;
    case STRING:
        o = c.getStringCellValue();
        break;
    default:
        o = null;
        break;
    }
    return o;
}