Example usage for org.apache.poi.xssf.usermodel XSSFCell getStringCellValue

List of usage examples for org.apache.poi.xssf.usermodel XSSFCell getStringCellValue

Introduction

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

Prototype

@Override
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.apache.ofbiz.pricat.sample.SamplePricatParser.java

License:Apache License

public boolean existsCurrencyId(XSSFSheet sheet) {
    report.print(UtilProperties.getMessage(resource, "StartCheckCurrencyId", locale),
            InterfaceReport.FORMAT_NOTE);
    XSSFCell currencyIdCell = sheet.getRow(2).getCell(1);
    currencyId = currencyIdCell.getStringCellValue().trim().toUpperCase();
    if (UtilValidate.isEmpty(currencyId)) {
        String errorMessage = UtilProperties.getMessage(resource, "CurrencyIdRequired", locale);
        report.println(errorMessage, InterfaceReport.FORMAT_ERROR);
        errorMessages.put(new CellReference(currencyIdCell), errorMessage);
        return false;
    } else {// w  w  w .  ja v  a2 s .c  om
        try {
            GenericValue currencyUom = delegator.findOne("Uom", UtilMisc.toMap("uomId", currencyId), false);
            if (!"CURRENCY_MEASURE".equals(currencyUom.getString("uomTypeId"))) {
                String errorMessage = UtilProperties.getMessage(resource, "CurrencyIdNotCurrency",
                        new Object[] { currencyId }, locale);
                report.println(errorMessage, InterfaceReport.FORMAT_ERROR);
                errorMessages.put(new CellReference(currencyIdCell), errorMessage);
                return false;
            }
        } catch (GenericEntityException e) {
            String errorMessage = UtilProperties.getMessage(resource, "CurrencyIdNotFound",
                    new Object[] { currencyId }, locale);
            report.println(errorMessage, InterfaceReport.FORMAT_ERROR);
            errorMessages.put(new CellReference(currencyIdCell), errorMessage);
            return false;
        }
        report.print(UtilProperties.getMessage(resource, "CurrencyIdIs", new Object[] { currencyId }, locale),
                InterfaceReport.FORMAT_NOTE);
        report.println(" ... " + UtilProperties.getMessage(resource, "ok", locale), InterfaceReport.FORMAT_OK);
    }
    return true;
}

From source file:org.apache.ofbiz.pricat.sample.SamplePricatParser.java

License:Apache License

/**
 * Get data by version definition.// w  w w . j  a v a2  s.com
 * 
 * @param row
 * @param colNames 
 * @param size 
 * @return
 */
