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

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

Introduction

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

Prototype

@Deprecated
@Removal(version = "4.2")
CellType getCellTypeEnum();

Source Link

Document

Return the cell type.

Usage

From source file:addCE.ExcelReader.java

public void read() {

    try {//from w  ww  .j  a v a  2  s .c o m

        FileInputStream excelFile = new FileInputStream(new File(excelFileName));
        Workbook workbook = new XSSFWorkbook(excelFile);
        Sheet datatypeSheet = workbook.getSheetAt(0);
        Iterator<Row> iterator = datatypeSheet.iterator();

        iterator.next();
        iterator.next();
        iterator.next();

        while (iterator.hasNext()) {

            Row currentRow = iterator.next();
            ArrayList<String> data = new ArrayList<String>();
            Iterator<Cell> cellIterator = currentRow.iterator();

            while (cellIterator.hasNext()) {

                Cell currentCell = cellIterator.next();
                //getCellTypeEnum shown as deprecated for version 3.15
                //getCellTypeEnum will be renamed to getCellType starting from version 4.0
                if (currentCell == null || currentCell.getCellTypeEnum() == CellType.BLANK) {
                    data.add("None");
                } else if (currentCell.getCellTypeEnum() == CellType.STRING) {
                    data.add(currentCell.getStringCellValue());
                    //System.out.print(currentCell.getStringCellValue() + "--");
                } else if (currentCell.getCellTypeEnum() == CellType.NUMERIC) {
                    data.add(Double.toString(currentCell.getNumericCellValue()));
                    //System.out.print(currentCell.getNumericCellValue() + "--");
                }

            }
            this.attendees.add(data);

        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:br.unesp.rc.desafio.utils.Spreadsheet.java

public static ArrayList<String> ReadXlsSpreadsheet(File spreadsheet) {

    /*/*  w  w w  .  j  a va 2  s  . c o m*/
    Constructing File
    */
    ArrayList values = new ArrayList<String>();
    FileInputStream inputStr = null;
    try {
        inputStr = new FileInputStream(spreadsheet);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Spreadsheet.class.getName()).log(Level.SEVERE, null, ex);
    }

    Workbook currentSpreadsheetFile = null;

    try {
        HSSFRow row;
        currentSpreadsheetFile = new HSSFWorkbook(inputStr);
    } catch (IOException ex) {
        Logger.getLogger(Spreadsheet.class.getName()).log(Level.SEVERE, null, ex);
    }

    Sheet sheet = currentSpreadsheetFile.getSheetAt(0);
    Iterator<Row> rowItr = sheet.rowIterator();

    while (rowItr.hasNext()) {
        row = (HSSFRow) rowItr.next();
        Iterator<Cell> cellIterator = row.cellIterator();
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            String cellValue = "";
            switch (cell.getCellTypeEnum()) {
            default:
                // cellValue = cell.getCellFormula();
                cellValue = Double.toString(cell.getNumericCellValue());
                cell.setCellType(CellType.STRING);
                cell.setCellValue(cellValue);
                break;
            case NUMERIC:
                cellValue = Double.toString(cell.getNumericCellValue());
                cell.setCellType(CellType.STRING);
                cell.setCellValue(cellValue);
            case BLANK:
                break;
            case STRING:
                break;
            }
            if (!cell.getStringCellValue().isEmpty()) {
                values.add(cell.getStringCellValue());
                values.add(",");
                // System.out.println("HOLD IT");
            } else {
                values.add("0");
                values.add(",");
                // System.out.println("OBJECTION!!");
            }
            //System.out.print(cell.getStringCellValue() + " \t\t " );
        }
        //System.out.println();
        values.add(";");

    }

    try {
        inputStr.close();
    } catch (IOException ex) {
        Logger.getLogger(Spreadsheet.class.getName()).log(Level.SEVERE, null, ex);
    }
    //System.out.println(values.get(0));
    return values;
}

From source file:br.unesp.rc.desafio.utils.Spreadsheet.java

public static ArrayList<String> ReadXlsxSpreadsheet(File spreadsheet) {

    /*// ww w.  ja va2  s. c  om
    Constructing File
    */
    ArrayList values = new ArrayList<String>();
    FileInputStream inputStr = null;
    try {
        inputStr = new FileInputStream(spreadsheet);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Spreadsheet.class.getName()).log(Level.SEVERE, null, ex);
    }

    Workbook currentSpreadsheetFile = null;

    try {
        XSSFRow row1;
        currentSpreadsheetFile = new XSSFWorkbook(inputStr);
    } catch (IOException ex) {
        Logger.getLogger(Spreadsheet.class.getName()).log(Level.SEVERE, null, ex);
    }

    Sheet sheet = currentSpreadsheetFile.getSheetAt(0);
    Iterator<Row> rowItr = sheet.rowIterator();

    while (rowItr.hasNext()) {
        row1 = (XSSFRow) rowItr.next();
        Iterator<Cell> cellIterator = row1.cellIterator();
        while (cellIterator.hasNext()) {
            String cellValue = "";
            Cell cell = cellIterator.next();

            switch (cell.getCellTypeEnum()) {
            default:
                cellValue = cell.getCellFormula();
                cell.setCellType(CellType.STRING);
                cell.setCellValue(cellValue);
                break;
            case BLANK:
                break;
            case STRING:
                break;
            }
            values.add(cell.getStringCellValue());
            System.out.print(cell.getStringCellValue() + " \t\t ");
        }
        System.out.println();
    }
    try {
        inputStr.close();
    } catch (IOException ex) {
        Logger.getLogger(Spreadsheet.class.getName()).log(Level.SEVERE, null, ex);
    }

    return values;
}

