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:ch.ledcom.log4jtools.config.ConfigReader.java

License:Apache License

private String readCell(Row row, int cellnum) {
    if (row == null) {
        return null;
    }/*  w ww. ja  v a  2  s. c  o  m*/
    Cell cell = row.getCell(cellnum);
    return cell == null ? (String) null : cell.getStringCellValue().trim();
}

From source file:cherry.goods.excel.ExcelReader.java

License:Apache License

/**
 * ??????<br />/*from   w  w  w  . ja v  a  2 s . co  m*/
 * 
 * @param cell ??
 * @return ?
 */
private String getCellValueAsString(Cell cell) {
    switch (cell.getCellType()) {
    case CELL_TYPE_NUMERIC:
        return getNumericCellValueAsString(cell);
    case CELL_TYPE_STRING:
        return cell.getStringCellValue();
    case CELL_TYPE_FORMULA:
        switch (cell.getCachedFormulaResultType()) {
        case CELL_TYPE_NUMERIC:
            return getNumericCellValueAsString(cell);
        case CELL_TYPE_STRING:
            return cell.getStringCellValue();
        case CELL_TYPE_BOOLEAN:
            return String.valueOf(cell.getBooleanCellValue());
        default:
            return null;
        }
    case CELL_TYPE_BLANK:
        return "";
    case CELL_TYPE_BOOLEAN:
        return String.valueOf(cell.getBooleanCellValue());
    default:
        return null;
    }
}

From source file:cherry.goods.telno.SoumuExcelParser.java

License:Apache License

/**
 * ?/*from   w  w  w  .  ja  v a 2s. c o m*/
 * 
 * @param in ?
 * @return ?????? (6?)?????
 * @throws InvalidFormatException ???
 * @throws IOException ??
 */
public Map<String, Pair<String, String>> parse(InputStream in) throws InvalidFormatException, IOException {
    Map<String, Pair<String, String>> map = new LinkedHashMap<>();
    try (Workbook workbook = WorkbookFactory.create(in)) {
        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
            Sheet sheet = workbook.getSheetAt(i);
            Integer numberCol = null;
            Integer areaCodeCol = null;
            Integer localCodeCol = null;
            boolean preparing = true;
            for (Row row : sheet) {
                if (preparing) {
                    for (Cell cell : row) {
                        String value = cell.getStringCellValue();
                        if (numberLabel.equals(value)) {
                            numberCol = cell.getColumnIndex();
                        }
                        if (areaCodeLabel.equals(value)) {
                            areaCodeCol = cell.getColumnIndex();
                        }
                        if (localCodeLabel.equals(value)) {
                            localCodeCol = cell.getColumnIndex();
                        }
                    }
                    if (numberCol != null && areaCodeCol != null && localCodeCol != null) {
                        preparing = false;
                    }
                } else {
                    String number = getCellValue(row, numberCol.intValue());
                    String areaCode = getCellValue(row, areaCodeCol.intValue());
                    String localCode = getCellValue(row, localCodeCol.intValue());
                    if (isNotEmpty(number) && isNotEmpty(areaCode) && isNotEmpty(localCode)) {
                        map.put(number, Pair.of(areaCode, localCode));
                    }
                }
            }
        }
    }
    return map;
}

From source file:cherry.goods.telno.SoumuExcelParser.java

License:Apache License

/**
 * ? () ??/*from  ww w.j a  v  a  2 s  .co m*/
 * 
 * @param row 
 * @param col ?
 * @return ? ()
 */
private String getCellValue(Row row, int col) {
    Cell cell = row.getCell(col);
    if (cell == null) {
        return null;
    }
    return cell.getStringCellValue();
}

From source file:cherry.parser.worksheet.CellUtil.java

License:Apache License

public static String getCellValueAsString(Cell cell) {
    switch (cell.getCellType()) {
    case CELL_TYPE_STRING:
        return cell.getStringCellValue();
    case CELL_TYPE_NUMERIC:
        return String.valueOf((int) cell.getNumericCellValue());
    case CELL_TYPE_FORMULA:
        switch (cell.getCachedFormulaResultType()) {
        case CELL_TYPE_STRING:
            return cell.getStringCellValue();
        case CELL_TYPE_NUMERIC:
            return String.valueOf((int) cell.getNumericCellValue());
        default:/*  w ww  . j a va 2 s.  c  om*/
            return null;
        }
    default:
        return null;
    }
}

