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

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

Introduction

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

Prototype

int getRowNum();

Source Link

Document

Get row number this row represents

Usage

From source file:org.talend.dataprep.schema.xls.serialization.XlsxStreamRunnable.java

License:Open Source License

/**
 * @see Runnable#run()/* w w  w .  j av a 2s.  co  m*/
 */
@Override
public void run() {
    try {
        JsonGenerator generator = jsonFactory.createGenerator(jsonOutput);
        Workbook workbook = StreamingReader.builder() //
                .bufferSize(4096) //
                .rowCacheSize(1) //
                .open(rawContent);
        try {
            Sheet sheet = StringUtils.isEmpty(metadata.getSheetName()) ? //
                    workbook.getSheetAt(0) : workbook.getSheet(metadata.getSheetName());
            generator.writeStartArray();
            for (Row row : sheet) {
                if (limit > 0 && row.getRowNum() > limit) {
                    break;
                }
                if (!XlsSerializer.isHeaderLine(row.getRowNum(), metadata.getRowMetadata().getColumns())) {
                    generator.writeStartObject();
                    // data quality Analyzer doesn't like to not have all columns even if we don't have any values
                    // so create so field with empty value otherwise we get exceptions
                    int i = 0;
                    for (ColumnMetadata columnMetadata : metadata.getRowMetadata().getColumns()) {
                        Cell cell = row.getCell(i);
                        String cellValue = cell == null ? null : cell.getStringCellValue(); // StringUtils.EMPTY
                        generator.writeFieldName(columnMetadata.getId());
                        if (cellValue != null) {
                            generator.writeString(cellValue);
                        } else {
                            generator.writeNull();
                        }
                        i++;
                    }
                    generator.writeEndObject();
                }
            }
            generator.writeEndArray();
            generator.flush();
        } finally {
            workbook.close();
        }
    } catch (Exception e) {
        // Consumer may very well interrupt consumption of stream (in case of limit(n) use for sampling).
        // This is not an issue as consumer is allowed to partially consumes results, it's up to the
        // consumer to ensure data it consumed is consistent.
        LOG.debug("Unable to continue serialization for {}. Skipping remaining content.", metadata.getId(), e);
    } finally {
        try {
            jsonOutput.close();
        } catch (IOException e) {
            LOG.error("Unable to close output", e);
        }
    }
}

From source file:org.teiid.translator.excel.BaseExcelExecution.java

License:Apache License

private Iterator<Row> readXLSFile(VirtualFile xlsFile) throws TranslatorException {
    try (InputStream xlsFileStream = xlsFile.openInputStream(true)) {
        String extension = ExcelMetadataProcessor.getFileExtension(xlsFile);
        if (extension.equalsIgnoreCase("xls")) { //$NON-NLS-1$
            workbook = new HSSFWorkbook(xlsFileStream);
        } else if (extension.equalsIgnoreCase("xlsx")) { //$NON-NLS-1$
            workbook = new XSSFWorkbook(xlsFileStream);
        } else {//from  ww w  .  ja v  a2  s  . c  om
            throw new TranslatorException(ExcelPlugin.Event.TEIID23000,
                    ExcelPlugin.Util.gs(ExcelPlugin.Event.TEIID23000));
        }
        Sheet sheet = workbook.getSheet(this.visitor.getSheetName());
        this.evaluator = workbook.getCreationHelper().createFormulaEvaluator();
        Iterator<Row> rowIter = sheet.iterator();

        // skip up to the first data row
        if (this.visitor.getFirstDataRowNumber() > 0 && rowIter != null) {
            while (rowIter.hasNext()) {
                Row row = rowIter.next();
                if (row.getRowNum() >= this.visitor.getFirstDataRowNumber()) {
                    this.currentRow = row;
                    break;
                }
            }
        }
        return rowIter;
    } catch (IOException e) {
        throw new TranslatorException(e);
    }
}

From source file:org.teiid.translator.excel.BaseExcelExecution.java

License:Apache License

public Row nextRow() throws TranslatorException, DataNotAvailableException {
    while (true) {
        Row row = nextRowInternal();
        if (row == null) {
            return null;
        }//from   w w  w .j av a 2  s .c  om

        // when the first cell number is -1, then it is empty row, skip it
        if (row.getFirstCellNum() == -1) {
            continue;
        }

        if (!this.visitor.allows(row.getRowNum())) {
            continue;
        }
        return row;
    }
}

From source file:org.teiid.translator.excel.ExcelExecution.java

License:Open Source License

@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
    while (hasNext()) {
        Row row = nextRow();
        // when the first cell number is -1, then it is empty row, skip it
        if (row.getFirstCellNum() == -1) {
            continue;
        }//from  w  ww . jav a 2 s  .c  o m

        if (!this.visitor.allows(row.getRowNum())) {
            continue;
        }
        return projectRow(row);
    }
    return null;
}

From source file:org.teiid.translator.excel.ExcelExecution.java

License:Open Source License

/**
 * @param row//w  w  w .  j  a va2s.co  m
 * @param neededColumns
 */
