Example usage for org.apache.poi.ss.usermodel Row cellIterator

List of usage examples for org.apache.poi.ss.usermodel Row cellIterator

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Row cellIterator.

Prototype

Iterator<Cell> cellIterator();

Source Link

Usage

From source file:org.silverpeas.core.index.indexing.parser.excelParser.ExcelParser.java

License:Open Source License

/**
 * Read the text content of a pdf file and store it in out to be ready to be indexed.
 * @param out/*  w w w  . jav  a  2  s . c om*/
 * @param path
 * @param encoding
 * @throws IOException
 */
@Override
public void outPutContent(Writer out, String path, String encoding) throws IOException {
    FileInputStream file = new FileInputStream(path);
    try {
        POIFSFileSystem fs = new POIFSFileSystem(file);
        HSSFWorkbook workbook = new HSSFWorkbook(fs);

        HSSFSheet sheet;
        for (int nbSheet = 0; nbSheet < workbook.getNumberOfSheets(); nbSheet++) {
            // extract sheet's name
            out.write(workbook.getSheetName(nbSheet));
            sheet = workbook.getSheetAt(nbSheet);
            Iterator<Row> rows = sheet.rowIterator();
            while (rows.hasNext()) {
                Row row = rows.next();
                Iterator<Cell> cells = row.cellIterator();
                while (cells.hasNext()) {
                    Cell cell = cells.next();
                    if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                        out.write(cell.getStringCellValue());
                        out.write(' ');
                    }
                }
            }
        }
    } catch (IOException ioe) {
        SilverTrace.error("indexing", "ExcelParser.outPutContent()", "indexing.MSG_IO_ERROR_WHILE_READING",
                path, ioe);
    } finally {
        IOUtils.closeQuietly(file);
    }
}

From source file:org.silverpeas.search.indexEngine.parser.excelParser.ExcelParser.java

License:Open Source License

/**
 *Read the text content of a pdf file and store it in out to be ready to be indexed.
 * @param out/*from   w ww .j a  v a 2 s .  c om*/
 * @param path
 * @param encoding
 * @throws IOException
 */
@Override
public void outPutContent(Writer out, String path, String encoding) throws IOException {
    FileInputStream file = new FileInputStream(path);
    try {
        POIFSFileSystem fs = new POIFSFileSystem(file);
        HSSFWorkbook workbook = new HSSFWorkbook(fs);

        HSSFSheet sheet = null;
        for (int nbSheet = 0; nbSheet < workbook.getNumberOfSheets(); nbSheet++) {
            // extract sheet's name
            out.write(workbook.getSheetName(nbSheet));
            SilverTrace.debug("indexEngine", "ExcelParser.outputContent", "root.MSG_GEN_PARAM_VALUE",
                    "sheetName = " + workbook.getSheetName(nbSheet));
            sheet = workbook.getSheetAt(nbSheet);
            Iterator<Row> rows = sheet.rowIterator();
            while (rows.hasNext()) {
                Row row = rows.next();
                Iterator<Cell> cells = row.cellIterator();
                while (cells.hasNext()) {
                    Cell cell = cells.next();
                    if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
                        out.write(cell.getStringCellValue());
                        out.write(' ');
                        SilverTrace.debug("indexEngine", "ExcelParser.outputContent",
                                "root.MSG_GEN_PARAM_VALUE", "cellValue = " + cell.getStringCellValue());
                    }
                }
            }
        }
    } catch (IOException ioe) {
        SilverTrace.error("indexEngine", "ExcelParser.outPutContent()",
                "indexEngine.MSG_IO_ERROR_WHILE_READING", path, ioe);
    } finally {
        IOUtils.closeQuietly(file);
    }
}

From source file:org.squashtest.tm.service.internal.batchimport.testcase.excel.ExcelWorkbookParser.java

License:Open Source License

public boolean isEmpty(Row row) {
    boolean isEmpty = true;

    if (row != null) {
        Iterator<Cell> iterator = row.cellIterator();

        while (iterator.hasNext()) {
            Cell c = iterator.next();/*from  w  w  w  .  ja  v a2s.c o  m*/
            if (!StringUtils.isBlank(c.toString())) {
                isEmpty = false;
                break;
            }
        }
    }

    return isEmpty;
}

