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.bund.bfr.knime.openkrise.db.imports.GeneralXLSImporter.java

License:Open Source License

private Long manageBigInteger(PreparedStatement ps, PreparedStatement psUpdate, int lfdCol, HSSFCell cell)
        throws SQLException {
    Long result = null;//from  w ww.  ja va  2 s. c o  m
    if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
    } else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
        if (cell.getStringCellValue().trim().length() > 0) {
            result = new Long(cell.getStringCellValue());
            if (ps != null)
                ps.setLong(lfdCol, result);
            if (psUpdate != null)
                psUpdate.setLong(lfdCol, result);
            return result;
        }
    } else {
        result = new Long((long) cell.getNumericCellValue());
        if (ps != null)
            ps.setLong(lfdCol, result);
        if (psUpdate != null)
            psUpdate.setLong(lfdCol, result);
        return result;
    }
    if (ps != null)
        ps.setNull(lfdCol, java.sql.Types.BIGINT);
    if (psUpdate != null)
        psUpdate.setNull(lfdCol, java.sql.Types.BIGINT);
    return result;
}

From source file:de.bund.bfr.knime.openkrise.db.imports.GeneralXLSImporter.java

License:Open Source License

private Double manageDouble(PreparedStatement ps, PreparedStatement psUpdate, int lfdCol, HSSFCell cell)
        throws SQLException {
    Double dbl = null;//from   w  w w  . j  av a  2 s .c om
    if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
    } else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
        if (cell.getStringCellValue().trim().length() > 0
                && !cell.getStringCellValue().equalsIgnoreCase("null")) {
            try {
                dbl = Double.parseDouble(cell.getStringCellValue());
                ps.setDouble(lfdCol, dbl);
                psUpdate.setDouble(lfdCol, dbl);
                return dbl;
            } catch (Exception e) {
            }
        }
    } else {
        dbl = cell.getNumericCellValue();
        try {
            ps.setDouble(lfdCol, dbl);
            psUpdate.setDouble(lfdCol, dbl);
        } catch (Exception e) {
        }
        return dbl;
    }
    try {
        ps.setNull(lfdCol, java.sql.Types.DOUBLE);
        psUpdate.setNull(lfdCol, java.sql.Types.DOUBLE);
    } catch (Exception e) {
    }
    return dbl;
}

From source file:de.bund.bfr.knime.openkrise.db.imports.GeneralXLSImporter.java

License:Open Source License

private String manageString(PreparedStatement ps, PreparedStatement psUpdate, int lfdCol, HSSFCell cell,
        LinkedHashMap<Object, String> hashBL) throws SQLException {
    String result = null;// w  w  w  .  j a va  2  s  .c o m
    if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
        if (ps != null)
            ps.setNull(lfdCol, java.sql.Types.VARCHAR);
        if (psUpdate != null)
            psUpdate.setNull(lfdCol, java.sql.Types.VARCHAR);
    } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        result = cell.getStringCellValue().trim();
        if (ps != null)
            ps.setString(lfdCol, result);
        if (psUpdate != null)
            psUpdate.setString(lfdCol, result);
    } else {
        result = cell.getStringCellValue().trim();
        if (hashBL != null) {
            int val, min = 1000;
            String newResult = result;
            for (Object o : hashBL.keySet()) {
                val = Levenshtein.LD(result, o.toString());
                if (val < min) {
                    min = val;
                    newResult = o.toString();
                }
            }
            if (!newResult.equals(result)) {
                if (DBKernel.debug)
                    MyLogger.handleMessage("Levenshtein - not equal ... " + newResult + "\t" + result);
                result = newResult;
            }
        }
        if (ps != null)
            ps.setString(lfdCol, result);
        if (psUpdate != null)
            psUpdate.setString(lfdCol, result);
    }
    //ps.setNull(lfdCol, java.sql.Types.VARCHAR);      
    if (result != null && result.equals("?@lufa-itl.de")) {
        MyLogger.handleMessage(result);
    }
    return result;
}

From source file:de.bund.bfr.knime.openkrise.db.imports.GeneralXLSImporter.java

License:Open Source License

private Boolean manageBoolean(PreparedStatement ps, PreparedStatement psUpdate, int lfdCol, HSSFCell cell)
        throws SQLException {
    Boolean result = null;/* w  w  w . j a va2s . c om*/
    if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
        if (ps != null)
            ps.setNull(lfdCol, java.sql.Types.BOOLEAN);
        if (psUpdate != null)
            psUpdate.setNull(lfdCol, java.sql.Types.BOOLEAN);
    } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
        result = cell.getNumericCellValue() != 0;
        if (ps != null)
            ps.setBoolean(lfdCol, result);
        if (psUpdate != null)
            psUpdate.setBoolean(lfdCol, result);
    } else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
        result = cell.getStringCellValue().equalsIgnoreCase("true");
        if (ps != null)
            ps.setBoolean(lfdCol, result);
        if (psUpdate != null)
            psUpdate.setBoolean(lfdCol, result);
    } else {
        result = cell.getBooleanCellValue();
        if (ps != null)
            ps.setBoolean(lfdCol, result);
        if (psUpdate != null)
            psUpdate.setBoolean(lfdCol, result);
    }
    //ps.setNull(lfdCol, java.sql.Types.BOOLEAN);      
    return result;
}

