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:de.tum.in.socket.server.ReadExcel.java

License:Apache License

/**
 * Returns the excel column names// w w w. j  a va  2 s .c o  m
 */
private static String[] getColumnNames(final Row column) {
    final String columns[] = new String[column.getPhysicalNumberOfCells()];
    final Iterator<Cell> cellIterator = column.cellIterator();
    int i = 0;
    while (cellIterator.hasNext()) {
        final Cell cell = cellIterator.next();
        columns[i++] = cell.getStringCellValue();
    }
    return columns;
}

From source file:demons.studentsmanagesystem.excel.poi.PoiSheet.java

License:Apache License

/**
 * {@inheritDoc}/*w w w  .j  av  a  2 s. co m*/
 */
@Override
public String[] getRow(final int rowNumber) {
    final Row row = this.delegate.getRow(rowNumber);
    if (row == null) {
        return null;
    }
    final List<String> cells = new LinkedList<String>();

    for (int i = 0; i < getNumberOfColumns(); i++) {
        Cell cell = row.getCell(i);
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {
                Date date = cell.getDateCellValue();
                cells.add(String.valueOf(date.getTime()));
            } else {
                cells.add(String.valueOf(cell.getNumericCellValue()));
            }
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            cells.add(String.valueOf(cell.getBooleanCellValue()));
            break;
        case Cell.CELL_TYPE_STRING:
        case Cell.CELL_TYPE_BLANK:
            cells.add(cell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_FORMULA:
            cells.add(getFormulaEvaluator().evaluate(cell).formatAsString());
            break;
        default:
            throw new IllegalArgumentException("Cannot handle cells of type " + cell.getCellType());
        }
    }
    return cells.toArray(new String[cells.size()]);
}

From source file:dias.m20150711_get_armband_data.java

public Matrix m20150711_get_armband_data() {

    eedouble = 0;//  ww  w. j  ava  2  s .  com
    gsrdouble = 0;
    phys_actdouble = 0;
    sleepdouble = 0;

    try {
        FileInputStream file = new FileInputStream(new File(DIAS.bodymediaFileUrl));

        HSSFWorkbook workbook = new HSSFWorkbook(file);

        HSSFSheet sheet = workbook.getSheetAt(0);

        Iterator<Row> rowIterator = sheet.iterator();
        int s = 0;
        int i = 0;
        int j = 0;
        int kx = 0;
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<org.apache.poi.ss.usermodel.Cell> cellIterator = row.cellIterator();
            i++;
            s = 0;
            while (cellIterator.hasNext()) {
                j++;
                org.apache.poi.ss.usermodel.Cell cell = cellIterator.next();
                switch (cell.getCellType()) {
                case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN:
                    break;
                case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC:
                    armband_data.set(i, j, cell.getNumericCellValue());

                    if (kx == 28)
                        armband_data_with_time.set(i, 5, cell.getNumericCellValue()); // Heat-Flux Average

                    if (kx == 27)
                        armband_data_with_time.set(i, 4, cell.getNumericCellValue()); //Sleep Classification

                    if (kx == 26)
                        armband_data_with_time.set(i, 3, cell.getNumericCellValue()); //Activity Class

                    if (kx == 25)
                        armband_data_with_time.set(i, 2, cell.getNumericCellValue()); //Distance

                    if (kx == 24)
                        armband_data_with_time.set(i, 1, cell.getNumericCellValue()); //Speed

                    if (kx == 23)
                        armband_data_with_time.set(i, 0, cell.getNumericCellValue()); //MET 's

                    kx++;

                    break;

                case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING:
                    if (cell.getStringCellValue().equals("NAN")) {
                        if (s == 0) {
                            i--;
                            s = 1;
                        }
                    }
                    break;
                }
            }
            kx = 0;
            j = 0;
        }

        file.close();
        s = 0;

        eedouble = 0;
        gsrdouble = 0;
        sleepdouble = 0;
        phys_actdouble = 0;

        eedouble = armband_data.get(7164, 18);
        gsrdouble = armband_data.get(7164, 14);
        sleepdouble = armband_data.get(7164, 16);
        phys_actdouble = armband_data.get(7164, 17);

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

    return armband_data;

}