public List<Object> getCellContents(XSSFRow row, List<Object[]> colNames, int size) {
    List<Object> results = new ArrayList<Object>();
    boolean foundError = false;
    if (isEmptyRow(row, size, true)) {
        return null;
    }

    // check and get data
    for (int i = 0; i < size; i++) {
        XSSFCell cell = null;
        if (row.getPhysicalNumberOfCells() > i) {
            cell = row.getCell(i);
        }
        if (cell == null) {
            if (((Boolean) colNames.get(i)[2]).booleanValue()
                    && (facilities.keySet().size() > 1 || (facilities.keySet().size() == 1 && i >= 2))) {
                report.print(UtilProperties.getMessage(resource, "ErrorColCannotEmpty",
                        new Object[] { colNames.get(i)[0] }, locale), InterfaceReport.FORMAT_WARNING);
                cell = row.createCell(i);
                errorMessages.put(new CellReference(cell), UtilProperties.getMessage(resource,
                        "ErrorColCannotEmpty", new Object[] { colNames.get(i)[0] }, locale));
                foundError = true;
                results.add(null);
                continue;
            } else {
                cell = row.createCell(i);
            }
        }
        int cellType = cell.getCellType();
        String cellValue = formatter.formatCellValue(cell);
        if (UtilValidate.isNotEmpty(cellValue) && UtilValidate.isNotEmpty(cellValue.trim())) {
            if (cellType == XSSFCell.CELL_TYPE_FORMULA) {
                try {
                    cellValue = BigDecimal.valueOf(cell.getNumericCellValue())
                            .setScale(FinAccountHelper.decimals, FinAccountHelper.rounding).toString();
                } catch (IllegalStateException e) {
                    try {
                        cellValue = cell.getStringCellValue();
                    } catch (IllegalStateException e1) {
                        // do nothing
                    }
                }
                report.print(((i == 0) ? "" : ", ") + cellValue, InterfaceReport.FORMAT_NOTE);
            } else {
                report.print(((i == 0) ? "" : ", ") + cellValue, InterfaceReport.FORMAT_NOTE);
            }
        } else {
            report.print(((i == 0) ? "" : ","), InterfaceReport.FORMAT_NOTE);
        }
        if (((Boolean) colNames.get(i)[2]).booleanValue() && UtilValidate.isEmpty(cellValue)
                && (facilities.keySet().size() > 1 || (facilities.keySet().size() == 1 && i >= 2))) {
            report.print(UtilProperties.getMessage(resource, "ErrorColCannotEmpty",
                    new Object[] { colNames.get(i)[0] }, locale), InterfaceReport.FORMAT_WARNING);
            errorMessages.put(new CellReference(cell), UtilProperties.getMessage(resource,
                    "ErrorColCannotEmpty", new Object[] { colNames.get(i)[0] }, locale));
            foundError = true;
            results.add(null);
            continue;
        }
        if (((Boolean) colNames.get(i)[2]).booleanValue() && cellType != (int) colNames.get(i)[1]) {
            // String warningMessage = "";
            if ((int) colNames.get(i)[1] == XSSFCell.CELL_TYPE_STRING) {
                if (UtilValidate.isNotEmpty(cellValue) && UtilValidate.isNotEmpty(cellValue.trim())) {
                    results.add(cellValue);
                } else {
                    results.add(null);
                }
            } else if ((int) colNames.get(i)[1] == XSSFCell.CELL_TYPE_NUMERIC) {
                if (cell.getCellType() != XSSFCell.CELL_TYPE_STRING) {
                    cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                }
                try {
                    results.add(BigDecimal.valueOf(Double.parseDouble(cell.getStringCellValue()))
                            .setScale(FinAccountHelper.decimals, FinAccountHelper.rounding));
                } catch (NumberFormatException e) {
                    results.add(null);
                    errorMessages.put(new CellReference(cell),
                            UtilProperties.getMessage(resource, "ErrorParseValueToNumeric", locale));
                }
            }
        } else {
            if (UtilValidate.isEmpty(cellValue) || UtilValidate.isEmpty(cellValue.trim())) {
                results.add(null);
                continue;
            }
            if ((int) colNames.get(i)[1] == XSSFCell.CELL_TYPE_STRING) {
                if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
                    cellValue = cell.getStringCellValue().trim();
                    results.add(cellValue);
                } else {
                    results.add(cellValue.trim());
                }
            } else if ((int) colNames.get(i)[1] == XSSFCell.CELL_TYPE_NUMERIC) {
                if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
                    try {
                        results.add(BigDecimal.valueOf(Double.valueOf(cell.getStringCellValue())));
                    } catch (NumberFormatException e) {
                        results.add(null);
                        errorMessages.put(new CellReference(cell),
                                UtilProperties.getMessage(resource, "ErrorParseValueToNumeric", locale));
                    }
                } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
                    try {
                        results.add(BigDecimal.valueOf(cell.getNumericCellValue())
                                .setScale(FinAccountHelper.decimals, FinAccountHelper.rounding));
                    } catch (NumberFormatException e) {
                        results.add(null);
                        errorMessages.put(new CellReference(cell),
                                UtilProperties.getMessage(resource, "ErrorParseValueToNumeric", locale));
                    }
                } else {
                    try {
                        results.add(BigDecimal.valueOf(Double.valueOf(cellValue))
                                .setScale(FinAccountHelper.decimals, FinAccountHelper.rounding));
                    } catch (NumberFormatException e) {
                        results.add(null);
                        errorMessages.put(new CellReference(cell),
                                UtilProperties.getMessage(resource, "ErrorParseValueToNumeric", locale));
                    }
                }
            }
        }
    }
    if (foundError) {
        return null;
    }
    return results;
}

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