From source file:clases.excel.EXCELreader.java

private void prepareList(String path) {

    excelString = "";
    listaEXCEL = new ArrayList<>();
    int sw = 0;//0 no,1 si, 2 ready
    XSSFWorkbook workbook;//from  w ww. ja  v  a2 s.  c om

    try {
        //Create Workbook instance holding reference to .xlsx file
        workbook = new XSSFWorkbook(new FileInputStream(new File(path)));

        //Get first/desired sheet from the workbook
        XSSFSheet sheet = workbook.getSheetAt(0);

        //Iterate through each rows one by one
        Iterator<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext()) {
            String[] linea = new String[parametros];
            Row row = rowIterator.next();
            //For each row, iterate through all the columns
            Iterator<Cell> cellIterator = row.cellIterator();

            //  if (rowcont2 ==16) {
            //                excelString += ++rowcont + "***";
            int rowcont = 0;
            while (cellIterator.hasNext()) {

                Cell cell = cellIterator.next();

                //JOptionPane.showMessageDialog(null, "Tipo *" + cell.getCellType() + "*");
                //Check the cell type and format accordingly
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_NUMERIC:
                    //leyendo tabla
                    if (sw == 2) {
                        Double d = cell.getNumericCellValue();//String.format("%d", d.intValue())
                        excelString += ((d % 1 != 0) ? d.toString() : d.toString()) + "\t";
                        linea[rowcont++] = ((d % 1 != 0) ? d.toString() : d.toString());
                    }
                    break;

                case Cell.CELL_TYPE_STRING:
                    // JOptionPane.showMessageDialog(null, "String *" + cell.getStringCellValue().trim() + "*");
                    if (sw == 0) {
                        if (cell.getStringCellValue().trim().equals(init_detalle)) {
                            sw = 1;
                        }
                    }
                    //leyendo tabla
                    if (sw == 2) {
                        excelString += cell.getStringCellValue() + "\t";
                        linea[rowcont++] = cell.getStringCellValue();
                    }
                    break;

                case Cell.CELL_TYPE_BLANK:
                    //si estaba leyendo tabla, pero encuentra vacio
                    //  termina de leer la tabla
                    if (rowcont == 0 && sw == 2) {
                        sw = 4;
                    }
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    //leyendo tabla
                    if (sw == 2) {
                        Double d = cell.getNumericCellValue();
                        excelString += ((d % 1 != 0) ? d.toString() : d.toString()) + "\t";
                        linea[rowcont++] = ((d % 1 != 0) ? d.toString() : d.toString());
                    }
                    break;
                }
                //si ya es 1 termina el ciclo, para iniciar el detalle
                //  en la siguiente linea
                if (sw == 1) {
                    sw = 2;
                    break;
                }
                //si ya no leera mas tabla, termina de leer la row
                if (sw == 4) {
                    break;
                }
            }
            //guarda si esta leyendo tabla
            if (sw == 2 && linea[0] != null) {
                listaEXCEL.add(linea);
                excelString += "\n";
            }
            //termino de leer la tabla, cierra todo
            if (sw == 4) {
                break;
            }

            //   }
        }
        int as = 0;
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:classes.CLASSEDETEST.java

public static void main(String[] args) throws FileNotFoundException, IOException {

    //        DataControle dt = new DataControle();
    //        //from  w ww .j  a  va 2  s  .c  o m
    //        LocalDate x = LocalDate.parse("10/10/1991", dt.getFormatador());
    //        
    //        System.out.println(x.toString());
    //        
    //        System.exit(0);

    IOExcel io = new IOExcel("G:\\Pasta Eng\\xccx.xlsx");
    io.init();
    io.lerExcel();

    ControleTotal c = new ControleTotal();

    c.setListVendas(io.getListVendas());

    c.separarDados();

    System.out.println(c.produtoMaisVendido());

    System.exit(0);

    String file = "G:\\Pasta Eng\\ModeloDeCompra.xlsx";

    FileInputStream fis = new FileInputStream(new File(file));

    //create workbook instance

    XSSFWorkbook wb = new XSSFWorkbook(file);

    XSSFSheet sheet = wb.getSheetAt(0);

    //List<String> list = new ArrayList<>();

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

    int cont;

    List<String> l = new ArrayList<>();
    for (Row row : sheet) {

        for (Cell cell : row) {
            l.add(cell.getStringCellValue().trim().toLowerCase());
        }
        break;

        //matriz.add(list);
    }

    Comprador c = new Comprador();

    Produto p = new Produto();

    p.setNome(l.get(5));
    p.setMarca(l.get(6));
    p.setQtdTamanho_P(Integer.parseInt(l.get(7)));
    p.setQtdTamanho_M(Integer.parseInt(l.get(8)));
    p.setQtdTamanho_G(Integer.parseInt(l.get(9)));
    p.setGrupo(Integer.parseInt(l.get(10)));
    p.setValor(Double.parseDouble(l.get(11)));

    System.out.println("Produto:\n");

    System.out.println(p.getNome());
    System.out.println(p.getMarca());
    System.out.println(p.getQtdTamanho_P());
    System.out.println(p.getQtdTamanho_M());
    System.out.println(p.getQtdTamanho_G());
    System.out.println(p.getGrupo());
    System.out.println(p.getValor());

    System.out.println("Total De Produtos : " + p.getQuantidadeTotalProduto());

}