From source file:dk.cubing.liveresults.uploader.parser.WcaParser.java

License:Open Source License

/**
 * @param sheet//from  w  w  w .  j  a  va2  s  .  co  m
 * @throws IllegalStateException
 */
private void parseEventNames(Sheet sheet) throws IllegalStateException {
    if (eventNames.isEmpty()) {
        Row row = sheet.getRow(2);
        if (row != null) {
            for (int i = 7; i < row.getLastCellNum(); i++) {
                Cell cell = row.getCell(i);
                if (cell != null && cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    String eventName = StringUtil.ucfirst(cell.getStringCellValue());
                    log.debug("Event name: {}", eventName);
                    eventNames.add(eventName);
                }
            }
        }
    }
}

From source file:dk.cubing.liveresults.uploader.parser.WcaParser.java

License:Open Source License

/**
 * @param sheet/*from   w w w .  j a va2 s  . c o  m*/
 * @return
 * @throws IllegalStateException
 */
private boolean isValidRegistrationSheet(Sheet sheet) throws IllegalStateException {
    boolean isValid = false;
    if (sheet != null && SHEET_TYPE_REGISTRATION.equals(sheet.getSheetName())) {
        Row versionRow = sheet.getRow(1);
        if (versionRow != null) {
            Cell versionCell = versionRow.getCell(0);
            if (versionCell != null && versionCell.getCellType() == Cell.CELL_TYPE_STRING) {
                isValid = version.equals(versionCell.getStringCellValue());
            }
        }
        if (isValid) {
            // check for basic registration columns
            Row row = sheet.getRow(2);
            if (row != null) {
                for (int i = 0; i < 6; i++) {
                    Cell cell = row.getCell(i);
                    if (cell != null && cell.getCellType() == Cell.CELL_TYPE_STRING) {
                        isValid = basicRegistrationColumns[i].equals(cell.getStringCellValue());
                        if (!isValid)
                            break;
                    }
                }
            }
        }
        // check for position formula
        if (isValid) {
            Row row = sheet.getRow(3);
            if (row != null) {
                Cell cell = row.getCell(0);
                isValid = (cell != null && cell.getCellType() == Cell.CELL_TYPE_FORMULA);
            }
        }
    }
    return isValid;
}

From source file:dk.cubing.liveresults.uploader.parser.WcaParser.java

License:Open Source License

/**
 * @param sheet//  w  w  w  .ja va  2 s .co  m
 * @return
 * @throws IllegalStateException
 */
private boolean isValidResultSheet(Sheet sheet) throws IllegalStateException {
    boolean isValid = false;
    if (sheet != null && !SHEET_TYPE_REGISTRATION.equals(sheet.getSheetName())) {
        // check for basic result columns
        Row row = sheet.getRow(3);
        if (row != null) {
            for (int i = 0; i < 4; i++) {
                Cell cell = row.getCell(i);
                if (cell != null && cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    isValid = basicResultColumns[i].equals(cell.getStringCellValue());
                    if (!isValid)
                        break;
                }
            }
        }
        // check for position formula
        if (isValid) {
            row = sheet.getRow(4);
            if (row != null) {
                Cell cell = row.getCell(0);
                isValid = (cell != null && cell.getCellType() == Cell.CELL_TYPE_FORMULA);
            }
        }
    }
    return isValid;
}

From source file:dk.cubing.liveresults.uploader.parser.WcaParser.java

License:Open Source License