From source file:de.cenote.jasperstarter.ReportNGTest.java

License:Apache License

/**
 * Test of exportXls method, of class Report.
 */// w  w  w. j  a  v a  2s .co  m
@Test(dependsOnMethods = { "testFill" })
public void testExportXls() throws Exception {
    System.out.println("exportXls");
    Config config = null;
    config = new Config();
    config.input = "target/test-classes/reports/compileToFile.jrprint";
    Report instance = new Report(config, new File(config.getInput()));
    instance.exportXls();
    assertEquals(((File) new File("target/test-classes/reports/compileToFile.xls")).exists(), true);
    // Read the content of a cell:
    InputStream inputStream = new FileInputStream("target/test-classes/reports/compileToFile.xls");
    HSSFWorkbook wb = new HSSFWorkbook(inputStream);
    HSSFSheet sheet = wb.getSheetAt(0); // first sheet
    // select cell C12
    HSSFRow row = sheet.getRow(11);
    HSSFCell cell = row.getCell(2);
    assertEquals(cell.getStringCellValue(), "Carl Grant");
}

From source file:de.cenote.jasperstarter.ReportNGTest.java

License:Apache License

/**
 * Test of exportXlsMeta method, of class Report.
 *//*w  w w. j a va  2  s. c om*/
@Test(dependsOnMethods = { "testFillMeta" })
public void testExportXlsMeta() throws Exception {
    System.out.println("exportXlsMeta");
    Config config = null;
    config = new Config();
    config.input = "target/test-classes/reports/csvMeta.jrprint";
    Report instance = new Report(config, new File(config.getInput()));
    instance.exportXlsMeta();
    assertEquals(((File) new File("target/test-classes/reports/csvMeta.xls")).exists(), true);
    // Read the content of a cell:
    InputStream inputStream = new FileInputStream("target/test-classes/reports/csvMeta.xls");
    HSSFWorkbook wb = new HSSFWorkbook(inputStream);
    HSSFSheet sheet = wb.getSheetAt(0); // first sheet
    // select cell C12
    HSSFRow row = sheet.getRow(11);
    HSSFCell cell = row.getCell(2);
    assertEquals(cell.getStringCellValue(), "Dampremy");
}

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

License:Open Source License

/**
 * /*from  w ww .j a va  2s. c o  m*/
 * @param row
 * @param cellIdx
 * @return
 * @throws IllegalArgumentException
 */
public Boolean readCellAsBoolean(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);
    }

    Boolean result;
    switch (cellType) {
    case HSSFCell.CELL_TYPE_BOOLEAN:
        result = cell.getBooleanCellValue();
        break;
    case HSSFCell.CELL_TYPE_NUMERIC:
        result = cell.getNumericCellValue() == 1;
        break;
    case HSSFCell.CELL_TYPE_STRING:
        String stringCellValue = cell.getStringCellValue();
        result = "1".equalsIgnoreCase(stringCellValue) || "true".equalsIgnoreCase(stringCellValue)
                || "yes".equalsIgnoreCase(stringCellValue);
        break;
    default:
        result = null;
        break;
    }

    return result;
}

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

License:Open Source License

/**
 * //from  w w w  .j a  va  2 s .  c  o m
 * @param row
 * @param cellIdx
 * @return
 * @throws IllegalArgumentException
 */
public Double readCellAsDouble(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);
    }

    Double result;
    switch (cellType) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        double numericCellValue = cell.getNumericCellValue();
        result = Double.valueOf(numericCellValue);
        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);
        break;
    default:
        result = Double.MIN_VALUE;
        break;
    }

    return result;
}

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

License:Open Source License

/**
 * //ww  w.j  a  v a  2  s.  c o  m
 * @param row
 * @param cellIdx
 * @return
 * @throws IllegalArgumentException
 */
public Float readCellAsFloat(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);
    }

    Float result;
    switch (cellType) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        double numericCellValue = cell.getNumericCellValue();
        if (numericCellValue > Float.MAX_VALUE) {
            throw new IllegalArgumentException("Value " + numericCellValue + " to big for integer!");
        }
        result = Double.valueOf(numericCellValue).floatValue();
        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).floatValue();
        break;
    default:
        result = Float.MIN_VALUE;
        break;
    }

    return result;
}

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

License:Open Source License

/**
 * /*from  w w  w . j av a2  s.com*/
 * @param row
 * @param cellIdx
 * @return
 * @throws IllegalArgumentException
 */
public Integer readCellAsInt(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);
    }

    Integer result;
    switch (cellType) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        double numericCellValue = cell.getNumericCellValue();
        if (numericCellValue > Integer.MAX_VALUE) {
            throw new IllegalArgumentException("Value " + numericCellValue + " to big for integer!");
        }
        result = Double.valueOf(numericCellValue).intValue();
        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).intValue();
        break;
    default:
        result = Integer.MIN_VALUE;
        break;
    }

    return result;
}