From source file:com.abixen.platform.service.businessintelligence.multivisualisation.application.service.file.reader.ExcelReaderService.java

License:Open Source License

private String getTypeAsString(final Cell cell) {
    switch (cell.getCellTypeEnum()) {
    case NUMERIC:
        return isCellDateFormatted(cell) ? "DATE" : "DOUBLE";
    case STRING:/* w  w  w  .jav  a 2  s  .  c om*/
        return "STRING";
    default:
        return null;
    }
}

From source file:com.abixen.platform.service.businessintelligence.multivisualisation.application.service.file.reader.ExcelReaderService.java

License:Open Source License

private String formatIfData(final Cell cell) {
    if (cell.getCellTypeEnum() == CellType.NUMERIC && isCellDateFormatted(cell)) {
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(cell.getDateCellValue());
    }/*from   ww  w .  ja v a 2s  .  c  om*/
    return cell.toString();
}

From source file:com.abixen.platform.service.businessintelligence.multivisualisation.service.impl.parser.ExcelParserServiceImpl.java

License:Open Source License

private DataValueType getColumnTypeAsDataValueType(Cell cell, int i) {
    switch (cell.getCellTypeEnum()) {
    case NUMERIC:
        return isCellDateFormatted(cell) ? DATE : DOUBLE;
    case STRING://from ww w.  ja  v  a2  s .c  om
        return STRING;
    default:
        return null;
    }
}

From source file:com.celtris.exparse.parser.ExcelReader.java

License:Apache License

public List<SheetData<T>> readExcel(String absolutePath, Class<T> excelModelClass, boolean headerExtraction)
        throws IOException, InstantiationException, IllegalAccessException {

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

    // Create Workbook instance holding reference to .xlsx file
    XSSFWorkbook workbook = new XSSFWorkbook(file);
    DataFormatter objDefaultFormat = new DataFormatter();
    FormulaEvaluator objFormulaEvaluator = new XSSFFormulaEvaluator(workbook);

    Iterator<Sheet> sheetIterator = workbook.iterator();
    List<SheetData<T>> sheetDataList = new ArrayList<SheetData<T>>(workbook.getNumberOfSheets());
    int sheetCount = 0;
    while (sheetIterator.hasNext()) {
        sheetCount++;// w  w  w.ja v  a 2  s  .co m

        ExcelParser<T> excelParser = new ExcelParser<T>(headerExtraction, excelModelClass);
        Sheet sheet = sheetIterator.next();
        Iterator<Row> rowIterator = sheet.iterator();

        int rowCount = 0;

        // Evaluating header
        if (headerExtraction) {
            if (rowIterator.hasNext()) {

                rowCount++;

                Field[] fields = excelModelClass.getFields();
                List<String> heaaderStr = new ArrayList<String>(fields.length);

                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();

                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    String cellStrValue = cell.getStringCellValue();

                    heaaderStr.add(cellStrValue);
                }
                excelParser.processFieldAccordingToHeader(heaaderStr, sheet.getSheetName());
            }
        }

        while (rowIterator.hasNext()) {
            rowCount++;
            Row row = rowIterator.next();
            // For each row, iterate through all the columns
            Iterator<Cell> cellIterator = row.cellIterator();

            List<String> rowStr = new ArrayList<String>(excelParser.parameterCount());
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                String cellStrValue = "";
                switch (cell.getCellTypeEnum()) {
                case STRING:
                    cellStrValue = cell.getStringCellValue();
                    break;
                case NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        objFormulaEvaluator.evaluate(cell);
                        cellStrValue = objDefaultFormat.formatCellValue(cell, objFormulaEvaluator);
                    } else {

                        cellStrValue = Double.toString(cell.getNumericCellValue());
                    }
                    break;
                case BOOLEAN:
                    cellStrValue = Boolean.toString(cell.getBooleanCellValue());
                    break;
                case FORMULA:
                    cellStrValue = cell.getStringCellValue();
                    break;
                case BLANK:

                default:
                    break;
                }
                rowStr.add(cellStrValue);
            }

            excelParser.processRow(rowStr, rowCount, sheet.getSheetName());
        }

        SheetData<T> sheetData = new SheetData<T>(excelParser.getParsedObject(), sheet.getSheetName(),
                sheetCount);
        sheetDataList.add(sheetData);
    }

    file.close();
    workbook.close();
    return sheetDataList;
}