License:Open Source License

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

    StringBuilder builder = new StringBuilder("");
    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);

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

        XSSFWorkbook wb;
        try {
            wb = SecurityHelper.doPrivilegedIOExceptionAction(new PrivilegedExceptionAction<XSSFWorkbook>() {
                public XSSFWorkbook run() throws Exception {
                    return new XSSFWorkbook(is);
                }
            });
        } catch (IOException e) {
            throw new DocumentReadException("Can't open spreadsheet.", e);
        } catch (OpenXML4JRuntimeException e) {
            return builder.toString();
        }
        for (int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
            XSSFSheet sheet = wb.getSheetAt(sheetNum);
            if (sheet != null) {
                for (int rowNum = sheet.getFirstRowNum(); rowNum <= sheet.getLastRowNum(); rowNum++) {
                    XSSFRow row = sheet.getRow(rowNum);

                    if (row != null) {
                        int lastcell = row.getLastCellNum();
                        for (int k = 0; k < lastcell; k++) {
                            XSSFCell cell = row.getCell(k);
                            if (cell != null) {
                                switch (cell.getCellType()) {
                                case XSSFCell.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 XSSFCell.CELL_TYPE_FORMULA:
                                    builder.append(cell.getCellFormula().toString()).append(" ");
                                    break;
                                case XSSFCell.CELL_TYPE_BOOLEAN:
                                    builder.append(cell.getBooleanCellValue()).append(" ");
                                    break;
                                case XSSFCell.CELL_TYPE_ERROR:
                                    builder.append(cell.getErrorCellValue()).append(" ");
                                    break;
                                case XSSFCell.CELL_TYPE_STRING:
                                    builder.append(cell.getStringCellValue().toString()).append(" ");
                                    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.finra.jtaf.core.utilities.ExcelFileParser.java

License:Apache License

public List<List<String>> parseExcelFile(boolean isXlsx) throws Exception {
    List<List<String>> parsedExcelFile = new ArrayList<List<String>>();
    if (isXlsx) {
        for (int i = 0, numberOfRows = workBookSheetXlsx.getPhysicalNumberOfRows(); i < numberOfRows + 1; i++) {
            XSSFRow row = workBookSheetXlsx.getRow(i);
            if (row != null) {
                List<String> parsedExcelRow = new ArrayList<String>();
                for (int j = 0, numberOfColumns = row.getLastCellNum(); j < numberOfColumns; j++) {
                    XSSFCell cell = row.getCell(j);
                    if (cell != null) {
                        try {
                            if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
                                parsedExcelRow.add(cell.getStringCellValue());
                            } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BLANK) {
                                parsedExcelRow.add("");
                            } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
                                parsedExcelRow.add(String.valueOf(cell.getBooleanCellValue()));
                            } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
                                parsedExcelRow.add(String.valueOf(cell.getNumericCellValue()));
                            } else if (cell.getCellType() == XSSFCell.CELL_TYPE_FORMULA) {
                                parsedExcelRow.add("");
                            } else {
                                parsedExcelRow.add(cell.getStringCellValue());
                            }//from  w  w w.j a va 2s .  c om
                        } catch (Exception e) {
                            logger.fatal("Oops! Can't read cell (row = " + i + ", column = " + j
                                    + ") in the excel file! Change cell format to 'Text', please!");
                            return null;
                        }
                    } else {
                        parsedExcelRow.add("");
                    }
                }
                parsedExcelFile.add(parsedExcelRow);
            }
        }
    } else {
        for (int i = 0, numberOfRows = workBookSheetXls.getPhysicalNumberOfRows(); i < numberOfRows + 1; i++) {
            HSSFRow row = workBookSheetXls.getRow(i);
            if (row != null) {
                List<String> parsedExcelRow = new ArrayList<String>();
                for (int j = 0, numberOfColumns = row.getLastCellNum(); j < numberOfColumns; j++) {
                    HSSFCell cell = row.getCell(j);
                    if (cell != null) {
                        try {
                            if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                                parsedExcelRow.add(cell.getStringCellValue());
                            } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
                                parsedExcelRow.add("");
                            } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
                                parsedExcelRow.add(String.valueOf(cell.getBooleanCellValue()));
                            } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
                                parsedExcelRow.add(String.valueOf(cell.getNumericCellValue()));
                            } else if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
                                parsedExcelRow.add(String.valueOf(""));
                            } else {
                                parsedExcelRow.add(cell.getStringCellValue());
                            }
                        } catch (Exception e) {
                            logger.fatal("Oops! Can't read cell (row = " + i + ", column = " + j
                                    + ") in the excel file! Change cell format to 'Text', please!");
                            return null;
                        }
                    } else {
                        parsedExcelRow.add("");
                    }
                }
                parsedExcelFile.add(parsedExcelRow);
            }
        }
    }

    return parsedExcelFile;
}

