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.photon.phresco.service.tools.TechnologyDataGenerator.java

License:Apache License

/**
 * Handle dependencies.//from   w ww .  j av  a  2  s  . c o m
 *
 * @param modules
 *            the modules
 * @return the modules
 */
//   private Modules handleDependencies(Modules modules) {
//      Modules modifiedModules = new Modules();
//      List<Module> moduleList = modules.getModule();
//      for (Module module : moduleList) {
//         List<String> depModNameList = new ArrayList<String>();
//         String modName = module.getId();
//         String[] depList = NAME_AND_DEP_MAP.get(modName);
//         if (depList == null) {
//            modifiedModules.getModule().add(module);
//            continue;
//         }
//         for (String dep : depList) {
//            int depNo = (int) (Double.valueOf(dep).doubleValue());
//            String depModName = SNO_AND_NAME_MAP.get(String.valueOf(depNo));
//            depModNameList.add(depModName);
//         }
//
//         if (!depModNameList.isEmpty()) {
//            module.getDependentModules().addAll(depModNameList);
//         }
//
//         modifiedModules.getModule().add(module);
//      }
//      return modifiedModules;
//   }

private ModuleGroup createModuleGroup(String techId, Row row) throws PhrescoException {
    if (S_LOGGER.isDebugEnabled()) {
        S_LOGGER.debug("row " + row.getRowNum());
    }
    ModuleGroup moduleGroup = new ModuleGroup();

    Cell serialNo = row.getCell(0);
    if (serialNo == null || Cell.CELL_TYPE_BLANK == serialNo.getCellType()) {
        return null;
    }

    String name = row.getCell(1).getStringCellValue();

    String identifier = ID + row.getCell(1).getStringCellValue().toLowerCase()
            .replace(ServerConstants.STR_BLANK_SPACE, ServerConstants.STR_UNDER_SCORE);
    String no = String.valueOf(identifier);

    String version = "1.0";
    Cell versionCell = row.getCell(2);
    String[] versionArray = null;
    if (versionCell != null && Cell.CELL_TYPE_BLANK != versionCell.getCellType()) {
        String versions = getValue(versionCell);
        versionArray = StringUtils.split(versions, DELIMITER);
        NAME_AND_DEP_MAP.put(identifier, versionArray);
    }

    Cell requireCell = row.getCell(3);
    String[] reqArray = null;
    if (requireCell != null && Cell.CELL_TYPE_BLANK != requireCell.getCellType()) {
        String reqs = getValue(requireCell);
        reqArray = StringUtils.split(reqs, DELIMITER);
    }

    Cell coreCell = row.getCell(4);
    String[] coreArray = null;
    if (coreCell != null && Cell.CELL_TYPE_BLANK != coreCell.getCellType()) {
        String core = getValue(coreCell);
        coreArray = StringUtils.split(core, DELIMITER);
    }

    int sNoInt = (int) serialNo.getNumericCellValue();
    SNO_AND_NAME_MAP.put(String.valueOf(sNoInt), identifier);

    Cell depCell = row.getCell(5);
    if (depCell != null && Cell.CELL_TYPE_BLANK != depCell.getCellType()) {
        String depModules = getValue(depCell);
        String[] depModuleIds = StringUtils.split(depModules, DELIMITER);
        NAME_AND_DEP_MAP.put(identifier, depModuleIds);
    }

    List<Documentation> docs = new ArrayList<Documentation>();
    Cell helptext = row.getCell(9);
    if (helptext != null && Cell.CELL_TYPE_BLANK != helptext.getCellType()) {
        Documentation doc = new Documentation();
        String docContent = helptext.getStringCellValue();
        doc.setContent(docContent);
        doc.setType(DocumentationType.HELP_TEXT);
        docs.add(doc);
    }

    Cell description = row.getCell(12);
    if (description != null && Cell.CELL_TYPE_BLANK != description.getCellType()) {
        Documentation doc = new Documentation();
        String docContent = description.getStringCellValue();
        doc.setContent(docContent);
        doc.setType(DocumentationType.DESCRIPTION);
        docs.add(doc);

    }
    moduleGroup.setDocs(docs);

    String fileExt = "zip";
    Cell filenameCell = row.getCell(13);
    if (filenameCell != null && Cell.CELL_TYPE_BLANK != filenameCell.getCellType()) {
        if (S_LOGGER.isDebugEnabled()) {
            S_LOGGER.debug("no " + no);
            S_LOGGER.debug("filename.getCellType() " + filenameCell.getCellType());
        }

        String filePath = filenameCell.getStringCellValue().trim();

        if (filePath.endsWith(".tar.gz")) {
            fileExt = "tar.gz";
        } else if (filePath.endsWith(".tar")) {
            fileExt = "tar";
        } else if (filePath.endsWith(".zip")) {
            fileExt = "zip";
        } else if (filePath.endsWith(".jar")) {
            fileExt = "jar";
        }

        //      publishModule(tech, module, filePath, fileExt);
    }

    List<Module> modulesList = createModuleList(versionArray, reqArray, coreArray, techId, fileExt, identifier);

    Cell imagenameCell = row.getCell(16);
    if (imagenameCell != null && Cell.CELL_TYPE_BLANK != imagenameCell.getCellType()) {

        String imagePath = imagenameCell.getStringCellValue().trim();
        //        String imageUrl = createImageContentUrl("");
        //         module.setImageURL(imageUrl);
        moduleGroup.setImageURL(imagePath);
    }

    Cell groupNameCell = row.getCell(14);
    if (groupNameCell != null && Cell.CELL_TYPE_BLANK != groupNameCell.getCellType()) {
        if (S_LOGGER.isDebugEnabled()) {
            S_LOGGER.debug("no " + no);
            S_LOGGER.debug("groupname.getCellType() " + groupNameCell.getCellType());
        }
        String groupId = groupNameCell.getStringCellValue();
        moduleGroup.setGroupId(groupId);
    }

    Cell artifactCell = row.getCell(15);
    if (artifactCell != null && Cell.CELL_TYPE_BLANK != artifactCell.getCellType()) {
        if (S_LOGGER.isDebugEnabled()) {
            S_LOGGER.debug("no " + no);
            S_LOGGER.debug("artifact name.getCellType() " + artifactCell.getCellType());
        }
        String artifactId = artifactCell.getStringCellValue();
        moduleGroup.setArtifactId(artifactId);
    }
    moduleGroup.setName(name);
    moduleGroup.setTechId(techId);
    moduleGroup.setVersions(modulesList);
    moduleGroup.setSystem(true);
    moduleGroup.setModuleId(identifier);
    moduleGroup.setType("module");
    moduleGroup.setCustomerId("photon");
    return moduleGroup;
}

