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:com.yqboots.initializer.core.builder.excel.factory.DomainMetadataFactory.java

License:Apache License

/**
 * Gets the properties.// w  w  w  .j a  va  2  s .c  om
 *
 * @param row the row of one sheet in the Excel
 * @return one DomainMetadataProperty
 */
private static DomainMetadataProperty getMetadataProperty(final Row row) {
    final DomainMetadataProperty result = new DomainMetadataProperty();

    Cell cell = row.getCell(3);
    if (cell != null) {
        result.setDbColumn(cell.getStringCellValue());
    }
    cell = row.getCell(4);
    if (cell != null) {
        result.setClassField(cell.getStringCellValue());
    }
    cell = row.getCell(5);
    if (cell != null) {
        result.setFieldType(cell.getStringCellValue());
    }
    cell = row.getCell(6);
    if (cell != null && cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        result.setNullable(cell.getBooleanCellValue());
    }
    cell = row.getCell(7);
    if (cell != null && cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        result.setUnique(cell.getBooleanCellValue());
    }
    cell = row.getCell(8);
    if (cell != null) {
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            result.setDefaultValue(Double.toString(cell.getNumericCellValue()));
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            result.setDefaultValue(Boolean.toString(cell.getBooleanCellValue()));
            break;
        default:
            result.setDefaultValue(cell.getStringCellValue());
        }
    }

    return result;
}

From source file:com.yqboots.initializer.core.builder.excel.MenuItemSheetBuilder.java

License:Apache License

@Override
protected void formatChecking(final Sheet sheet) {
    // get the title row
    Row row = sheet.getRow(0);//from  ww w.  j  a  v a 2  s  .c om

    Cell nameCell = row.getCell(0);
    Cell urlCell = row.getCell(1);
    Cell menuGroupCell = row.getCell(2);
    Cell menuItemGroupCell = row.getCell(3);

    Assert.isTrue(StringUtils.equalsIgnoreCase(nameCell.getStringCellValue(), "name"),
            "Column 'name' is required");
    Assert.isTrue(StringUtils.equalsIgnoreCase(urlCell.getStringCellValue(), "url"),
            "Column 'URL' is required");
    Assert.isTrue(StringUtils.equalsIgnoreCase(menuGroupCell.getStringCellValue(), "Menu Group"),
            "Column 'Menu Group' is required");
    Assert.isTrue(StringUtils.equalsIgnoreCase(menuItemGroupCell.getStringCellValue(), "Menu Item Group"),
            "Column 'Menu Item Group' is required");
}

From source file:com.yqboots.initializer.core.builder.excel.MessageSheetBuilder.java

License:Apache License

@Override
protected void formatChecking(final Sheet sheet) {
    // get the title row
    Row row = sheet.getRow(0);// w ww .  j a  va2 s . c  om
    Cell keyCell = row.getCell(0);
    Assert.isTrue(StringUtils.equalsIgnoreCase(keyCell.getStringCellValue(), "key"),
            "Column 'key' is required");

    Cell langCell = row.getCell(1);
    Assert.isTrue(StringUtils.isNotBlank(langCell.getStringCellValue()), "one language is required at least");
}

From source file:com.yqboots.initializer.core.builder.excel.MessageSheetBuilder.java

License:Apache License

/**
 * Gets the languages from the specified row.
 *
 * @param row the row of a sheet on Excel
 * @return list of languages/*  w w w.ja  va 2  s. c  o  m*/
 */
private List<String> getLanguages(Row row) {
    final List<String> results = new ArrayList<>();
    for (Cell cell : row) {
        if (cell.getColumnIndex() < 1) {
            continue;
        }

        results.add(cell.getStringCellValue());
    }

    return results;
}

From source file:com.yqboots.initializer.core.builder.excel.SystemPropertiesSheetBuilder.java

License:Apache License

@Override
protected void formatChecking(final Sheet sheet) {
    // get the title row
    final Row row = sheet.getRow(0);
    final Cell keyCell = row.getCell(0);
    Assert.isTrue(StringUtils.equalsIgnoreCase(keyCell.getStringCellValue(), "Name"),
            "Column 'Name' is required");
    final Cell valueCell = row.getCell(1);
    Assert.isTrue(StringUtils.equalsIgnoreCase(valueCell.getStringCellValue(), "Profile"),
            "Column 'Profile' is required");
}

From source file:com.yqboots.initializer.core.builder.excel.SystemPropertiesSheetBuilder.java

License:Apache License

@Override
protected void doBuild(final Path root, final ProjectMetadata metadata, final Sheet sheet) throws IOException {
    final List<String> profiles = getProfiles(sheet.getRow(1));
    for (int i = 1; i <= profiles.size(); i++) {
        String profile = profiles.get(i - 1);
        String fileName = FILE_NAME_PREFIX + FileType.DOT_PROPERTIES;
        if (!PROFILE_DEFAULT.equals(profile)) {
            fileName = FILE_NAME_PREFIX + "-" + profile + FileType.DOT_PROPERTIES;
        }/*from   w w w .  jav  a 2s. co  m*/

        final Path targetPath = Paths.get(root + File.separator + properties.getExportRelativePath());
        if (!Files.exists(targetPath)) {
            Files.createDirectories(targetPath);
        }
        try (final FileWriter writer = new FileWriter(
                Paths.get(targetPath + File.separator + fileName).toFile())) {
            for (final Row row : sheet) {
                // ignore the first and second rows
                if (row.getRowNum() < 2) {
                    continue;
                }

                // set comment
                final Cell cell = row.getCell(3);
                if (cell != null) {
                    writer.write("#" + cell.getStringCellValue() + "\r\n");
                }

                final Cell cell0 = row.getCell(0);
                final Cell celli = row.getCell(i);
                if (cell0 != null && celli != null) {
                    writer.write(cell0.getStringCellValue() + "=" + celli.getStringCellValue() + "\r\n");
                }
            }
        }
    }
}