From source file:org.hlc.utility.excel.ExcelInputHandler.java

License:Apache License

/**
 * Import excel.//  w w w  . j  a v  a 2  s  .c o  m
 *
 * @param <T> the generic type
 * @param type the type
 * @param in the in
 * @return the list
 */
@SuppressWarnings("rawtypes")
public <T> List<T> importExcel2007(Class<T> type, InputStream in) {

    Excel excelAnn = type.getAnnotation(Excel.class);
    if (excelAnn == null) {
        throw new ExcelException("The Class <" + type + "> did not Excel");
    }

    List<T> list = new ArrayList<T>();

    Map<String, Method> mapping = new LinkedHashMap<String, Method>();
    Map<String, TypeHandler> converters = new HashMap<String, TypeHandler>();

    try {
        // Step1 ??
        Field fileds[] = type.getDeclaredFields();
        for (int i = 0; i < fileds.length; i++) {
            Field field = fileds[i];
            ExcelColumn column = field.getAnnotation(ExcelColumn.class);
            if (column != null) {
                Method setMethod = ReflectionUtils.setValueMethod(field, type);
                mapping.put(column.value(), setMethod);
                if (column.converter() != TypeHandler.class) {
                    converters.put(setMethod.getName().toString(), column.converter().newInstance());
                } else {
                    converters.put(setMethod.getName().toString(),
                            TypeHandlerFactory.getHandler(field.getType()));
                }
            }
        }

        T temp = null;
        XSSFWorkbook hssfWorkbook = new XSSFWorkbook(in);
        for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
            XSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            if (hssfSheet == null) {
                continue;
            }

            // ?Sheet
            List<Method> methods = new ArrayList<Method>();
            for (int rowNum = 0; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {

                XSSFRow hssfRow = hssfSheet.getRow(rowNum);
                if (hssfRow == null) {
                    continue;
                }

                // ?
                if (rowNum == 0) {
                    for (int cellNum = 0; cellNum < hssfRow.getLastCellNum(); cellNum++) {
                        String title = hssfRow.getCell(cellNum).getStringCellValue();
                        Method me = mapping.get(title);
                        if (me == null) {
                            continue;
                        }
                        methods.add(me);
                    }
                    continue;
                }

                temp = type.newInstance();
                for (int cellNum = 0; cellNum < methods.size(); cellNum++) {
                    XSSFCell xh = hssfRow.getCell(cellNum);
                    if (xh == null) {
                        continue;
                    }
                    Method m = methods.get(cellNum);
                    TypeHandler handler = converters.get(m.getName());
                    if (handler == null) {
                        continue;
                    }
                    xh.setCellType(Cell.CELL_TYPE_STRING);
                    String value = xh.getStringCellValue();
                    if (StringUtils.isEmpty(value)) {
                        continue;
                    }
                    Object val = null;
                    try {
                        val = handler.stringToType(value);
                    } catch (Exception e) {
                        throw new ExcelException("" + (numSheet + 1) + "" + (rowNum + 1)
                                + "" + (cellNum + 1) + "" + value + "??");
                    }
                    methods.get(cellNum).invoke(temp, val);
                }
                list.add(temp);
            }
        }
    } catch (Exception e) {
        throw new ExcelException("Excel processing error?", e);
    }
    return list;
}

