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:org.egov.egf.web.actions.brs.AutoReconcileHelper.java

License:Open Source License

private BigDecimal getNumericValue(final HSSFCell cell) {
    if (cell == null)
        return null;
    double numericCellValue = 0d;
    BigDecimal bigDecimalValue = BigDecimal.ZERO;
    String strValue = "";

    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_NUMERIC:
        numericCellValue = cell.getNumericCellValue();
        bigDecimalValue = BigDecimal.valueOf(numericCellValue);
        break;// w ww.  j a va  2  s  . c o  m
    case HSSFCell.CELL_TYPE_STRING:
        strValue = cell.getStringCellValue();
        strValue = strValue.replaceAll("[^\\p{L}\\p{Nd}]", "");
        if (strValue != null && strValue.contains("E+")) {
            final String[] split = strValue.split("E+");
            String mantissa = split[0].replaceAll(".", "");
            final int exp = Integer.parseInt(split[1]);
            while (mantissa.length() <= exp + 1)
                mantissa += "0";
            numericCellValue = Double.parseDouble(mantissa);
            bigDecimalValue = BigDecimal.valueOf(numericCellValue);
        } else if (strValue != null && strValue.contains(","))
            strValue = strValue.replaceAll(",", "");
        // Ignore the error and continue Since in numric field we find empty or non numeric value
        try {
            numericCellValue = Double.parseDouble(strValue);
            bigDecimalValue = BigDecimal.valueOf(numericCellValue);
        } catch (final Exception e) {
            if (LOGGER.isDebugEnabled())
                LOGGER.debug("Found : Non numeric value in Numeric Field :" + strValue + ":");
        }
        break;
    }
    return bigDecimalValue;

}

From source file:org.everit.i18n.propsxlsconverter.internal.workbook.WorkbookReader.java

License:Apache License

/**
 * Constructor.//  ww  w  .j av  a2  s  .c om
 *
 * @param xlsFileName
 *          the file name of the xls to read
 */
public WorkbookReader(final String xlsFileName) {
    super(xlsFileName);

    HSSFRow firstRow = sheet.getRow(rowNumber++);

    int columnNumber = COLUMN_DEFAULT_LANG + 1;
    HSSFCell cell = null;
    while (((cell = firstRow.getCell(columnNumber)) != null)) {
        if (!cell.getStringCellValue().trim().isEmpty()) {
            String lang = cell.getStringCellValue();
            langColumnNumber.put(lang, columnNumber);
            columnNumber++;
        }
    }

}

From source file:org.everit.i18n.propsxlsconverter.internal.workbook.WorkbookReader.java

License:Apache License

/**
 * Get next row in the sheet. Read rows between second to last row.
 *
 * @return the {@link WorkbookRowDTO}./* ww  w.  j a  v a 2 s  .co  m*/
 */
public WorkbookRowDTO getNextRow() {
    if (sheet == null) {
        throw new RuntimeException("Not opened workbook yet.");
    }

    HSSFRow row = sheet.getRow(rowNumber++);
    HSSFCell propertiesFileNameCell = row.getCell(COLUMN_PROPERTIES_FILE_NAME);
    String propertiesFileName = propertiesFileNameCell.getStringCellValue();

    HSSFCell propKeyCell = row.getCell(COLUMN_PROPERTY_KEY);
    HSSFCell defaultLangCell = row.getCell(COLUMN_DEFAULT_LANG);
    HashMap<String, String> langValues = new HashMap<String, String>();

    langColumnNumber.forEach((key, value) -> {
        HSSFCell langCell = row.getCell(value);
        String langValue = langCell == null ? "" : langCell.getStringCellValue();
        langValues.put(key, langValue);
    });

    return new WorkbookRowDTO().propertiesFile(propertiesFileName).propKey(propKeyCell.getStringCellValue())
            .defaultLangValue(defaultLangCell.getStringCellValue()).langValues(langValues);
}

From source file:org.exoplatform.addon.pulse.service.ws.RestActivitiesStatistic.java

License:Open Source License