From source file:classes.IOExcel.java

public boolean lerExcel() {

    try {// ww w  .j  a va2s . c  om

        FileInputStream fis = new FileInputStream(this.getFileExcel());

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

        for (Row row : sheet) {

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

            for (Cell cell : row) {
                list.add(cell.getStringCellValue().trim());
            }

            if (list.size() >= 15) {

                Vendas venda = new Vendas();

                //Vendedor
                venda.setNomeVendedor(list.get(0));

                //Comprador
                venda.setNomeComprador(list.get(1));
                venda.setSexoComprador(list.get(2));
                venda.setEstadoComprador(list.get(3));
                venda.setDataNascimentoComprador(
                        LocalDate.parse(list.get(4), new DataControle().getFormatador()));

                //Produto
                venda.setNomeProduto(list.get(5));
                venda.setMarcaProduto(list.get(6));

                venda.setQtdTamanho_P_Produto(Integer.parseInt(list.get(7)));
                venda.setQtdTamanho_M_Produto(Integer.parseInt(list.get(8)));
                venda.setQtdTamanho_G_Produto(Integer.parseInt(list.get(9)));

                venda.setGrupoProduto((Integer.parseInt(list.get(10))));
                venda.setValorProduto(Double.parseDouble(list.get(11)));

                //Pagamento
                venda.setTipoPagamento(Boolean.parseBoolean(list.get(12)));
                venda.setDebOrCredPagamento(list.get(13));
                venda.setBandeiraCartaoPagamento(list.get(14));
                venda.setNumeroParcelasPagamento(Integer.parseInt(list.get(15)));

                this.getListVendas().add(venda);
                this.numeroDeRegistros++;
            }
        }

        return true;

    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    }

}

From source file:cn.afterturn.easypoi.excel.export.template.ExcelExportOfTemplateUtil.java

License:Apache License

/**
 * ??,??//www  . jav  a 2 s  .  c  o m
 *
 * @param sheet
 * @return
 */
private Map<String, Integer> getTitleMap(Sheet sheet) {
    Row row = null;
    Iterator<Cell> cellTitle;
    Map<String, Integer> titlemap = new HashMap<String, Integer>();
    for (int j = 0; j < teplateParams.getHeadingRows(); j++) {
        row = sheet.getRow(j + teplateParams.getHeadingStartRow());
        cellTitle = row.cellIterator();
        int i = row.getFirstCellNum();
        while (cellTitle.hasNext()) {
            Cell cell = cellTitle.next();
            String value = cell.getStringCellValue();
            if (!StringUtils.isEmpty(value)) {
                titlemap.put(value, i);
            }
            i = i + 1;
        }
    }
    return titlemap;

}

From source file:cn.afterturn.easypoi.excel.export.template.ExcelExportOfTemplateUtil.java

License:Apache License

private void handlerSumCell(Sheet sheet) {
    for (TemplateSumEntity sumEntity : templateSumHandler.getDataList()) {
        Cell cell = sheet.getRow(sumEntity.getRow()).getCell(sumEntity.getCol());
        if (cell.getStringCellValue().contains(sumEntity.getSumKey())) {
            cell.setCellValue(cell.getStringCellValue().replace("sum:(" + sumEntity.getSumKey() + ")",
                    sumEntity.getValue() + ""));
        } else {/*from   w ww  . ja  va2s.  c o  m*/
            cell.setCellValue(cell.getStringCellValue() + sumEntity.getValue());
        }
    }
}