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.virtusa.isq.vtaf.runtime.SeleniumTestBase.java

License:Apache License

/**
 * Adds the values from excel./*from   w w  w . j  a va2s .co  m*/
 * 
 * @param path
 *            the path
 * @param index
 *            the index
 * @return the string[][]
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws InvalidFormatException
 *             the invalid format exception
 */
public final String[][] addValuesFromExcel(final String path, final String index)
        throws IOException, InvalidFormatException {

    String cellStringValue = null;
    double cellDoubleValue = 0;
    Boolean cellBooleanValue;
    byte cellErrorValue = 0;
    String[][] arrExcelContent;
    FileInputStream file = null;
    Workbook workbook = null;

    Sheet sheet = null;
    try {
        file = new FileInputStream(new File(path));
        workbook = WorkbookFactory.create(file);
        sheet = workbook.getSheetAt(Integer.parseInt(index));
        Iterator<Row> rowIterator = sheet.iterator();
        arrExcelContent = new String[sheet.getPhysicalNumberOfRows()][];
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            int rowNumber = row.getRowNum();
            Iterator<Cell> cellIterator = row.cellIterator();
            arrExcelContent[rowNumber] = new String[sheet.getRow(rowNumber).getPhysicalNumberOfCells()];
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                int cellNumber = cell.getColumnIndex();
                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    cellStringValue = cell.getStringCellValue();
                    arrExcelContent[rowNumber][cellNumber] = cellStringValue;
                } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                    cellBooleanValue = cell.getBooleanCellValue();
                    arrExcelContent[rowNumber][cellNumber] = cellBooleanValue.toString();
                } else if (cell.getCellType() == Cell.CELL_TYPE_ERROR) {
                    cellErrorValue = cell.getErrorCellValue();
                    arrExcelContent[rowNumber][cellNumber] = Byte.toString(cellErrorValue);
                } else if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
                    cellStringValue = cell.getCellFormula();
                    arrExcelContent[rowNumber][cellNumber] = cellStringValue;

                } else {

                    cellDoubleValue = cell.getNumericCellValue();
                    arrExcelContent[rowNumber][cellNumber] = Double.toString(cellDoubleValue);
                }
            }

        }
    } finally {
        if (((InputStream) workbook) != null) {
            ((InputStream) workbook).close();
        }
    }
    return arrExcelContent;
}

From source file:com.vodafone.poms.ii.helpers.ActivityCodeLoader.java

public static String getCellValue(Cell cell) {
    cell.setCellType(Cell.CELL_TYPE_STRING);
    return cell.getStringCellValue();
}

From source file:com.vodafone.poms.ii.helpers.ActivityLoader.java

public String getCellValue(Cell cell) {
    if (cell == null) {
        return "";
    }/*  w  w w  .j  av  a  2 s. c o m*/
    cell.setCellType(Cell.CELL_TYPE_STRING);
    return cell.getStringCellValue();
}

From source file:com.voicecomdemomvn.logic.XLSParser.java