From source file:org.kuali.test.runner.output.PoiHelper.java

License:Educational Community License

private void copyCell(XSSFCell oldCell, XSSFCell newCell, Map<Integer, XSSFCellStyle> styleMap) {
    if (styleMap != null) {
        int stHashCode = oldCell.getCellStyle().hashCode();
        XSSFCellStyle newCellStyle = styleMap.get(stHashCode);
        if (newCellStyle == null) {
            newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
            newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
            newCellStyle.setFont(oldCell.getCellStyle().getFont());
            styleMap.put(stHashCode, newCellStyle);
        }//from w w w .  j ava2 s  .  c  o  m

        newCell.setCellStyle(newCellStyle);
    }

    switch (oldCell.getCellType()) {
    case XSSFCell.CELL_TYPE_STRING:
        newCell.setCellValue(oldCell.getStringCellValue());
        break;
    case XSSFCell.CELL_TYPE_NUMERIC:
        newCell.setCellValue(oldCell.getNumericCellValue());
        break;
    case XSSFCell.CELL_TYPE_BLANK:
        newCell.setCellType(HSSFCell.CELL_TYPE_BLANK);
        break;
    case XSSFCell.CELL_TYPE_BOOLEAN:
        newCell.setCellValue(oldCell.getBooleanCellValue());
        break;
    case XSSFCell.CELL_TYPE_ERROR:
        newCell.setCellErrorValue(oldCell.getErrorCellValue());
        break;
    case XSSFCell.CELL_TYPE_FORMULA:
        newCell.setCellFormula(oldCell.getCellFormula());
        break;
    default:
        break;
    }
}

From source file:org.nuxeo.ecm.core.convert.plugins.text.extractors.XLX2TextConverter.java

License:Apache License

protected void appendTextFromCell(XSSFCell cell, StringBuffer sb) {
    String cellValue = null;//from ww  w  .j a v a2  s. c  om
    switch (cell.getCellType()) {
    case XSSFCell.CELL_TYPE_NUMERIC:
        cellValue = Double.toString(cell.getNumericCellValue()).trim();
        break;
    case XSSFCell.CELL_TYPE_STRING:
        cellValue = cell.getStringCellValue().trim();
        break;
    }

    if (cellValue != null && cellValue.length() > 0) {
        sb.append(cellValue).append(CELL_SEP);
    }
}

From source file:org.openstreetmap.josm.plugins.msf1.XLSX_Reader.java