List<Object> projectRow(Row row) throws TranslatorException {
    ArrayList output = new ArrayList();

    int id = row.getRowNum() + 1;

    int i = -1;
    for (int index : this.visitor.getProjectedColumns()) {

        i++;
        // check if the row is ROW_ID
        if (index == -1) {
            output.add(id);
            continue;
        }

        Cell cell = row.getCell(index - 1, Row.RETURN_BLANK_AS_NULL);
        if (cell == null) {
            output.add(null);
            continue;
        }
        switch (this.evaluator.evaluateInCell(cell).getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            output.add(convertFromExcelType(cell.getNumericCellValue(), cell, this.expectedColumnTypes[i]));
            break;
        case Cell.CELL_TYPE_STRING:
            output.add(convertFromExcelType(cell.getStringCellValue(), this.expectedColumnTypes[i]));
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            if (this.expectedColumnTypes[i].isAssignableFrom(Boolean.class)) {
                output.add(Boolean.valueOf(cell.getBooleanCellValue()));
            } else {
                throw new TranslatorException(ExcelPlugin.Event.TEIID23001, ExcelPlugin.Util
                        .gs(ExcelPlugin.Event.TEIID23001, this.expectedColumnTypes[i].getName()));
            }
            break;
        default:
            output.add(null);
            break;
        }
    }

    return output;
}

From source file:org.teiid.translator.excel.ExcelUpdateExecution.java

License:Apache License

private void handleDelete() throws TranslatorException {
    while (true) {
        Row row = nextRow();
        if (row == null) {
            break;
        }//from  w  w w.  j a  va 2 s  .  c  o m
        this.rowIterator = null;
        int start = row.getRowNum();
        Sheet sheet = row.getSheet();
        int end = sheet.getLastRowNum();
        //a different iteration style is needed, which will not perform as well for sparse documents
        for (int i = start; i <= end; i++) {
            row = sheet.getRow(i);
            if (row == null) {
                continue;
            }
            if (row.getFirstCellNum() == -1) {
                continue;
            }

            if (!this.visitor.allows(row.getRowNum())) {
                continue;
            }
            sheet.removeRow(row);
            result++;
            modified = true;
        }
    }
}

From source file:org.tiefaces.components.websheet.configuration.ConfigurationHandler.java

License:MIT License

/**
 * Gets the sheet configuration./*  w w w.  java2 s . c  om*/
 *
 * @param sheet
 *            the sheet
 * @param formName
 *            the form name
 * @param sheetRightCol
 *            the sheet right col
 * @return the sheet configuration
 */
private SheetConfiguration getSheetConfiguration(final Sheet sheet, final String formName,
        final int sheetRightCol) {

    SheetConfiguration sheetConfig = new SheetConfiguration();
    sheetConfig.setFormName(formName);
    sheetConfig.setSheetName(sheet.getSheetName());
    int leftCol = sheet.getLeftCol();
    int lastRow = sheet.getLastRowNum();
    int firstRow = sheet.getFirstRowNum();
    int rightCol = 0;
    int maxRow = 0;
    for (Row row : sheet) {
        if (row.getRowNum() > TieConstants.TIE_WEB_SHEET_MAX_ROWS) {
            break;
        }
        maxRow = row.getRowNum();
        int firstCellNum = row.getFirstCellNum();
        if (firstCellNum >= 0 && firstCellNum < leftCol) {
            leftCol = firstCellNum;
        }
        if ((row.getLastCellNum() - 1) > rightCol) {
            int verifiedcol = verifyLastCell(row, rightCol, sheetRightCol);
            if (verifiedcol > rightCol) {
                rightCol = verifiedcol;
            }
        }
    }
    if (maxRow < lastRow) {
        lastRow = maxRow;
    }
    // header range row set to 0 while column set to first column to
    // max
    // column (FF) e.g. $A$0 : $FF$0
    String tempStr = TieConstants.CELL_ADDR_PRE_FIX + WebSheetUtility.getExcelColumnName(leftCol)
            + TieConstants.CELL_ADDR_PRE_FIX + "0 : " + TieConstants.CELL_ADDR_PRE_FIX
            + WebSheetUtility.getExcelColumnName(rightCol) + TieConstants.CELL_ADDR_PRE_FIX + "0";
    sheetConfig.setFormHeaderRange(tempStr);
    sheetConfig.setHeaderCellRange(new CellRange(tempStr));
    // body range row set to first row to last row while column set
    // to
    // first column to max column (FF) e.g. $A$1 : $FF$1000
    tempStr = TieConstants.CELL_ADDR_PRE_FIX + WebSheetUtility.getExcelColumnName(leftCol)
            + TieConstants.CELL_ADDR_PRE_FIX + (firstRow + 1) + " : " + TieConstants.CELL_ADDR_PRE_FIX
            + WebSheetUtility.getExcelColumnName(rightCol) + TieConstants.CELL_ADDR_PRE_FIX + (lastRow + 1);
    sheetConfig.setFormBodyRange(tempStr);
    sheetConfig.setBodyCellRange(new CellRange(tempStr));
    sheetConfig.setFormBodyType(org.tiefaces.common.TieConstants.FORM_TYPE_FREE);
    sheetConfig.setCellFormAttributes(new HashMap<String, List<CellFormAttributes>>());

    // check it's a hidden sheet
    int sheetIndex = parent.getWb().getSheetIndex(sheet);
    if (parent.getWb().isSheetHidden(sheetIndex) || parent.getWb().isSheetVeryHidden(sheetIndex)) {
        sheetConfig.setHidden(true);
    }

    return sheetConfig;

}