public XLSParser(String filePath) {
    if (!"".equals(filePath)) {
        if (filePath.toLowerCase().endsWith(".xls")) {
            try {
                results = new ArrayList<>(); //..      ? - ? ?
                FileInputStream file = new FileInputStream(new File(filePath));
                workbook = new HSSFWorkbook(file);
                sheet = workbook.getSheetAt(0);
                rowIterator = sheet.iterator();
                while (rowIterator.hasNext()) {
                    Row row = rowIterator.next();
                    cellIterator = row.cellIterator();
                    while (cellIterator.hasNext()) {
                        Cell cell = cellIterator.next();
                        cell.setCellType(CellType.STRING);
                        results.add(cell.getStringCellValue());
                    }//from w  w w.j a  va2 s. c  o  m
                }
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(null, "   ??!",
                        "!", JOptionPane.ERROR_MESSAGE, null);
                Logger.getLogger(XLSParser.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "?    !",
                        "!", JOptionPane.ERROR_MESSAGE, null);
                Logger.getLogger(XLSParser.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else
            JOptionPane.showMessageDialog(null, "    *.xls",
                    "!", JOptionPane.ERROR_MESSAGE, null);
    } else
        JOptionPane.showMessageDialog(null, "   !", "!",
                JOptionPane.ERROR_MESSAGE, null);
}

From source file:com.web.mavenproject6.other.XLSParser.java

private static String readCell(Row row) {
    String buf = "";
    Iterator<Cell> cells = row.iterator();
    while (cells.hasNext()) {
        Cell cell = cells.next();
        int cellType = cell.getCellType();
        if (cell.getCellStyle().getLocked() == false) {
            switch (cellType) {
            case Cell.CELL_TYPE_STRING:
                buf += cell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                buf += (int) cell.getNumericCellValue();
                break;

            case Cell.CELL_TYPE_FORMULA:
                buf += cell.getNumericCellValue();
                break;
            default:
                buf += " ";
                break;
            }//from ww  w .j ava  2s  .  c o m
        }
    }
    return buf;
}

From source file:com.web.mavenproject6.other.XLSParser.java

private static String readCell(Row row, int offset, int count) {
    String buf = "";
    Iterator<Cell> cells = row.iterator();
    int index = 0;
    while (cells.hasNext()) {
        Cell cell = cells.next();

        int cellType = cell.getCellType();
        if (cell.getCellStyle().getLocked() == false) {
            if (index >= offset && index < offset + count) {
                switch (cellType) {
                case Cell.CELL_TYPE_STRING:
                    buf += cell.getStringCellValue();
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    buf += (int) cell.getNumericCellValue();
                    break;

                case Cell.CELL_TYPE_FORMULA:
                    buf += cell.getNumericCellValue();
                    break;

                }/*from ww  w  .  ja v a  2s. c om*/
            }
            index++;
        }
    }
    return buf;
}

From source file:com.xl.main.ReadExcelSampleSilk.java

public static String read(String filename) {
    Gson gson = new Gson();
    Map<String, List<SampleSinkBean>> values = new HashMap<String, List<SampleSinkBean>>();
    List<SampleSinkBean> byRow = new ArrayList<SampleSinkBean>();
    try {//from   www.j  a  v  a 2s . c  om

        FileInputStream file = null;
        if (filename == null) {
            file = new FileInputStream(new File("H:\\anil\\sample-sink.xlsx"));
        } else {
            file = new FileInputStream(new File(filename));
        }

        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook(file);

        //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()) {
            Row row = rowIterator.next();
            //For each row, iterate through all the columns
            Iterator<Cell> cellIterator = row.cellIterator();

            if (row.getRowNum() > 0 && row.getRowNum() < 20) {
                SampleSinkBean sb = new SampleSinkBean();
                //System.out.println("row value" + sheet.getRow(3).getCell(3));
                while (cellIterator.hasNext()) {//
                    Cell cell = cellIterator.next();

                    String cellString = " ";
                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_NUMERIC:
                        cellString = cell.getNumericCellValue() + "";
                        break;
                    case Cell.CELL_TYPE_FORMULA:
                        cellString = cell.getStringCellValue() + "";

                        break;
                    case Cell.CELL_TYPE_ERROR:
                        cellString = cell.getErrorCellValue() + "";
                        break;
                    default:
                        cellString = cell.getStringCellValue() + "";

                    }

                    switch (cell.getColumnIndex()) {
                    case 0:
                        sb.setFrYear(cellString);
                        break;
                    case 1:
                        sb.setVpmod(cellString);
                    case 2:
                        sb.setProjectName(cellString);
                    case 3:
                        sb.setProjectWorktype(cellString);
                    case 4:
                        sb.setBusinessObjective(cellString);

                    }

                }
                byRow.add(sb);

            }
            // System.out.println("");

        }
        values.put("sink", byRow);
        System.out.println("output *********" + gson.toJson(values));

        file.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    return gson.toJson(values);
}

From source file:com.yqboots.initializer.core.builder.excel.DataDictSheetBuilder.java

License:Apache License

@Override
protected void formatChecking(final Sheet sheet) {
    // get the title row
    final Row row = sheet.getRow(0);

    final Cell nameCell = row.getCell(0);
    final Cell textCell = row.getCell(1);
    final Cell valueCell = row.getCell(2);

    Assert.isTrue(StringUtils.equalsIgnoreCase(nameCell.getStringCellValue(), "name"),
            "Column 'name' is required");
    Assert.isTrue(StringUtils.equalsIgnoreCase(textCell.getStringCellValue(), "text"),
            "Column 'text' is required");
    Assert.isTrue(StringUtils.equalsIgnoreCase(valueCell.getStringCellValue(), "value"),
            "Column 'value' is required");
}

From source file:com.yqboots.initializer.core.builder.excel.DataDictSheetBuilder.java

License:Apache License

private static DataDict getDataDicts(final Row row) {
    DataDict result = new DataDict();
    Assert.notNull(row.getCell(0));//from   w  w w  . j  av  a2 s.c o m
    Assert.notNull(row.getCell(1));
    Assert.notNull(row.getCell(2));

    Cell cell = row.getCell(0);
    result.setName(cell.getStringCellValue());
    cell = row.getCell(1);
    result.setText(cell.getStringCellValue());
    cell = row.getCell(2);
    result.setValue(cell.getStringCellValue());
    cell = row.getCell(3);
    if (cell != null) {
        result.setDescription(cell.getStringCellValue());
    }

    return result;
}

From source file:com.yqboots.initializer.core.builder.excel.DomainSheetBuilder.java

License:Apache License

@Override
protected void formatChecking(final Sheet sheet) {
    // get the title row
    Row mergedRow = sheet.getRow(0);/*from w  w w.j  a  va  2s. c  o  m*/
    Cell mergedCell = mergedRow.getCell(0);
    Assert.isTrue(StringUtils.equalsIgnoreCase(mergedCell.getStringCellValue(), "Module"),
            "Column 'Module' is required");

    mergedCell = mergedRow.getCell(1);
    Assert.isTrue(StringUtils.equalsIgnoreCase(mergedCell.getStringCellValue(), "Domain Name"),
            "Column 'Domain Name' is required");

    mergedCell = mergedRow.getCell(2);
    Assert.isTrue(StringUtils.equalsIgnoreCase(mergedCell.getStringCellValue(), "Generated"),
            "Column 'Generated' is required");

    Row row = sheet.getRow(1);
    Assert.isTrue(StringUtils.equalsIgnoreCase(row.getCell(3).getStringCellValue(), "DB Column"),
            "Column 'DB Column' is required");
    Assert.isTrue(StringUtils.equalsIgnoreCase(row.getCell(4).getStringCellValue(), "Class Field"),
            "Column 'Class Field' is required");
    Assert.isTrue(StringUtils.equalsIgnoreCase(row.getCell(5).getStringCellValue(), "Field Type"),
            "Column 'Field Type' is required");
}