From source file:com.chlq.fileprocessor.ToCSV.java

License:Apache License

/**
 * Called to convert a row of cells into a line of data that can later be
 * output to the CSV file.//from  ww w.  j a  v  a2  s. c  o  m
 *
 * @param row An instance of either the HSSFRow or XSSFRow classes that
 *            encapsulates information about a row of cells recovered from
 *            an Excel workbook.
 */
private void rowToCSV(Row row) {
    Cell cell = null;
    int lastCellNum = 0;
    ArrayList<String> csvLine = new ArrayList<String>();

    // Check to ensure that a row was recovered from the sheet as it is
    // possible that one or more rows between other populated rows could be
    // missing - blank. If the row does contain cells then...
    if (row != null) {

        // Get the index for the right most cell on the row and then
        // step along the row from left to right recovering the contents
        // of each cell, converting that into a formatted String and
        // then storing the String into the csvLine ArrayList.
        lastCellNum = row.getLastCellNum();
        for (int i = 0; i <= lastCellNum; i++) {
            cell = row.getCell(i);
            if (cell == null) {
                csvLine.add("");
            } else {
                if (cell.getCellTypeEnum() != CellType.FORMULA) {
                    csvLine.add(this.formatter.formatCellValue(cell));
                } else {
                    csvLine.add(this.formatter.formatCellValue(cell, this.evaluator));
                }
            }
        }
        // Make a note of the index number of the right most cell. This value
        // will later be used to ensure that the matrix of data in the CSV file
        // is square.
        if (lastCellNum > this.maxRowWidth) {
            this.maxRowWidth = lastCellNum;
        }
    }
    this.csvData.add(csvLine);
}

From source file:com.cloudera.sa.ExcelRecordReader.java

License:Apache License

private Text getCellValue(Cell cell) {
    Text out = new Text();
    CellType cellType = cell.getCellTypeEnum();

    if (cellType == CellType.STRING) {
        out.set(cell.getStringCellValue());
    } else if (cellType == CellType.NUMERIC) {
        out.set(String.valueOf(cell.getNumericCellValue()));
    } else if (cellType == CellType.FORMULA) {
        out.set(cell.getCellFormula());/*from  w  w  w.j  a v a 2 s .  c o  m*/
    } else if (cellType == CellType.ERROR) {
        out.set(String.valueOf(cell.getErrorCellValue()));
    } else if (cellType == CellType.BOOLEAN) {
        out.set(String.valueOf(cell.getBooleanCellValue()));
    } else {
        out.set("");
    }

    return out;
}

From source file:com.fanniemae.ezpie.data.connectors.ExcelConnector.java

License:Open Source License

protected void readColumnNames() {
    // read the schema from the first row or the named row.
    _headerRange = null;//from w w  w.j a v a 2  s  .  c om
    Row headerRow = null;
    if (StringUtilities.isNotNullOrEmpty(_columnNameAddress)) {
        _headerRange = new ExcelRange(_columnNameAddress);
        CellReference cr = _headerRange.getStartCell();
        headerRow = _sheet.getRow(cr.getRow());
    } else {
        Iterator<Row> iterator = _sheet.iterator();
        headerRow = iterator.next();
    }

    _columnCount = 0;
    int endColumnIndex = (_headerRange == null) ? -1 : _headerRange.getEndColumn();
    String value = "";
    _columnLabels = new ArrayList<String>();
    _columnAddress = new ArrayList<String>();
    List<String> usedLabels = new ArrayList<String>();
    for (Cell cell : headerRow) {
        String columnLetter = CellReference.convertNumToColString(cell.getColumnIndex());
        if ((endColumnIndex != -1) && (cell.getColumnIndex() > endColumnIndex)) {
            break;
        }

        CellType ct = cell.getCellTypeEnum();
        if (ct == CellType.FORMULA)
            ct = cell.getCachedFormulaResultTypeEnum();
        switch (ct) {
        case STRING:
            value = cell.getStringCellValue();
            if (usedLabels.contains(value.toLowerCase()))
                value = String.format("%s_%s%d", value, columnLetter, cell.getRowIndex() + 1);
            else
                usedLabels.add(value.toLowerCase());
            _columnLabels.add(value);
            _columnAddress.add(columnLetter);
            break;
        case BOOLEAN:
        case NUMERIC:
        case FORMULA:
        case BLANK:
            _columnLabels.add(String.format("Column_%s", columnLetter));
            _columnAddress.add(columnLetter);
            break;
        default:
            break;
        }
    }

    _columnCount = _columnLabels.size();
    _dataSchema = new String[_columnCount][2];
    _dataTypes = new DataType[_columnCount];
    for (int i = 0; i < _columnCount; i++) {
        _dataSchema[i][0] = _columnLabels.get(i);
    }
}