From source file:org.tiefaces.components.websheet.configuration.ConfigurationHandler.java

License:MIT License

/**
 * Create sheet configuration from form command.
 *
 * @param sheet//from  ww w.ja va  2s  . co  m
 *            sheet.
 * @param fcommand
 *            form command.
 * @param sheetRightCol
 *            the sheet right col
 * @return sheet configuration.
 */
private SheetConfiguration getSheetConfigurationFromConfigCommand(final Sheet sheet, final FormCommand fcommand,
        final int sheetRightCol) {

    SheetConfiguration sheetConfig = new SheetConfiguration();
    sheetConfig.setFormName(fcommand.getName());
    sheetConfig.setSheetName(sheet.getSheetName());
    int leftCol = fcommand.getLeftCol();
    int lastRow = fcommand.getLastRow();
    int rightCol = 0;
    int maxRow = 0;
    for (Row row : sheet) {
        if (row.getRowNum() > TieConstants.TIE_WEB_SHEET_MAX_ROWS) {
            break;
        }
        maxRow = row.getRowNum();
        if ((row.getLastCellNum() - 1) > rightCol) {
            int verifiedcol = verifyLastCell(row, rightCol, sheetRightCol);
            if (verifiedcol > rightCol) {
                rightCol = verifiedcol;
            }
        }
    }
    if (maxRow < lastRow) {
        lastRow = maxRow;
    }
    // header range row set to 0 while column set to first column to
    // max
    // column (FF) e.g. $A$0 : $FF$0
    setHeaderOfSheetConfiguration(fcommand, sheetConfig, leftCol, rightCol);
    // body range row set to first row to last row while column set
    // to
    // first column to max column (FF) e.g. $A$1 : $FF$1000
    setBodyOfSheetConfiguration(fcommand, sheetConfig, leftCol, lastRow, rightCol);

    // footer range row set to 0 while column set to first column to
    // max
    // column (FF) e.g. $A$0 : $FF$0
    setFooterOfSheetConfiguration(fcommand, sheetConfig, leftCol, rightCol);

    String hidden = fcommand.getHidden();
    if ((hidden != null) && (Boolean.parseBoolean(hidden))) {
        sheetConfig.setHidden(true);
    }
    String fixedWidthStyle = fcommand.getFixedWidthStyle();
    if ((fixedWidthStyle != null) && (Boolean.parseBoolean(fixedWidthStyle))) {
        sheetConfig.setFixedWidthStyle(true);
    }
    sheetConfig.setFormCommand(fcommand);
    return sheetConfig;

}

From source file:org.tiefaces.components.websheet.utility.CellUtility.java

License:MIT License

/**
 * Copy single row.//from w  w  w .  j a  v a  2s  . c o  m
 *
 * @param srcSheet
 *            the src sheet
 * @param destSheet
 *            the dest sheet
 * @param sourceRowNum
 *            the source row num
 * @param destinationRowNum
 *            the destination row num
 * @param checkLock
 *            the check lock
 * @param setHiddenColumn
 *            the set hidden column
 */
private static void copySingleRow(final Sheet srcSheet, final Sheet destSheet, final int sourceRowNum,
        final int destinationRowNum, final boolean checkLock, final boolean setHiddenColumn) {
    // Get the source / new row
    Row newRow = destSheet.getRow(destinationRowNum);
    Row sourceRow = srcSheet.getRow(sourceRowNum);

    if (newRow == null) {
        newRow = destSheet.createRow(destinationRowNum);
    }
    newRow.setHeight(sourceRow.getHeight());
    // Loop through source columns to add to new row
    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        // Grab a copy of the old/new cell
        copyCell(destSheet, sourceRow, newRow, i, checkLock);
    }
    if (setHiddenColumn) {
        ConfigurationUtility.setOriginalRowNumInHiddenColumn(newRow, sourceRow.getRowNum());
    }
    return;

}

From source file:org.tiefaces.components.websheet.utility.CommandUtility.java

License:MIT License

/**
 * Checks if is row allow add./*ww w  . java  2s  .  c o  m*/
 *
 * @param row
 *            the row
 * @param sheetConfig
 *            the sheet config
 * @return true, if is row allow add
 */
public static boolean isRowAllowAdd(final Row row, final SheetConfiguration sheetConfig) {
    String fullName = ConfigurationUtility.getFullNameFromRow(row);
    if (fullName != null) {
        ConfigRangeAttrs attrs = sheetConfig.getShiftMap().get(fullName);
        if ((attrs != null) && (attrs.isAllowAdd())
                && (row.getRowNum() == attrs.getFirstRowRef().getRowIndex())) {
            return true;
        }
    }
    return false;
}