From source file:org.talend.dataprep.schema.xls.XlsSchemaParser.java

License:Open Source License

/**
 * We store (cell types per row) per column.
 *
 * @param sheet key is the column number, value is a Map with key row number and value Type
 * @return A Map&lt;colId, Map&lt;rowId, type&gt;&gt;
 *///from   w  w w  .  ja va 2 s  . com
private SortedMap<Integer, SortedMap<Integer, String>> collectSheetTypeMatrix(Sheet sheet,
        FormulaEvaluator formulaEvaluator) {

    int firstRowNum = sheet.getFirstRowNum();
    int lastRowNum = sheet.getLastRowNum();

    LOGGER.debug("firstRowNum: {}, lastRowNum: {}", firstRowNum, lastRowNum);

    SortedMap<Integer, SortedMap<Integer, String>> cellsTypeMatrix = new TreeMap<>();

    // we start analysing rows
    for (int rowCounter = firstRowNum; rowCounter <= lastRowNum; rowCounter++) {

        int cellCounter = 0;

        Row row = sheet.getRow(rowCounter);
        if (row == null) {
            continue;
        }

        Iterator<Cell> cellIterator = row.cellIterator();

        String currentType;

        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();

            int xlsType = Cell.CELL_TYPE_STRING;

            try {
                xlsType = cell.getCellType() == Cell.CELL_TYPE_FORMULA ? //
                        formulaEvaluator.evaluate(cell).getCellType() : cell.getCellType();
            } catch (Exception e) {
                // ignore formula error evaluation get as a String with the formula
            }
            switch (xlsType) {
            case Cell.CELL_TYPE_BOOLEAN:
                currentType = BOOLEAN.getName();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                currentType = getTypeFromNumericCell(cell);
                break;
            case Cell.CELL_TYPE_BLANK:
                currentType = BLANK;
                break;
            case Cell.CELL_TYPE_FORMULA:
            case Cell.CELL_TYPE_STRING:
                currentType = STRING.getName();
                break;
            case Cell.CELL_TYPE_ERROR:
                // we cannot really do anything with an error
            default:
                currentType = ANY.getName();
            }

            SortedMap<Integer, String> cellInfo = cellsTypeMatrix.get(cellCounter);

            if (cellInfo == null) {
                cellInfo = new TreeMap<>();
            }
            cellInfo.put(rowCounter, currentType);

            cellsTypeMatrix.put(cellCounter, cellInfo);
            cellCounter++;
        }
    }

    LOGGER.trace("cellsTypeMatrix: {}", cellsTypeMatrix);
    return cellsTypeMatrix;
}

From source file:org.talend.mdm.webapp.browserecords.server.service.UploadService.java

License:Open Source License

protected String[] readHeader(Row headerRow, String[] headerRecord) throws UploadException {
    List<String> headers = new LinkedList<String>();
    String header;/*from ww  w.j  av  a2s. co  m*/
    int index = 0;
    if (headersOnFirstLine) {
        if (FILE_TYPE_EXCEL_SUFFIX.equals(fileType.toLowerCase())
                || FILE_TYPE_EXCEL2010_SUFFIX.equals(fileType.toLowerCase())) {
            Iterator<Cell> headerIterator = headerRow.cellIterator();
            while (headerIterator.hasNext()) {
                Cell cell = headerIterator.next();
                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    header = cell.getRichStringCellValue().getString();
                    headers.add(handleHeader(header, index));
                }
                index++;
            }
        } else if (FILE_TYPE_CSV_SUFFIX.equals(fileType.toLowerCase())) {
            for (String element : headerRecord) {
                headers.add(handleHeader(element, index));
                index++;
            }
        }
    } else {
        Iterator<String> headerIterator = headerVisibleMap.keySet().iterator();
        while (headerIterator.hasNext()) {
            header = headerIterator.next();
            registXsiType(header, index);
            headers.add(header);
            index++;
        }
    }
    return headers.toArray(new String[headers.size()]);
}