private String buildCsvContent(HSSFWorkbook workbook) {
    HSSFSheet sheet = workbook.getSheetAt(0);
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i <= sheet.getLastRowNum(); i++) {
        HSSFRow row = sheet.getRow(i);//  w  w w . ja  v a2 s. c  o  m
        for (int j = 0; j < row.getLastCellNum(); j++) {
            HSSFCell cell = row.getCell(j);
            int cellType = cell.getCellType();
            if (cellType == HSSFCell.CELL_TYPE_STRING) {
                buffer.append(cell.getStringCellValue());
            } else if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
                buffer.append(new DecimalFormat("#").format(cell.getNumericCellValue()));
            }
            if (j < row.getLastCellNum() - 1) {
                buffer.append(',');
            }
        }
        buffer.append('\n');
    }
    return buffer.toString();
}

From source file:org.exoplatform.services.document.impl.MSExcelDocumentReader.java

License:Open Source License

/**
 * Returns only a text from .xls file content.
 * /*w w  w  . j a  v  a 2 s .  com*/
 * @param is an input stream with .xls file content.
 * @return The string only with text from file content.
 */
public String getContentAsText(InputStream is) throws IOException, DocumentReadException {
    if (is == null) {
        throw new IllegalArgumentException("InputStream is null.");
    }

    final StringBuilder builder = new StringBuilder("");

    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);

    try {
        if (is.available() == 0) {
            return "";
        }

        HSSFWorkbook wb;
        try {
            wb = new HSSFWorkbook(is);
        } catch (IOException e) {
            throw new DocumentReadException("Can't open spreadsheet.", e);
        }
        for (int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
            HSSFSheet sheet = wb.getSheetAt(sheetNum);
            if (sheet != null) {
                for (int rowNum = sheet.getFirstRowNum(); rowNum <= sheet.getLastRowNum(); rowNum++) {
                    HSSFRow row = sheet.getRow(rowNum);

                    if (row != null) {
                        int lastcell = row.getLastCellNum();
                        for (int k = 0; k < lastcell; k++) {
                            final HSSFCell cell = row.getCell((short) k);
                            if (cell != null) {
                                switch (cell.getCellType()) {
                                case HSSFCell.CELL_TYPE_NUMERIC: {
                                    double d = cell.getNumericCellValue();
                                    if (isCellDateFormatted(cell)) {
                                        Date date = HSSFDateUtil.getJavaDate(d);
                                        String cellText = dateFormat.format(date);
                                        builder.append(cellText).append(" ");
                                    } else {
                                        builder.append(d).append(" ");
                                    }
                                    break;
                                }
                                case HSSFCell.CELL_TYPE_FORMULA:
                                    SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>() {
                                        public Void run() {
                                            builder.append(cell.getCellFormula().toString()).append(" ");
                                            return null;
                                        }
                                    });
                                    break;
                                case HSSFCell.CELL_TYPE_BOOLEAN:
                                    SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>() {
                                        public Void run() {
                                            builder.append(cell.getBooleanCellValue()).append(" ");
                                            return null;
                                        }
                                    });
                                    break;
                                case HSSFCell.CELL_TYPE_ERROR:
                                    SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>() {
                                        public Void run() {
                                            builder.append(cell.getErrorCellValue()).append(" ");
                                            return null;
                                        }
                                    });
                                    break;
                                case HSSFCell.CELL_TYPE_STRING:
                                    SecurityHelper.doPrivilegedAction(new PrivilegedAction<Void>() {
                                        public Void run() {
                                            builder.append(cell.getStringCellValue().toString()).append(" ");
                                            return null;
                                        }
                                    });
                                    break;
                                default:
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("An exception occurred: " + e.getMessage());
                }
            }
        }
    }
    return builder.toString();
}

From source file:org.gageot.excel.core.ObjectCellMapper.java

License:Apache License

@Override
public Object mapCell(HSSFCell cell, int rowNum, int columnNum) throws IOException {
    try {//from   w w w. ja  v  a2s. c om
        return cell.getNumericCellValue();
    } catch (NumberFormatException e) {
        return cell.getStringCellValue();
    } catch (IllegalStateException e) {
        return cell.getStringCellValue();
    }
}

