Example usage for org.apache.poi.hssf.usermodel HSSFCell getStringCellValue

List of usage examples for org.apache.poi.hssf.usermodel HSSFCell getStringCellValue

Introduction

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

Prototype

public 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.ma.it.common.excel.ExcelFileManager.java

License:Open Source License

/**
 * /*www  . j ava2s .co  m*/
 * @param row
 * @param cellIdx
 * @return
 * @throws IllegalArgumentException
 */
public Long readCellAsLong(HSSFRow row, int cellIdx) throws IllegalArgumentException {
    HSSFCell cell = getCell(row, cellIdx);
    if (cell == null) {
        return null;
    }

    int cellType = cell.getCellType();
    // First evaluate formula if present
    if (cellType == HSSFCell.CELL_TYPE_FORMULA) {
        cellType = evaluator.evaluateFormulaCell(cell);
    }

    Long result;
    switch (cellType) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        double numericCellValue = cell.getNumericCellValue();
        if (numericCellValue > Long.MAX_VALUE) {
            throw new IllegalArgumentException("Value " + numericCellValue + " to big for integer!");
        }
        result = Double.valueOf(numericCellValue).longValue();
        break;
    case HSSFCell.CELL_TYPE_STRING:
        String stringCellValue = cell.getStringCellValue();
        if (!StringUtils.isNumeric(stringCellValue)) {
            throw new IllegalArgumentException("Value " + stringCellValue + " is not numeric!");
        }
        result = Double.valueOf(stringCellValue).longValue();
        break;
    default:
        result = Long.MIN_VALUE;
        break;
    }

    return result;
}

From source file:de.ma.it.common.excel.ExcelFileManager.java

License:Open Source License

/**
 * /*from   w w  w . jav  a 2 s  . c  o m*/
 * @param row
 * @param cellIdx
 * @return
 * @throws IllegalArgumentException
 */
public String readCellAsString(HSSFRow row, int cellIdx) throws IllegalArgumentException {
    HSSFCell cell = getCell(row, cellIdx);
    if (cell == null) {
        return null;
    }

    int cellType = cell.getCellType();
    // First evaluate formula if present
    if (cellType == HSSFCell.CELL_TYPE_FORMULA) {
        cellType = evaluator.evaluateFormulaCell(cell);
    }

    String result;
    switch (cellType) {
    case HSSFCell.CELL_TYPE_STRING:
        result = cell.getStringCellValue();
        break;
    default:
        result = "";
        break;
    }

    return result;
}

From source file:de.viaboxx.nlstools.formats.MBExcelPersistencer.java

License:Apache License

private Object getValue(HSSFCell cell, CellType cellType) {
    switch (cellType) {
    case NUMERIC:
        if (HSSFDateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue();
        } else {//  www .ja v  a  2s  .c o  m
            return cell.getNumericCellValue();
        }
    case FORMULA:
        return getValue(cell, cell.getCachedFormulaResultTypeEnum());
    case BOOLEAN:
        return cell.getBooleanCellValue();
    case STRING:
        return cell.getStringCellValue();
    case ERROR:
        return cell.getErrorCellValue();
    default:
        return null;
    // do not handle Formular, Error, Blank, ...
    }
}

From source file:eafit.cdei.asignacion.input.ReadCurrentCourses.java