@Override
protected Competition parseCompetitionDetails(Workbook workBook, Competition competition)
        throws IllegalStateException {
    if (competition.getName() == null) { // only used when automatic upload are disabled
        Sheet sheet = workBook.getSheet(SHEET_TYPE_REGISTRATION);
        if (isValidRegistrationSheet(sheet)) {
            Row row = sheet.getRow(0);/*from w  w  w . j av a2 s  .  c o  m*/
            if (row != null) {
                Cell cell = row.getCell(0);
                if (cell != null && cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    competition.setName(cell.getStringCellValue());
                }
            }
        }
    }
    return competition;
}

From source file:dk.cubing.liveresults.uploader.parser.WcaParser.java

License:Open Source License

/**
 * @param row//from   w  w w .  ja  v  a 2 s . com
 * @return
 * @throws IllegalStateException
 */
private Competitor parseCompetitorRow(Row row) throws IllegalStateException {
    Competitor competitor = new Competitor();
    // parse competitor data
    for (int i = 1; i < 6; i++) {
        Cell cell = row.getCell(i);
        if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) {
            switch (i) {
            // parse name
            case 1:
                String name = cell.getStringCellValue();
                if (name != null) {
                    name = StringUtil.ucwords(name);
                    if (name.lastIndexOf(' ') != -1) {
                        competitor.setFirstname(StringUtil.parseFirstname(name));
                        competitor.setSurname(StringUtil.parseSurname(name));
                        log.debug("Found competitor: {}", name);
                    } else {
                        log.error("[{}] Missing firstname and/or surname for row: {}",
                                row.getSheet().getSheetName(), row.getRowNum() + 1);
                    }
                }
                break;

            // parse iso country code
            case 2:
                String country = cell.getStringCellValue();
                if (country != null) {
                    String countryCode = null;
                    if (country.length() > 2) {
                        countryCode = getCountryUtil().getCountryCodeByName(country);
                    } else {
                        countryCode = getCountryUtil().getCountryByCode(country);
                    }
                    if (countryCode != null) {
                        competitor.setCountry(countryCode);
                        log.debug("Country: {} - {}", countryCode, country);
                    } else {
                        log.error("[{}] Missing country information for row: {}", row.getSheet().getSheetName(),
                                row.getRowNum() + 1);
                    }
                }
                break;

            // parse wca id
            case 3:
                String wcaId = cell.getStringCellValue();
                if (wcaId != null) {
                    wcaId = wcaId.trim();
                    if (wcaId.length() == 10) {
                        Matcher m = wcaIdPattern.matcher(wcaId);
                        if (m.find()) {
                            competitor.setWcaId(wcaId);
                            log.debug("WCA Id: {}", competitor.getWcaId());
                        } else {
                            log.warn("[{}] Invalid wcaId format: {}", row.getSheet().getSheetName(), wcaId);
                        }
                    } else {
                        log.warn("[{}] Entered WCA id has wrong length. Expected: 10, Was: {}. Row: {}",
                                new Object[] { row.getSheet().getSheetName(), wcaId.length(),
                                        row.getRowNum() + 1 });
                    }
                }
                break;

            // parse gender
            case 4:
                String gender = cell.getStringCellValue();
                if (gender != null) {
                    gender = gender.toLowerCase();
                    if ("f".equals(gender) || "m".equals(gender)) {
                        competitor.setGender(gender);
                        log.debug("Gender: {}", ("f".equals(gender) ? "Female" : "Male"));
                    } else {
                        log.warn("[{}] Invalid gender: {}", row.getSheet().getSheetName(), gender);
                    }
                } else {
                    log.warn("[{}] Missing gender information for row: {}", row.getSheet().getSheetName(),
                            row.getRowNum() + 1);
                }
                break;

            // parse birthday
            case 5:
                Date birthday = cell.getDateCellValue();
                if (birthday != null) {
                    try {
                        competitor.setBirthday(birthday);
                        log.debug("Birthday: {}", birthdayFormat.format(birthday));
                    } catch (Exception e) {
                        log.warn("[{}] Invalid birthday format: {}", row.getSheet().getSheetName(), birthday);
                    }
                } else {
                    log.warn("[{}] Missing birthday information for row: {}", row.getSheet().getSheetName(),
                            row.getRowNum() + 1);
                }
                break;
            }
        } else {
            switch (i) {
            case 1:
                log.error("[{}] Missing firstname and/or surname for row: {}", row.getSheet().getSheetName(),
                        row.getRowNum() + 1);
                break;
            case 2:
                log.error("[{}] Missing country information for row: {}", row.getSheet().getSheetName(),
                        row.getRowNum() + 1);
                break;
            case 3:
                // WCA ID are optional
                break;
            case 4:
                log.warn("[{}] Missing gender information for row: {}", row.getSheet().getSheetName(),
                        row.getRowNum() + 1);
                break;
            case 5:
                log.warn("[{}] Missing birthday information for row: {}", row.getSheet().getSheetName(),
                        row.getRowNum() + 1);
                break;
            }
        }
    }

    // parse registered events
    if (competitor.getFirstname() != null && competitor.getSurname() != null) {
        RegisteredEvents registeredEvents = new RegisteredEvents();
        for (int i = 0; i < getLastEventNum(); i++) {
            Cell cell = row.getCell(i + 7);
            boolean registered = false;
            if (cell != null && cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                log.debug("Registered for: {}", getEventName(i));
                registered = true;
            } else {
                log.debug("Not registered for: {}", getEventName(i));
                registered = false;
            }
            try {
                Method method = registeredEvents.getClass().getMethod("setSignedUpFor" + getEventName(i),
                        boolean.class);
                method.invoke(registeredEvents, registered);
            } catch (Exception e) {
                log.error("[{}] " + e.getLocalizedMessage(), row.getSheet().getSheetName(), e);
            }
        }
        competitor.setRegisteredEvents(registeredEvents);
        return competitor;
    } else {
        return null;
    }
}