From source file:org.gageot.excel.core.StringCellMapper.java

License:Apache License

private String richTextToString(HSSFCell cell) {
    return cell.getStringCellValue();
}

From source file:org.gaixie.micrite.car.service.impl.DealWithCar.java

final private void save3(HSSFRow row) {
    HSSFCell cell = null;
    Carfile carfile = new Carfile();

    if (makeCarowner(row, carfile))
        return;//from  www. j  ava 2s.c  o m

    cell = row.getCell(5);// ?
    licenseNumber = cell.getStringCellValue();

    if (carfileService.existCar(licenseNumber, paiSeTmp)) {
        if (log.isInfoEnabled()) {
            log.info("return for existCar:" + licenseNumber + "," + paiSeTmp.getName());
        }
        return;
    }

    carfile.setPaiSe(paiSeTmp);// 
    carfile.setLicenseNumber(licenseNumber);// ?
    cell = row.getCell(6);
    carfile.setCarType(DictionaryUtil.getDictionary(cell.getStringCellValue(), carType));
    cell = row.getCell(7);
    carfile.setLicenseType(DictionaryUtil.getDictionary(cell.getStringCellValue(), licenseType));
    cell = row.getCell(8);
    carfile.setLoadTon(cell.getStringCellValue());
    cell = row.getCell(13);
    carfile.setBrandType(cell.getStringCellValue());
    cell = row.getCell(14);
    carfile.setYingyunNo(cell.getStringCellValue());
    cell = row.getCell(25);
    carfile.setCarRemark(cell.getStringCellValue());
    cell = row.getCell(27);
    carfile.setFuelRank(DictionaryUtil.getDictionary(cell.getStringCellValue(), fuelRank));
    cell = row.getCell(28);
    carfile.setSkillRank(DictionaryUtil.getDictionary(cell.getStringCellValue(), skillRank));
    cell = row.getCell(29);
    if (cell.getStringCellValue() != null && !cell.getStringCellValue().equals("")) {
        try {
            carfile.setEvaluateDate(df.parse(cell.getStringCellValue()));
        } catch (Exception e) {
        } // {log.info(e+":cell("+i+",29).getStringCellValue()="+cell.getStringCellValue());}
    } // else{log.info(":cell("+i+",29).getStringCellValue()==null");}
    cell = row.getCell(30);
    carfile.setEvaluateCycle(DictionaryUtil.getDictionary(cell.getStringCellValue(), evaluateCycle));
    cell = row.getCell(31);
    if (cell.getStringCellValue() != null && !cell.getStringCellValue().equals("")) {
        try {
            carfile.setMaintainDate(df.parse(cell.getStringCellValue()));// new
            // SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S")
        } catch (Exception e) {
        } // {log.info(e+":cell("+i+",31).getStringCellValue()="+cell.getStringCellValue());}
    } // else{log.info(":cell("+i+",31).getStringCellValue()==null");}
    cell = row.getCell(32);
    carfile.setMaintainCycle(DictionaryUtil.getDictionary(cell.getStringCellValue(), maintainCycle));
    try {
        carfileService.addFromUpload(carfile);
    } catch (Exception e) {
        log.info("when save yunyingNo=" + carfile.getYingyunNo() + "," + carfile.getLicenseNumber() + ","
                + carfile.getPaiSe().getName() + ":" + e);
    }
}

From source file:org.gaixie.micrite.car.service.impl.DealWithCar.java