public static void getIndexes(String arg) throws IOException {
    try {//  www.j  a  v  a 2s .  co m

        ExcelFileToRead = new FileInputStream(arg);
        XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
        XSSFCell cell;
        XSSFSheet sheet = wb.getSheetAt(0);
        Iterator cells = sheet.getRow(0).cellIterator();

        while (cells.hasNext()) {
            cell = (XSSFCell) cells.next();
            if (cell != null) {
                if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
                    if (cell.getStringCellValue().equalsIgnoreCase("_Location_Latitude")) {
                        lat_index = cell.getColumnIndex();
                    }
                    if (cell.getStringCellValue().equalsIgnoreCase("_LOCATION_longitude")) {
                        lon_index = cell.getColumnIndex();
                    }
                    if (cell.getStringCellValue().equalsIgnoreCase("Village_name")) {
                        villageName_index = cell.getColumnIndex();
                    }
                    if (cell.getStringCellValue().equalsIgnoreCase("Alt_village_name")) {
                        altVillageName_index = cell.getColumnIndex();
                    }
                    if (cell.getStringCellValue().equalsIgnoreCase("HANDPUMP_WORKING")) {
                        handpump_condition_index = cell.getColumnIndex();
                    }
                    if (cell.getStringCellValue().equalsIgnoreCase("WATERPOINT_NAME")) {
                        waterPointName_index = cell.getColumnIndex();
                    }
                    if (cell.getStringCellValue().equalsIgnoreCase("BOREHOLE_PROTECTED")) {
                        borehole_access_index = cell.getColumnIndex();
                    }
                }

            }

        }

        lon_array = new String[sheet.getPhysicalNumberOfRows()];

        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            Row row = sheet.getRow(i);

            if (row.getCell(lon_index) == null
                    || row.getCell(lon_index).getCellType() == Cell.CELL_TYPE_BLANK) {
                i++;
            } else if (row.getCell(lon_index).getCellType() == XSSFCell.CELL_TYPE_NUMERIC
                    || row.getCell(lon_index).getCellType() == XSSFCell.CELL_TYPE_STRING) {
                String var = row.getCell(lon_index).toString();
                lon_array[i] = var;
                // System.out.println(var);
            } else {
                lon_array[i] = "null";

            }
        }

        lat_array = new String[sheet.getPhysicalNumberOfRows()];
        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            Row row = sheet.getRow(i);
            if (row.getCell(lat_index) == null
                    || row.getCell(lat_index).getCellType() == Cell.CELL_TYPE_BLANK) {
                i++;
            } else if (row.getCell(lat_index).getCellType() == XSSFCell.CELL_TYPE_NUMERIC
                    || row.getCell(lat_index).getCellType() == XSSFCell.CELL_TYPE_STRING) {
                String var = row.getCell(lat_index).toString();
                lat_array[i] = var;
                // System.out.println(var);
            } else {
                lat_array[i] = "null";

            }
        }

        villageName_array = new String[sheet.getPhysicalNumberOfRows()];
        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            Row row = sheet.getRow(i);
            if (row.getCell(villageName_index) == null
                    || row.getCell(villageName_index).getCellType() == Cell.CELL_TYPE_BLANK) {
                i++;
            } else if (row.getCell(villageName_index).getCellType() == XSSFCell.CELL_TYPE_NUMERIC
                    || row.getCell(villageName_index).getCellType() == XSSFCell.CELL_TYPE_STRING) {
                String var = row.getCell(villageName_index).toString();
                villageName_array[i] = var;
                //  System.out.println(var);
            } else {
                villageName_array[i] = "null";

            }

        }

        altVilageName_array = new String[sheet.getPhysicalNumberOfRows()];
        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            Row row = sheet.getRow(i);
            if (row.getCell(altVillageName_index) == null
                    || row.getCell(altVillageName_index).getCellType() == Cell.CELL_TYPE_BLANK) {
                i++;
            } else if (row.getCell(altVillageName_index).getCellType() == XSSFCell.CELL_TYPE_NUMERIC
                    || row.getCell(altVillageName_index).getCellType() == XSSFCell.CELL_TYPE_STRING) {
                String var = row.getCell(altVillageName_index).toString();
                altVilageName_array[i] = var;
                //  System.out.println(var);
            } else {
                altVilageName_array[i] = "null";

            }

        }

        borehole_access_array = new String[sheet.getPhysicalNumberOfRows()];
        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            Row row = sheet.getRow(i);
            if (row.getCell(borehole_access_index) == null
                    || row.getCell(borehole_access_index).getCellType() == Cell.CELL_TYPE_BLANK) {
                i++;
            } else if (row.getCell(borehole_access_index).getCellType() == XSSFCell.CELL_TYPE_NUMERIC
                    || row.getCell(borehole_access_index).getCellType() == XSSFCell.CELL_TYPE_STRING) {
                String var = row.getCell(borehole_access_index).toString();
                borehole_access_array[i] = var;
                // System.out.println(var);
            } else {
                borehole_access_array[i] = "null";

            }

        }
        handpump_condition_array = new String[sheet.getPhysicalNumberOfRows()];
        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            Row row = sheet.getRow(i);
            if (row.getCell(handpump_condition_index) == null
                    || row.getCell(handpump_condition_index).getCellType() == Cell.CELL_TYPE_BLANK) {
                i++;
            } else if (row.getCell(handpump_condition_index).getCellType() == XSSFCell.CELL_TYPE_NUMERIC
                    || row.getCell(handpump_condition_index).getCellType() == XSSFCell.CELL_TYPE_STRING) {
                String var = row.getCell(handpump_condition_index).toString();
                handpump_condition_array[i] = var;
                //System.out.println(var);
            } else {
                handpump_condition_array[i] = "null";

            }

        }

        waterPointName_array = new String[sheet.getPhysicalNumberOfRows()];
        for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
            Row row = sheet.getRow(i);
            if (row.getCell(waterPointName_index) == null
                    || row.getCell(waterPointName_index).getCellType() == Cell.CELL_TYPE_BLANK) {
                i++;
            } else if (row.getCell(waterPointName_index).getCellType() == XSSFCell.CELL_TYPE_NUMERIC
                    || row.getCell(waterPointName_index).getCellType() == XSSFCell.CELL_TYPE_STRING) {
                String var = row.getCell(waterPointName_index).toString();
                waterPointName_array[i] = var;
                // System.out.println(var);
            } else {
                waterPointName_array[i] = "null";

            }

        }

        //    public static String[] getLon_array() {
        //        return lon_array;
        //    }
        //    
        //    public static String[] getLat_array() {
        //        return lat_array;
        //    }
        //    public static String[] getVillageName_array() {
        //        return  villageName_array;
        //    } 
        //    public static String[] getAltVillageName_array() {
        //        return altVilageName_array;
        //    }
        //    public static String[] getBoreholeAccess_array() {
        //        return borehole_access_array;
        //    }
        //    public static String[] getHandPumpCondition_array() {
        //        return handpump_condition_array;
        //    }
        //    public static String[] getWaterPoint_array() {
        //        return waterPointName_array;
        //    }
        wb.close();
    } catch (IOException e) {
    }
}