From source file:dk.cubing.liveresults.uploader.parser.WcaParser.java

License:Open Source License

/**
 * @param sheet// w w w.ja v a 2  s.  c  om
 * @return
 * @throws IllegalStateException
 */
private Event parseEventDetails(Sheet sheet) throws IllegalStateException {
    Event event = new Event();
    for (int i = 0; i < 3; i++) {
        Row row = sheet.getRow(i);
        if (row != null) {
            Cell cell = row.getCell(0);
            if (cell != null && cell.getCellType() == Cell.CELL_TYPE_STRING) {
                switch (i) {
                // event name
                case 0:
                    String eventName = cell.getStringCellValue();
                    if (eventName != null) {
                        log.debug("Results for: {}", eventName);
                        event.setName(eventName);
                    }
                    break;

                // event format
                case 1:
                    String eventFormat = cell.getStringCellValue();
                    if (eventFormat != null) {
                        Matcher m = eventFormatPattern.matcher(eventFormat);
                        if (m.find()) {
                            int numberOfAttempts = Integer.parseInt(m.group(3));
                            if ("best".equals(m.group(2))) {
                                eventFormat = Integer.toString(numberOfAttempts);
                            } else {
                                eventFormat = m.group(2);
                            }
                            log.debug("Event format: {}", m.group(1));
                            event.setFormat(eventFormat.substring(0, 1));
                        }
                    }
                    break;

                // time format
                case 2:
                    String timeFormat = cell.getStringCellValue();
                    if (timeFormat != null) {
                        if (event.getName() != null && event.getName().toLowerCase().contains("multi")) { // multi bld has a special format
                            log.debug("Time format: {}", Event.TimeFormat.MULTI_BLD.toString());
                            event.setTimeFormat(Event.TimeFormat.MULTI_BLD.getValue());
                        } else {
                            Matcher m = timeFormatPattern.matcher(timeFormat);
                            if (m.find()) {
                                log.debug("Time format: {}", timeFormat);
                                event.setTimeFormat(m.group().substring(0, 1));
                            }
                        }
                    }
                    break;
                }
            }
        }
    }
    return event;
}