From source file:com.yqboots.initializer.core.builder.excel.SystemPropertiesSheetBuilder.java

License:Apache License

private static List<String> getProfiles(Row row) {
    final List<String> results = new ArrayList<>();
    for (final Cell cell : row) {
        if (cell.getColumnIndex() == 0) {
            continue;
        }/*from   ww  w. ja v a  2 s  .  c om*/

        final String profile = cell.getStringCellValue();
        if (StringUtils.isBlank(profile)) {
            break;
        }

        results.add(profile);
    }

    Assert.isTrue(!results.isEmpty());
    return results;
}

From source file:com.zlfun.framework.excel.ExcelUtils.java

private static <T> Map<String, String> genRowMap(Row row, List<String> cols, FormulaEvaluator evaluator) {
    Map<String, String> map = new HashMap<String, String>();
    int j = 0;/*  w ww. j av  a2s  . c  o m*/
    for (String col : cols) {
        Cell cell = row.getCell(j);// ??
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_BOOLEAN:
            map.put(col, String.valueOf(cell.getBooleanCellValue()));
            break;
        case Cell.CELL_TYPE_NUMERIC:
            map.put(col, String.valueOf(cell.getNumericCellValue()));

            break;
        case Cell.CELL_TYPE_STRING:

            map.put(col, String.valueOf(cell.getStringCellValue()));
            break;
        case Cell.CELL_TYPE_BLANK:
            map.put(col, String.valueOf(""));

            break;
        case Cell.CELL_TYPE_ERROR:
            map.put(col, String.valueOf("error"));
            break;

        // CELL_TYPE_FORMULA will never occur  
        case Cell.CELL_TYPE_FORMULA:

            evaluator.evaluateFormulaCell(cell);
            map.put(col, String.valueOf(cell.getNumericCellValue()));
            break;

        }
        j++;
    }

    return map;
}

From source file:com.zlfun.framework.excel.ExcelUtils.java

private static <T> void fill(Class<T> clazz, List<T> result, String fileName, InputStream is) {

    try {/*from w  w w . j a v a2 s  . co  m*/
        Workbook excel = null;
        if (fileName.indexOf(".xlsx") > 0) {
            excel = new XSSFWorkbook(is);// Excel2007
        } else if (fileName.indexOf(".xls") > 0) {
            excel = new HSSFWorkbook(is);// Excel2003

        } else {
            return;
        }
        FormulaEvaluator evaluator = excel.getCreationHelper().createFormulaEvaluator();
        Sheet sheet = excel.getSheetAt(0);// ?0
        // ????1
        List<String> header = new ArrayList<String>();
        if (sheet.getLastRowNum() >= 0) {
            Row row = sheet.getRow(0);// ?

            for (int i = 0; i < row.getLastCellNum(); i++) {
                Cell cell = row.getCell(i);// ??

                if (cell != null) {// ?cellStr

                    header.add(cell.getStringCellValue());
                }

            }

        }

        //
        if (sheet.getLastRowNum() > 1) {

            for (int i = 1; i <= sheet.getLastRowNum(); i++) {

                Row row = sheet.getRow(i);// ?
                if (row == null) {// ??
                    continue;
                }
                Map<String, String> map = genRowMap(row, header, evaluator);
                T t = fill(map, clazz.newInstance());
                result.add(t);
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(ExcelUtils.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        Logger.getLogger(ExcelUtils.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        Logger.getLogger(ExcelUtils.class.getName()).log(Level.SEVERE, null, ex);
    } finally {// ?
        if (is != null) {
            try {
                is.close();

            } catch (IOException ex) {
                Logger.getLogger(ExcelUtils.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    return;
}

From source file:com.znsx.cms.service.impl.DeviceManagerImpl.java

private String isNameExist(List<String> dvrs, int rowIndex, Row row) {
    Cell cell = row.getCell(0);
    String name = "";
    if (cell == null) {
        throw new BusinessException(ErrorCode.EXCEL_CONTENT_ERROR,
                "excel row:" + (rowIndex + 1) + ",cellIndex:" + 1 + "," + TypeDefinition.DVR_TEMPLATE + ","
                        + TypeDefinition.PARAMETER_NULL + ",dvrName is not null");

    } else {/*from w w  w  . j a  v  a  2  s  .  c o m*/
        row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
        name = cell.getStringCellValue();
        if (dvrs.contains(name)) {
            throw new BusinessException(ErrorCode.EXCEL_CONTENT_ERROR,
                    "excel row:" + (rowIndex + 1) + ",cellIndex:" + 1 + "," + TypeDefinition.DVR_TEMPLATE + ","
                            + TypeDefinition.DVR_NAME_EXIST + ",name[" + name + "] is already exist !");
        }
    }
    return name;
}