public List<Teacher> loadCurrentOffering() throws Exception {

    try {/* w w  w .  j  av  a2  s .c  o m*/
        FileInputStream fileInputStream = new FileInputStream("current_classes.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        HSSFSheet worksheet = workbook.getSheet("currentOffering");

        boolean keepDoing = true;
        for (int i = 0; i < worksheet.getLastRowNum() + 1; i++) {

            HSSFRow row1 = worksheet.getRow(i);

            HSSFCell cellF1 = row1.getCell((short) 5);
            String f1Val = ((int) cellF1.getNumericCellValue()) + "";

            HSSFCell cellW1 = row1.getCell((short) 22);
            String w1Val = cellW1.getStringCellValue();

            HSSFCell cellX1 = row1.getCell((short) 23);
            String x1Val = cellX1.getStringCellValue();

            HSSFCell cellY1 = row1.getCell((short) 24);
            String y1Val = cellY1.getStringCellValue();

            HSSFCell cellAU1 = row1.getCell((short) 46);
            String au1Val = ((int) cellAU1.getNumericCellValue()) + "";

            HSSFCell cellAW1 = row1.getCell((short) 48);
            String aw1Val = cellAW1.getStringCellValue();

            if (f1Val == null || f1Val.length() == 0 || f1Val.equals("0")) {
                keepDoing = false;
            }

            Course c = new Course();

            c.setHourStart(f1Val);
            c.setHourEnd(au1Val);
            c.setDowList(generateDaysList(aw1Val));
            c.setNameCourse(x1Val);
            c.setCodeCourse(w1Val);
            c.setCodeCourse(y1Val);

            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HHmm");
            if (au1Val.length() == 3) {
                au1Val = "0" + au1Val;
            }

            if (f1Val.length() == 3) {
                f1Val = "0" + f1Val;
            }

            c.setHourEndLocal(LocalTime.parse(au1Val, dtf));
            c.setHourStartLocal(LocalTime.parse(f1Val, dtf));

            c.setDateStartLocal(LocalTime.parse(au1Val, dtf));
            c.setDateEndLocal(LocalTime.parse(aw1Val, dtf));

            System.out.println(c);

        }

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

    return null;
}

From source file:eafit.cdei.asignacion.input.ReadCurrentOffering.java

public List<Teacher> loadCurrentOffering() throws Exception {

    try {//from  w  w  w  .  j a  v a2 s.c om
        FileInputStream fileInputStream = new FileInputStream("currentOffering.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        HSSFSheet worksheet = workbook.getSheet("currentOffering");

        boolean keepDoing = true;
        for (int i = 0; i < worksheet.getLastRowNum() + 1; i++) {

            HSSFRow row1 = worksheet.getRow(i);

            HSSFCell cellF1 = row1.getCell((short) 5);
            String f1Val = ((int) cellF1.getNumericCellValue()) + "";

            HSSFCell cellW1 = row1.getCell((short) 22);
            String w1Val = cellW1.getStringCellValue();

            HSSFCell cellX1 = row1.getCell((short) 23);
            String x1Val = cellX1.getStringCellValue();

            HSSFCell cellY1 = row1.getCell((short) 24);
            String y1Val = cellY1.getStringCellValue();

            HSSFCell cellAU1 = row1.getCell((short) 46);
            String au1Val = ((int) cellAU1.getNumericCellValue()) + "";

            HSSFCell cellAW1 = row1.getCell((short) 48);
            String aw1Val = cellAW1.getStringCellValue();

            HSSFCell cellAB = row1.getCell((short) 27);
            String abVal = cellAB.getStringCellValue();

            HSSFCell cellAC = row1.getCell((short) 28);
            String acVal = cellAC.getStringCellValue();

            if (f1Val == null || f1Val.length() == 0 || f1Val.equals("0")) {
                keepDoing = false;
            }

            Course c = new Course();

            c.setHourStart(f1Val);
            c.setHourEnd(au1Val);
            c.setDowList(generateDaysList(aw1Val));
            c.setNameCourse(x1Val);
            c.setCodeCourse(w1Val);
            c.setCodeCourse(y1Val);

            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HHmm");
            if (au1Val.length() == 3) {
                au1Val = "0" + au1Val;
            }

            if (f1Val.length() == 3) {
                f1Val = "0" + f1Val;
            }

            c.setHourEndLocal(LocalTime.parse(au1Val, dtf));
            c.setHourStartLocal(LocalTime.parse(f1Val, dtf));

            DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyyMMdd");

            //c.setDateStart(LocalDate.parse(abVal,dtf1));
            //c.setHourStartLocal(LocalTime.parse(f1Val,dtf));

            System.out.println(c);

        }

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

    return null;
}

From source file:eafit.cdei.asignacion.input.ReadTeacherAvailability.java

@Override
public List<Teacher> loadTeacherAvailability() throws Exception {

    try {/*from  www .  j  a  v a  2  s  . c  o m*/
        FileInputStream fileInputStream = new FileInputStream("googleDoc.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        HSSFSheet worksheet = workbook.getSheet("Form Responses 1");

        boolean keepDoing = true;
        for (int i = 1; i < worksheet.getLastRowNum() + 1; i++) {

            HSSFRow row1 = worksheet.getRow(i);

            HSSFCell cellB1 = row1.getCell((short) 1);
            String b1Val = cellB1.getStringCellValue() + "";

            HSSFCell cellG1 = row1.getCell((short) 6);
            String g1Val = cellG1.getStringCellValue() + "";

            HSSFCell cellH1 = row1.getCell((short) 7);
            String h1Val = cellH1.getStringCellValue() + "";

            HSSFCell cellJ1 = row1.getCell((short) 8);
            String j1Val = cellJ1.getStringCellValue() + "";

            HSSFCell cellK1 = row1.getCell((short) 9);
            String k1Val = cellK1.getStringCellValue() + "";

            HSSFCell cellI1 = row1.getCell((short) 10);
            String i1Val = cellI1.getStringCellValue() + "";

            HSSFCell cellL1 = row1.getCell((short) 11);
            String l1Val = cellL1.getStringCellValue() + "";

            HSSFCell cellM1 = row1.getCell((short) 12);
            String m1Val = cellM1.getStringCellValue() + "";

            HSSFCell cellN1 = row1.getCell((short) 13);
            String n1Val = cellN1.getStringCellValue() + "";

            HSSFCell cellO1 = row1.getCell((short) 14);
            String o1Val = cellO1.getStringCellValue() + "";

            HSSFCell cellP1 = row1.getCell((short) 15);
            String p1Val = cellP1.getStringCellValue() + "";

            HSSFCell cellQ1 = row1.getCell((short) 16);
            String q1Val = cellQ1.getStringCellValue() + "";

            HSSFCell cellR1 = row1.getCell((short) 17);
            String r1Val = cellR1.getStringCellValue() + "";

            HSSFCell cellS1 = row1.getCell((short) 18);
            String s1Val = cellS1.getStringCellValue() + "";

            HSSFCell cellT1 = row1.getCell((short) 19);
            String t1Val = cellT1.getStringCellValue() + "";

            HSSFCell cellU1 = row1.getCell((short) 20);
            String u1Val = cellU1.getStringCellValue() + "";

            HSSFCell cellV1 = row1.getCell((short) 21);
            String v1Val = cellV1.getStringCellValue() + "";

            HSSFCell cellW1 = row1.getCell((short) 22);
            String w1Val = cellW1.getStringCellValue() + "";

            HSSFCell cellX1 = row1.getCell((short) 23);
            String x1Val = cellX1.getStringCellValue() + "";

            HSSFCell cellY1 = row1.getCell((short) 24);
            String y1Val = cellY1.getStringCellValue() + "";

            HSSFCell cellZ1 = row1.getCell((short) 25);
            String z1Val = cellZ1.getStringCellValue() + "";

            HSSFCell cellAA1 = row1.getCell((short) 26);
            String aa1Val = cellAA1.getStringCellValue() + "";

            HSSFCell cellAB1 = row1.getCell((short) 27);
            String ab1Val = cellAB1.getStringCellValue() + "";

            if (b1Val.length() == 0) {
                keepDoing = false;
            } else {
                Teacher t = new Teacher();
                t.setFullName(b1Val);

                t.addCourseAvaliability(generateMTF(getAnswer(g1Val), "06", "08"));
                t.addCourseAvaliability(generateMTF(getAnswer(h1Val), "08", "10"));
                t.addCourseAvaliability(generateMTF(getAnswer(i1Val), "10", "12"));
                t.addCourseAvaliability(generateMTF(getAnswer(j1Val), "12", "14"));
                t.addCourseAvaliability(generateMTF(getAnswer(k1Val), "14", "16"));

                t.addCourseAvaliability(generateMWF(getAnswer(l1Val), "06", "08"));
                t.addCourseAvaliability(generateMWF(getAnswer(n1Val), "08", "10"));
                t.addCourseAvaliability(generateMWF(getAnswer(m1Val), "10", "12"));
                t.addCourseAvaliability(generateMWF(getAnswer(o1Val), "12", "14"));
                t.addCourseAvaliability(generateMWF(getAnswer(p1Val), "14", "16"));

                t.addCourseAvaliability(generateMTT(getAnswer(q1Val), "18", "15", "20", "45"));
                t.addCourseAvaliability(generateMTW(getAnswer(r1Val), "18", "30", "20", "30"));

                t.addCourseAvaliability(generateTTT(getAnswer(s1Val), "10", "12"));
                t.addCourseAvaliability(generateTTT(getAnswer(t1Val), "12", "14"));
                t.addCourseAvaliability(generateTTT(getAnswer(u1Val), "14", "16"));
                t.addCourseAvaliability(generateTTT(getAnswer(v1Val), "18", "30", "20", "30"));

                t.addCourseAvaliability(generateTTT(getAnswer(w1Val), "06", "09"));
                t.addCourseAvaliability(generateTTT(getAnswer(x1Val), "09", "12"));

                t.addCourseAvaliability(generateWF(getAnswer(y1Val), "06", "09"));

                t.addCourseAvaliability(generateWF(getAnswer(z1Val), "07", "00", "08", "30"));

                t.addCourseAvaliability(generateTTT(getAnswer(aa1Val), "12", "00", "13", "30"));
                t.addCourseAvaliability(generateTTT(getAnswer(ab1Val), "07", "00", "08", "30"));

                System.out.println(t);
            }

        }

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

    return null;
}

From source file:eafit.cdei.asignacion.input.ReadTeacherPreferences.java

public List<Teacher> loadTeacherPreferences() throws Exception {

    try {//  w  ww. j a va  2  s . c o  m
        FileInputStream fileInputStream = new FileInputStream("TeacherPreferences.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        HSSFSheet worksheet = workbook.getSheet("Preferences");

        boolean keepDoing = true;
        for (int i = 1; i < worksheet.getLastRowNum() + 1; i++) {

            HSSFRow row1 = worksheet.getRow(i);
            HSSFCell cellA1 = row1.getCell((short) 0);
            String a1Val = cellA1.getNumericCellValue() + "";
            HSSFCell cellB1 = row1.getCell((short) 1);
            String b1Val = cellB1.getStringCellValue();
            HSSFCell cellC1 = row1.getCell((short) 2);
            String c1Val = cellC1.getStringCellValue();

            String d1Val = "";

            HSSFCell cellD1 = row1.getCell((short) 3);

            if (cellD1 != null) {
                d1Val = cellD1.getStringCellValue();
            }

            HSSFCell cellE1 = row1.getCell((short) 4);
            double e1Val = cellE1.getNumericCellValue();

            if (a1Val == null || a1Val.length() == 0 || a1Val.equals("0")) {
                keepDoing = false;
            }

            Teacher t = new Teacher();
            t.setFullName(b1Val);

            t.setMaxCourses((int) e1Val);

            String[] preferedCourses = c1Val.split(",");

            ArrayList<LevelCourse> preferedCoursed = new ArrayList<LevelCourse>();

            for (String preferedCoursed1 : preferedCourses) {
                LevelCourse lc = new LevelCourse();
                lc.setLevelName(preferedCoursed1);
                preferedCoursed.add(lc);
            }

            String[] preferedTimes = d1Val.split(",");
            ArrayList<String> preferedTime = new ArrayList<>();

            for (String pf : preferedTimes) {

                preferedTime.add(pf);
            }

            t.setListPreferedCourses(preferedCoursed);

            System.out.println(t);

        }

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

    return null;
}

From source file:es.tena.foundation.util.POIUtil.java

public static String getCelda(HSSFCell cellSugg) {
    String suggestion = "";
    if (cellSugg != null) {
        if (cellSugg.getCellType() == HSSFCell.CELL_TYPE_STRING) {
            suggestion = StringUtil.trim(StringEscapeUtils.escapeSql(cellSugg.getStringCellValue()));
        } else if (cellSugg.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            BigDecimal big = new BigDecimal(cellSugg.getNumericCellValue());
            suggestion = big.toString();
        } // no hace falta else
        else {/*  ww w  .  j a  v a  2s.  c o  m*/
            suggestion = StringUtil.trim(StringEscapeUtils.escapeSql(cellSugg.getStringCellValue()));
        }
        suggestion = SQLUtil.replace(suggestion);
    }
    return suggestion;
}

From source file:excel.scanner.ExcelScanner.java

private void jButtonSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSearchActionPerformed
    if (file == null) {
        jLabelResults.setText("No File Found");
        return;//from   w  ww .j a  v a 2 s. c  o  m
    }
    HSSFRow row;
    HSSFCell cell;
    String selectedSheet;
    int index = 0;
    int resultCount = 0;
    String curCellText;

    try {
        jLabelResults.setText("");
        listModel.removeAllElements();
        for (int i = 0; i < sheet.getLastRowNum(); i++) {
            row = sheet.getRow(i);
            for (int j = 0; j < row.getLastCellNum(); j++) {
                cell = row.getCell(j);
                curCellText = cell.getStringCellValue();
                if (curCellText.contains(jTextFieldSearch.getText())) {
                    listModel.addElement(" " + CellReference.convertNumToColString(j) + (i + 1));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:file.open.util.parse.XlsParser.java

License:Open Source License

public String[] splitLine() throws Exception {
    if (m_iCurrentRow == m_iNbRows) {
        return null;
    }/*from w  ww  .j  av a 2 s. co  m*/

    HSSFRow row = m_sheet.getRow(m_iCurrentRow);
    if (row == null) {
        return null;
    } else {
        int cellIndex = 0;
        int noOfCells = row.getPhysicalNumberOfCells();
        String[] values = new String[noOfCells];
        short firstCellNum = row.getFirstCellNum();
        short lastCellNum = row.getLastCellNum();

        if (firstCellNum >= 0 && lastCellNum >= 0) {
            for (short iCurrent = firstCellNum; iCurrent < lastCellNum; iCurrent++) {
                HSSFCell cell = (HSSFCell) row.getCell((int) iCurrent);
                if (cell == null) {
                    values[iCurrent] = "";
                    cellIndex++;
                    continue;
                } else {
                    switch (cell.getCellType()) {
                    case HSSFCell.CELL_TYPE_NUMERIC:
                        double value = cell.getNumericCellValue();
                        if (HSSFDateUtil.isCellDateFormatted(cell)) {
                            if (HSSFDateUtil.isValidExcelDate(value)) {
                                Date date = HSSFDateUtil.getJavaDate(value);
                                SimpleDateFormat dateFormat = new SimpleDateFormat(JAVA_TOSTRING);
                                values[iCurrent] = dateFormat.format(date);
                            } else {
                                throw new Exception("Invalid Date value found at row number " + row.getRowNum()
                                        + " and column number " + cell.getNumericCellValue());
                            }
                        } else {
                            values[iCurrent] = value + "";
                        }
                        break;

                    case HSSFCell.CELL_TYPE_STRING:
                        values[iCurrent] = cell.getStringCellValue();
                        break;

                    case HSSFCell.CELL_TYPE_BLANK:
                        values[iCurrent] = null;
                        break;

                    default:
                        values[iCurrent] = null;
                    }
                }
            }
        }
        m_iCurrentRow++;
        return values;
    }

}