From source file:org.sakaiproject.tool.gradebook.ui.SpreadsheetUploadBean.java

License:Educational Community License

private String fromXSSFRowtoCSV(XSSFRow row) {
    StringBuffer csvRow = new StringBuffer();
    int l = row.getLastCellNum();
    for (int i = 0; i < l; i++) {
        XSSFCell cell = row.getCell((short) i);
        String cellValue = "";
        if (cell == null || cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
            cellValue = "";
        } else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
            cellValue = "\"" + cell.getStringCellValue() + "\"";
        } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            double value = cell.getNumericCellValue();
            cellValue = getNumberFormat().format(value);
            cellValue = "\"" + cellValue + "\"";
        }/*  w  w w  .jav  a 2 s  . com*/

        csvRow.append(cellValue);

        if (i < l) {
            csvRow.append(getCsvDelimiter().toCharArray()[0]);
        }
    }
    return csvRow.toString();
}

From source file:org.syrahtest.core.data.xls.ExcelReader.java

License:Open Source License

public static void readXLSXFile() throws IOException {

    InputStream ExcelFileToRead = new FileInputStream("C:/Test.xlsx");
    XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);

    XSSFWorkbook test = new XSSFWorkbook();

    XSSFSheet sheet = wb.getSheetAt(0);/*from  w  w  w .j a  v  a  2 s  . c  o  m*/
    XSSFRow row;
    XSSFCell cell;

    Iterator rows = sheet.rowIterator();

    while (rows.hasNext()) {
        row = (XSSFRow) rows.next();
        Iterator cells = row.cellIterator();
        while (cells.hasNext()) {
            cell = (XSSFCell) cells.next();

            if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
                System.out.print(cell.getStringCellValue() + " ");
            } else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
                System.out.print(cell.getNumericCellValue() + " ");
            } else {
                //U Can Handel Boolean, Formula, Errors
            }
        }
        System.out.println();
    }

}