final private void save1_2(HSSFRow row) {

    HSSFCell cell = null;
    Carfile carfile = new Carfile();

    if (makeCarowner(row, carfile)) {
        return;//  w  w  w .ja v a  2 s .c om
    }

    cell = row.getCell(5);// ?
    licenseNumber = cell.getStringCellValue();
    cell = row.getCell(PAISE_COL_1_2);// 
    paiSeTmp = DictionaryUtil.getDictionaryWhenNotGiveLast(cell.getStringCellValue(), paiSe);

    if (carfileService.existCar(licenseNumber, paiSeTmp)) {
        if (log.isInfoEnabled()) {
            log.info("return for existCar:" + licenseNumber + "," + paiSeTmp.getName());
        }
        return;
    }
    carfile.setPaiSe(paiSeTmp);// 
    carfile.setLicenseNumber(licenseNumber);// ?
    cell = row.getCell(6);
    carfile.setCarType(DictionaryUtil.getDictionary(cell.getStringCellValue(), carType));
    cell = row.getCell(7);
    carfile.setLicenseType(DictionaryUtil.getDictionary(cell.getStringCellValue(), licenseType));
    cell = row.getCell(8);
    carfile.setLoadTon(cell.getStringCellValue());
    cell = row.getCell(13);
    carfile.setBrandType(cell.getStringCellValue());
    cell = row.getCell(14);
    carfile.setYingyunNo(cell.getStringCellValue());
    cell = row.getCell(22);
    carfile.setCarRemark(cell.getStringCellValue());
    cell = row.getCell(24);
    carfile.setFuelRank(DictionaryUtil.getDictionary(cell.getStringCellValue(), fuelRank));
    cell = row.getCell(25);
    carfile.setSkillRank(DictionaryUtil.getDictionary(cell.getStringCellValue(), skillRank));
    cell = row.getCell(26);
    if (cell.getStringCellValue() != null && !cell.getStringCellValue().equals("")) {
        try {
            carfile.setEvaluateDate(df.parse(cell.getStringCellValue()));
        } catch (Exception e) {
        } // {log.info(e+":cell("+i+",26).getStringCellValue()="+cell.getStringCellValue());}
    } // else{log.info(":cell("+i+",26).getStringCellValue()==null");}
    cell = row.getCell(27);
    carfile.setEvaluateCycle(DictionaryUtil.getDictionary(cell.getStringCellValue(), evaluateCycle));
    cell = row.getCell(28);
    if (cell.getStringCellValue() != null && !cell.getStringCellValue().equals("")) {
        try {
            carfile.setMaintainDate(df.parse(cell.getStringCellValue()));
        } catch (Exception e) {
        } // {log.info(e+":cell("+i+",28).getStringCellValue()="+cell.getStringCellValue());}
    } // else{log.info(":cell("+i+",28).getStringCellValue()==null");}
    cell = row.getCell(29);
    carfile.setMaintainCycle(DictionaryUtil.getDictionary(cell.getStringCellValue(), maintainCycle));
    try {
        carfileService.addFromUpload(carfile);
    } catch (Exception e) {
        log.info("when save yunyingNo=" + carfile.getYingyunNo() + "," + carfile.getLicenseNumber() + ","
                + carfile.getPaiSe().getName() + ":" + e);
    }
}

From source file:org.gaixie.micrite.car.service.impl.DealWithCar.java

final boolean makeCarowner(HSSFRow row, Carfile carfile) {
    HSSFCell cell = null;
    cell = row.getCell(2);// ?????
    String ownerLicense = cell.getStringCellValue().trim();
    if (ownerLicense.equals("")) {
        if (log.isInfoEnabled()) {
            log.info("return for makeCarowner==true:yingyunNo=" + ownerLicense + ",column0:" + row.getCell(0));
        }/*www  .  j a  v a2 s .c  o m*/
        return true;
    }
    Carowner carowner = carownerService.getByLicense(ownerLicense);
    if (carowner != null) {
        carfile.setCarowner(carowner);
    } else {
        cell = row.getCell(1);// ??
        carfile.setOwner(cell.getStringCellValue().trim());
        carfile.setOwnerLicense(ownerLicense);
        cell = row.getCell(3);// ?
        carfile.setTelephone(cell.getStringCellValue());
        cell = row.getCell(4);// ?
        carfile.setAddress(cell.getStringCellValue().trim());
        try {
            carownerService.save(carfile.getCarowner());
        } catch (Exception e) {
            // TODO: handle exception
            log.info("save carowner:" + e);
        }

    }
    return false;
}