From source file:com.photon.phresco.service.tools.VideoInfoGenerator.java

License:Apache License

 private static String getValue(Cell cell) {
   if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
      return cell.getStringCellValue();
   }// w w  w .  ja v a2  s .c o  m

   if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
      return String.valueOf(cell.getNumericCellValue());
   }

   return null;
}

From source file:com.phucdk.emailsender.utils.ExcelUtils.java

public static String getStringCellValue(int row, int column, XSSFSheet sheet) {
    Cell cell = getCell(row, column, sheet);
    if (cell != null) {
        return cell.getStringCellValue().trim();
    } else {/*  w w  w .j  a  v a 2s  .  c o m*/
        return null;
    }
}

From source file:com.plugin.excel.util.ExcelUtil.java

License:Apache License

/**
 * @param oldCell//ww w. j a v a2 s  .c o m
 * @param newCell
 * @param styleMap
 */
public static void copyCell(Cell oldCell, Cell newCell, Map<Integer, CellStyle> styleMap) {
    if (styleMap != null) {
        if (oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()) {
            newCell.setCellStyle(oldCell.getCellStyle());
        } else {
            int stHashCode = oldCell.getCellStyle().hashCode();
            CellStyle newCellStyle = styleMap.get(stHashCode);
            if (newCellStyle == null) {
                newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
                newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
                styleMap.put(stHashCode, newCellStyle);
            }
            newCell.setCellStyle(newCellStyle);
        }
    }
    switch (oldCell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        newCell.setCellValue(oldCell.getStringCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        newCell.setCellValue(oldCell.getNumericCellValue());
        break;
    case Cell.CELL_TYPE_BLANK:
        newCell.setCellType(Cell.CELL_TYPE_BLANK);
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        newCell.setCellValue(oldCell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        newCell.setCellErrorValue(oldCell.getErrorCellValue());
        break;
    case Cell.CELL_TYPE_FORMULA:
        newCell.setCellFormula(oldCell.getCellFormula());
        break;
    default:
        break;
    }

}

From source file:com.preparatic.csvreaders.FicheroExcel.java

License:Apache License

private void recorrerExcel() {
    this.listaPreguntas = new ArrayList<PreguntaTest>();

    /*/*from www  . j a  v  a  2 s  . co  m*/
     * Obtenemos la primera pestaa que se quiera procesar indicando el
     * ndice. Una vez obtenida la hoja excel con las filas que se quieren
     * leer obtenemos el iterator que nos permite recorrer cada una de las
     * filas que contiene.
     */
    HSSFSheet sheet = this.fichero.getSheetAt(0);
    ArrayList<String> celdasPregunta = null;

    // Recorremos todas las filas para obtener el contenido de cada celda
    // int NumberOfRows = 8493; // sheet.getPhysicalNumberOfRows() devuelve
    // 1.000 filas adicionales. Para evitar esto, ponemos el n
    // directamente.

    int NumberOfRows = 11;

    for (int rowIndex = 0; rowIndex < NumberOfRows; rowIndex++) {
        Row fila = sheet.getRow(rowIndex);
        celdasPregunta = new ArrayList<String>();
        for (int columna = 0; columna < 20; columna++) {
            // If the cell is missing from the file, generate a blank one
            // (Works by specifying a MissingCellPolicy)
            Cell celda = fila.getCell(columna, Row.CREATE_NULL_AS_BLANK);
            celda.setCellType(Cell.CELL_TYPE_STRING);
            celdasPregunta.add(celda.getStringCellValue());
        }
        this.listaPreguntas.add(new PreguntaTest(celdasPregunta));
    }

    try {
        fichero.close(); // cerramos el libro excel
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.projectswg.tools.SwgExcelConverter.java

License:Open Source License

private DatatableRow getDataTableRow(Row row, int expectedColumns, String[] types) {
    if (row == null || expectedColumns == 0)
        return null;

    int count = row.getPhysicalNumberOfCells();
    // Use > because empty cells are not considered a "physical cell"
    if (count > expectedColumns) {
        System.err.println("Row " + row.getRowNum() + " has " + count + " cells, expected " + expectedColumns);
        return null;
    }//from   w ww .jav a  2  s.co  m

    DatatableRow dataRow = new DatatableRow(expectedColumns);

    for (int i = 0; i < expectedColumns; i++) {
        Cell cell = row.getCell(i);
        if (cell == null) { // empty cell
            parseDataEmptyCell(types, dataRow, i);
            continue;
        }

        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            parseDataCell(types, dataRow, i, cell.getStringCellValue());
            break;
        case Cell.CELL_TYPE_NUMERIC:
            parseDataCell(types, dataRow, i, cell.getNumericCellValue());
            break;
        default:
            System.out.println("UNK CELL TYPE: " + cell.getCellType());
        }
    }
    return dataRow;
}

From source file:com.qihang.winter.poi.excel.export.template.ExcelExportOfTemplateUtil.java

License:Apache License

/**
 * ,??/*from   w ww .j  a  v a 2  s .  c  o  m*/
 * @param sheet
 * @param map
 * @throws Exception 
 */
private void deleteCell(Sheet sheet, Map<String, Object> map) throws Exception {
    Row row = null;
    Cell cell = null;
    int index = 0;
    while (index <= sheet.getLastRowNum()) {
        row = sheet.getRow(index++);
        if (row == null) {
            continue;
        }
        for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
            cell = row.getCell(i);
            if (row.getCell(i) != null && (cell.getCellType() == Cell.CELL_TYPE_STRING
                    || cell.getCellType() == Cell.CELL_TYPE_NUMERIC)) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
                String text = cell.getStringCellValue();
                if (text.contains(PoiElUtil.IF_DELETE)) {
                    if (Boolean.valueOf(PoiElUtil.eval(text
                            .substring(text.indexOf(PoiElUtil.START_STR) + 2, text.indexOf(PoiElUtil.END_STR))
                            .trim(), map).toString())) {
                        com.qihang.winter.poi.util.PoiSheetUtility.deleteColumn(sheet, i);
                    }
                    cell.setCellValue("");
                }
            }
        }
    }
}

From source file:com.qihang.winter.poi.excel.export.template.ExcelExportOfTemplateUtil.java

License:Apache License

/**
 * ?Cell??set/*from  w  w w . j  a v  a 2s  .c  o m*/
 * 
 * @param cell
 * @param map
 */
private void setValueForCellByMap(Cell cell, Map<String, Object> map) throws Exception {
    int cellType = cell.getCellType();
    if (cellType != Cell.CELL_TYPE_STRING && cellType != Cell.CELL_TYPE_NUMERIC) {
        return;
    }
    String oldString;
    cell.setCellType(Cell.CELL_TYPE_STRING);
    oldString = cell.getStringCellValue();
    if (oldString != null && oldString.indexOf(PoiElUtil.START_STR) != -1
            && !oldString.contains(PoiElUtil.FOREACH)) {
        // step 2. ???
        String params = null;
        boolean isNumber = false;
        if (isNumber(oldString)) {
            isNumber = true;
            oldString = oldString.replace(PoiElUtil.NUMBER_SYMBOL, "");
        }
        while (oldString.indexOf(PoiElUtil.START_STR) != -1) {
            params = oldString.substring(oldString.indexOf(PoiElUtil.START_STR) + 2,
                    oldString.indexOf(PoiElUtil.END_STR));

            oldString = oldString.replace(PoiElUtil.START_STR + params + PoiElUtil.END_STR,
                    PoiElUtil.eval(params, map).toString());
        }
        // ,
        if (isNumber && StringUtils.isNotBlank(oldString)) {
            cell.setCellValue(Double.parseDouble(oldString));
            cell.setCellType(Cell.CELL_TYPE_NUMERIC);
        } else {
            cell.setCellValue(oldString);
        }
    }
    //foreach ?
    if (oldString != null && oldString.contains(PoiElUtil.FOREACH)) {
        addListDataToExcel(cell, map, oldString.trim());
    }

}

From source file:com.qihang.winter.poi.excel.export.template.ExcelExportOfTemplateUtil.java

License:Apache License

/**
 * ??/*from  w w w  .  j  a v a2s.c  o m*/
 * @param cell
 * @param name
 * @return
 */
private List<com.qihang.winter.poi.excel.entity.params.ExcelTemplateParams> getAllDataColumns(Cell cell,
        String name) {
    List<com.qihang.winter.poi.excel.entity.params.ExcelTemplateParams> columns = new ArrayList<com.qihang.winter.poi.excel.entity.params.ExcelTemplateParams>();
    cell.setCellValue("");
    if (name.contains(PoiElUtil.END_STR)) {
        columns.add(new com.qihang.winter.poi.excel.entity.params.ExcelTemplateParams(
                name.replace(PoiElUtil.END_STR, PoiElUtil.EMPTY).trim(), cell.getCellStyle(),
                cell.getRow().getHeight()));
        return columns;
    }
    columns.add(new com.qihang.winter.poi.excel.entity.params.ExcelTemplateParams(name.trim(),
            cell.getCellStyle(), cell.getRow().getHeight()));
    int index = cell.getColumnIndex();
    Cell tempCell;
    while (true) {
        tempCell = cell.getRow().getCell(++index);
        if (tempCell == null) {
            break;
        }
        String cellStringString;
        try {//?,??,?
            cellStringString = tempCell.getStringCellValue();
            if (StringUtils.isBlank(cellStringString)) {
                break;
            }
        } catch (Exception e) {
            throw new ExcelExportException("for each ,?");
        }
        //?cell 
        tempCell.setCellValue("");
        if (cellStringString.contains(PoiElUtil.END_STR)) {
            columns.add(new com.qihang.winter.poi.excel.entity.params.ExcelTemplateParams(
                    cellStringString.trim().replace(PoiElUtil.END_STR, ""), tempCell.getCellStyle(),
                    tempCell.getRow().getHeight()));
            break;
        } else {
            if (cellStringString.trim().contains(teplateParams.getTempParams())) {
                columns.add(new com.qihang.winter.poi.excel.entity.params.ExcelTemplateParams(
                        cellStringString.trim(), tempCell.getCellStyle(), tempCell.getRow().getHeight()));
            } else {
                //?
                break;
            }
        }

    }
    return columns;
}

From source file:com.qihang.winter.poi.excel.imports.CellValueServer.java

License:Apache License

/**
 * ??/*  ww w .j a v  a2s  .  co m*/
 * 
 * @param xclass
 * @param cell
 * @param entity
 * @return
 */
private Object getCellValue(String xclass, Cell cell, ExcelImportEntity entity) {
    if (cell == null) {
        return "";
    }
    Object result = null;
    // ?,cell??
    if ("class java.util.Date".equals(xclass) || ("class java.sql.Time").equals(xclass)) {
        if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
            // ?
            result = cell.getDateCellValue();
        } else {
            cell.setCellType(Cell.CELL_TYPE_STRING);
            result = getDateData(entity, cell.getStringCellValue());
        }
        if (("class java.sql.Time").equals(xclass)) {
            result = new Time(((Date) result).getTime());
        }
    } else if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
        result = cell.getNumericCellValue();
    } else if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
        result = cell.getBooleanCellValue();
    } else {
        result = cell.getStringCellValue();
    }
    return result;
}