From source file:org.unhcr.eg.odk.utilities.xlsform.controller.SheetProcessor.java

public static Survey processSurveySheet(Workbook wb, Survey survey) {
    Sheet sheet = wb.getSheet(XLSFormModel.SheetName.SURVEY.value());
    //Iterate through each rows from first sheet
    Iterator<Row> rowIterator = sheet.iterator();
    int i = 0;/*w  w  w  .  ja v  a 2 s. c  o m*/
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next(); //For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        if (i == 0) {
            loadColumns(cellIterator, survey);
        } else {
            Question q = createQuestion(cellIterator, survey);
            String position = getQuestionPosition(q, survey);
            survey.getQuestions().put(new QuestionPosition(i, position, q.getName()), q);
        }
        i++;
    }
    return survey;

}

From source file:org.unhcr.eg.odk.utilities.xlsform.controller.SheetProcessor.java

public static Survey processSettingsSheet(Workbook wb, Survey survey) {
    Sheet sheet = wb.getSheet(XLSFormModel.SheetName.SETTINGS.value());
    //Iterate through each rows from first sheet
    Iterator<Row> rowIterator = sheet.iterator();
    int i = 0;// w w w  .j av  a  2s .  co  m
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next(); //For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        if (i == 0) {
            loadSettingsColumns(cellIterator, survey);
        } else if (i == 1) {
            survey = createSetting(cellIterator, survey);
        }
        i++;
    }
    return survey;
}

From source file:org.unhcr.eg.odk.utilities.xlsform.controller.SheetProcessor.java

public static Survey processChoicesSheet(Workbook wb, Survey survey) {
    Sheet sheet = wb.getSheet(XLSFormModel.SheetName.CHOICES.value());
    //Iterate through each rows from first sheet
    Iterator<Row> rowIterator = sheet.iterator();
    int i = 0;//from  w  w w  . ja va  2s. c o m
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next(); //For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        if (i == 0) {
            loadChoicesColumns(cellIterator, survey);
        } else {
            Item q = createChoice(cellIterator, survey);
            ListItem listItem = survey.getChoices().get(q.getListName());
            if (listItem == null) {
                listItem = new ListItem(q.getListName());
            }
            listItem.getListOfItems().put(q.getName(), q);
            survey.getChoices().put(listItem.getName(), listItem);
        }
        i++;
    }
    return survey;

}

From source file:org.unhcr.eg.odk.utilities.xlsform.controller.SheetProcessor.java

protected static void copyContent(Sheet sheetSource) {
    //Iterate through each rows from first sheet
    Iterator<Row> rowIterator = sheetSource.iterator();
    int i = 0;/*from w  w  w  . j a v  a2s  . co  m*/
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        i++;
        //For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        int j = 0;
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            j++;
            getCelValue(cell);
        }

    }

}

From source file:org.unhcr.eg.odk.utilities.xlsform.excel.ExcelFileUtility.java

protected static void copyContent(Sheet sheetSource, Sheet sheetDestination) {
    //Iterate through each rows from first sheet
    Iterator<Row> rowIterator = sheetSource.iterator();
    int i = 0;//from   ww w  .j a v  a2  s.  co  m
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Row rowDestination = sheetDestination.createRow(i);
        i++;
        //For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        int j = 0;
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            Cell cellDestination = rowDestination.createCell(j);
            j++;
            cellDestination.setCellComment(cell.getCellComment());
            //                cellDestination.setCellStyle(cell.getCellStyle());
            cellDestination.setCellType(cell.getCellType());
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                cellDestination.setCellValue(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    cellDestination.setCellValue(cell.getDateCellValue());
                } else {
                    cellDestination.setCellValue(cell.getNumericCellValue());
                }
                break;
            case Cell.CELL_TYPE_STRING:
                cellDestination.setCellValue(cell.getRichStringCellValue());
                break;
            case Cell.CELL_TYPE_BLANK:
                cellDestination.setCellValue(cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_ERROR:
                cellDestination.setCellValue(cell.getErrorCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                cellDestination.setCellFormula(cell.getCellFormula());
                break;
            }

        }

    }

}