Example usage for org.apache.poi.ss.usermodel Cell getColumnIndex

List of usage examples for org.apache.poi.ss.usermodel Cell getColumnIndex

Introduction

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

Prototype

int getColumnIndex();

Source Link

Document

Returns column index of this cell

Usage

From source file:org.drools.informer.load.spreadsheet.SpreadsheetData.java

License:Apache License

/**
 * Will split the sheet from the workbook up into {@link SpreadsheetRow} and {@link SpreadsheetItem}
 * //from w w w. j  a v a2s  .c o  m
 * @param sheet
 */
public SpreadsheetData(HSSFSheet sheet) {
    super();
    sheetName = sheet.getSheetName();
    for (Row row : sheet) {
        int rowNumber = row.getRowNum();
        SpreadsheetRow rowItems = new SpreadsheetRow(rowNumber);
        rows.add(rowItems);
        for (Cell cell : row) {
            if ((cell == null) || (cell.getCellType() == Cell.CELL_TYPE_BLANK)) {
                // null check is just in case - should never be!
                continue;
            }
            if ((keyColumn > 0) && (cell.getColumnIndex() < keyColumn)) {
                // comments column
                continue;
            }
            SpreadsheetItem item = new SpreadsheetItem(sheet.getSheetName(), cell);
            if (firstItemOnSheet == null) {
                // The first cell item must be sheet identifier/heading - previous columns will be treated as comments
                // and thus ignored
                firstItemOnSheet = item;
                keyColumn = cell.getColumnIndex();
            }
            String id = item.getCellIdentifier();

            //System.out.println("Sheet:" + sheet.getSheetName() + ", id=" + id + ", toString=" + item.toString());
            data.put(id, item);
            rowItems.addRowItem(item);
            cellList.add(item.getCellIdentifier());
        }
    }
}

From source file:org.drools.scorecards.parser.xls.XLSScorecardParser.java

License:Apache License

private void processSheet(HSSFSheet worksheet) throws ScorecardParseException {
    for (Row row : worksheet) {
        int currentRowCtr = row.getRowNum();
        excelDataCollector.newRow(currentRowCtr);
        for (Cell cell : row) {
            int currentColCtr = cell.getColumnIndex();
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                excelDataCollector.newCell(currentRowCtr, currentColCtr, cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    excelDataCollector.newCell(currentRowCtr, currentColCtr, cell.getDateCellValue());
                } else {
                    excelDataCollector.newCell(currentRowCtr, currentColCtr,
                            Double.valueOf(cell.getNumericCellValue()));
                }/*w ww. j  av a2 s.  co  m*/
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                excelDataCollector.newCell(currentRowCtr, currentColCtr,
                        Boolean.valueOf(cell.getBooleanCellValue()).toString());
                break;
            case Cell.CELL_TYPE_FORMULA:
                break;
            case Cell.CELL_TYPE_BLANK:
                excelDataCollector.newCell(currentRowCtr, currentColCtr, "");
                break;
            }
        }
    }
}

From source file:org.drugepi.table.ExcelUtils.java

License:Mozilla Public License

public static Cell getColumnParentCell(Sheet sheet, Row row, Cell cell) throws Exception {
    if (row.getRowNum() == 0)
        return null;

    if (cell == null)
        return null;

    Row prevRow = sheet.getRow(row.getRowNum() - 1);
    if (prevRow == null)
        return null;

    Cell parentCell = null;/* w  w w.  j  a v  a2  s.  c o m*/
    int lookupIndex = cell.getColumnIndex();
    while (lookupIndex >= 0) {
        parentCell = prevRow.getCell(lookupIndex);

        if (parentCell == null)
            break;

        if ((cellIsColumnDefinition(parentCell)) && (!cellIsEmpty(parentCell)))
            return parentCell;

        lookupIndex--;
    }

    return null;
}

From source file:org.drugepi.table.ExcelUtils.java

License:Mozilla Public License

public static Cell getRowParentCell(Sheet sheet, Row row, Cell cell) throws Exception {
    if (row.getRowNum() == 0)
        return null;

    if (cell == null)
        return null;

    int prevCol = cell.getColumnIndex() - 1;
    if (prevCol < 0)
        return null;

    Cell parentCell = null;/*from www  .  j a va2 s.  c om*/
    for (int i = row.getRowNum() - 1; i >= 0; i--) {
        Row searchRow = sheet.getRow(i);
        if (searchRow != null) {
            parentCell = searchRow.getCell(prevCol);

            if ((cellIsRowDefinition(parentCell)) && (!cellIsEmpty(parentCell)))
                return parentCell;
        }
    }

    return null;
}

From source file:org.drugepi.table.ExcelUtils.java

License:Mozilla Public License

public static String getCellId(Cell cell) throws Exception {
    if (cellIsEmpty(cell))
        return null;

    if (!cellIsStringOrBlank(cell))
        throw new Exception("Cannot operate on a non-string cell.");

    String strippedCell = stripFootnoteReference(cell);
    if (strippedCell != null)
        strippedCell = strippedCell.trim();

    String id = String.format("R%dC%d", cell.getRowIndex(), cell.getColumnIndex());
    if (cellIsEmpty(cell))
        return id;

    String[] tokens = strippedCell.split(CELL_ID_DIVIDER_REGEX);
    if (tokens.length == 2)
        return tokens[1].trim();

    return id;/*from   w w  w.ja  va  2  s.  c om*/
}

From source file:org.drugepi.table.TableCreator.java

License:Mozilla Public License

private void fillTableInSheet(Sheet sheet, Table t) throws Exception {
    Row firstRow = sheet.getRow(0);//w  w  w. ja  v  a 2  s  . c o m

    if (firstRow == null) {
        System.out.println("Sheet is empty.");
        return;
    }

    // Find four cells:
    Cell columnDefBottomLeft = null;
    Cell columnDefBottomRight = null;
    Cell rowDefTopRight = null;
    Cell rowDefBottomRight = null;

    for (Row row : sheet) {
        if (row != null) {
            for (Cell cell : row) {
                // We are iterating top to bottom, left to right
                if (ExcelUtils.cellIsColumnDefinition(cell)) {
                    // get the last cell that is on the left side
                    if ((columnDefBottomLeft == null)
                            || (cell.getColumnIndex() <= columnDefBottomLeft.getColumnIndex()))
                        columnDefBottomLeft = cell;

                    // get the last cell that is on the right side
                    if ((columnDefBottomRight == null)
                            || (cell.getColumnIndex() >= columnDefBottomRight.getColumnIndex()))
                        columnDefBottomRight = cell;

                }

                if (ExcelUtils.cellIsRowDefinition(cell)) {
                    // get the first cell that is on the right side
                    if ((rowDefTopRight == null) || (cell.getColumnIndex() > rowDefTopRight.getColumnIndex()))
                        rowDefTopRight = cell;

                    // get the last cell that is on the right side
                    if ((rowDefBottomRight == null)
                            || (cell.getColumnIndex() >= rowDefBottomRight.getColumnIndex()))
                        rowDefBottomRight = cell;
                }
            }
        }
    }

    if ((columnDefBottomLeft == null) || (columnDefBottomRight == null) || (rowDefTopRight == null)
            || (rowDefBottomRight == null))
        return;

    int rowFillStart = rowDefTopRight.getRowIndex();
    int rowFillEnd = rowDefBottomRight.getRowIndex();
    int colFillStart = columnDefBottomLeft.getColumnIndex();
    int colFillEnd = columnDefBottomRight.getColumnIndex();

    for (int rowIndex = rowFillStart; rowIndex <= rowFillEnd; rowIndex++) {
        Row row = sheet.getRow(rowIndex);
        for (int colIndex = colFillStart; colIndex <= colFillEnd; colIndex++) {
            Cell columnParent = ExcelUtils.getContainerColumnCell(sheet, rowIndex, colIndex);
            Cell rowParent = ExcelUtils.getContainerRowCell(sheet, rowIndex, colIndex);

            if ((columnParent != null) && (rowParent != null)) {
                Cell cell = row.getCell(colIndex);
                if (cell == null)
                    cell = row.createCell(colIndex);

                String colId = TableElement.makeId(ExcelUtils.getCellId(columnParent));
                String rowId = TableElement.makeId(ExcelUtils.getCellId(rowParent));
                String cellId = TableCell.getCellId(rowId, colId);

                TableCell c = t.cells.get(cellId);
                if ((c != null) && (c.description.length() > 0))
                    ExcelUtils.setCellValue(cell, c.description);
            }
        }
    }

    CellStyleLookup csl = new CellStyleLookup();
    for (Row row : sheet) {
        if (row != null) {
            for (Cell cell : row) {
                if (ExcelUtils.cellIsStringOrBlank(cell)) {
                    String id = ExcelUtils.getCellId(cell);
                    if (id != null)
                        cell.setCellValue(ExcelUtils.getCellContents(cell));
                }

                ExcelUtils.restyleCell(csl, cell);
            }
        }
    }
}

From source file:org.eclipse.emfforms.internal.spreadsheet.core.converter.EMFFormsSpreadsheetMultiAttributeConverter.java

License:Open Source License

/**
 * {@inheritDoc}//from   www  .j a  va 2  s  . c  om
 *
 * @see org.eclipse.emfforms.spi.spreadsheet.core.converter.EMFFormsSpreadsheetValueConverter#getCellValue(org.apache.poi.ss.usermodel.Cell,
 *      org.eclipse.emf.ecore.EStructuralFeature)
 */
@Override
public Object getCellValue(Cell cell, EStructuralFeature eStructuralFeature) throws EMFFormsConverterException {
    String string;
    try {
        string = cell.getStringCellValue();
    } catch (final IllegalStateException e) {
        throw new EMFFormsConverterException(
                String.format("Cell value of column %1$s in row %2$s on sheet %3$s must be a string.", //$NON-NLS-1$
                        cell.getColumnIndex() + 1, cell.getRowIndex() + 1, cell.getSheet().getSheetName()),
                e);
    }

    if (string == null || string.length() == 0) {
        return Collections.emptyList();
    }
    final EAttribute eAttribute = EAttribute.class.cast(eStructuralFeature);
    final EDataType eDataType = eAttribute.getEAttributeType();
    if (isDecimalNumber(eDataType.getInstanceClass())) {
        string = string.replace(
                DecimalFormatSymbols.getInstance(localeProvider.getLocale()).getDecimalSeparator(), '.');
    }

    final List<Object> result = new ArrayList<Object>();
    final EFactory eFactory = eDataType.getEPackage().getEFactoryInstance();
    for (final String element : string.split(SEPARATOR)) {
        try {
            result.add(eFactory.createFromString(eDataType, element));
        } // BEGIN SUPRESS CATCH EXCEPTION
        catch (final RuntimeException ex) {// END SUPRESS CATCH EXCEPTION
            throw new EMFFormsConverterException(
                    MessageFormat.format("The cell value {0} could not converted to a model value.", string)); //$NON-NLS-1$
        }
    }

    return result;
}

From source file:org.eclipse.emfforms.internal.spreadsheet.core.converter.EMFFormsSpreadsheetSingleAttributeConverter.java

License:Open Source License

/**
 * {@inheritDoc}/*from   ww w .j  a v  a  2s.c  o m*/
 *
 * @see org.eclipse.emfforms.spi.spreadsheet.core.converter.EMFFormsSpreadsheetValueConverter#getCellValue(org.apache.poi.ss.usermodel.Cell,
 *      org.eclipse.emf.ecore.EStructuralFeature)
 */
@Override
public Object getCellValue(Cell cell, EStructuralFeature eStructuralFeature) throws EMFFormsConverterException {
    final EAttribute eAttribute = EAttribute.class.cast(eStructuralFeature);
    if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
        return null;
    }
    if (eAttribute == null) {
        return null;
    }
    final EDataType attributeType = eAttribute.getEAttributeType();
    if (attributeType == null) {
        return null;
    }
    try {
        return readCellValue(cell, attributeType);
    } catch (final IllegalStateException e) {
        throw new EMFFormsConverterException(
                String.format("Cell value of column %1$s in row %2$s on sheet %3$s must be a string.", //$NON-NLS-1$
                        cell.getColumnIndex() + 1, cell.getRowIndex() + 1, cell.getSheet().getSheetName()),
                e);
    } catch (final NumberFormatException e) {
        throw new EMFFormsConverterException(
                String.format("Cell value of column %1$s in row %2$s on sheet %3$s is not a valid number.", //$NON-NLS-1$
                        cell.getColumnIndex() + 1, cell.getRowIndex() + 1, cell.getSheet().getSheetName()),
                e);
    }
}

From source file:org.eclipse.emfforms.internal.spreadsheet.core.transfer.EMFFormsSpreadsheetImporterImpl.java

License:Open Source License

private String getStringCellValue(Cell cell) {
    try {//from  w  w  w .j  a  v  a2s. co m
        return cell.getStringCellValue();
    } catch (final IllegalArgumentException ex) {
        throw new IllegalStateException(
                String.format("Cell value of column %1$s in row %2$s on sheet %3$s must be a string.", //$NON-NLS-1$
                        cell.getColumnIndex() + 1, cell.getRowIndex() + 1, cell.getSheet().getSheetName()),
                ex);
    }
}

From source file:org.efaps.esjp.common.file.FileUtil_Base.java

License:Apache License

/**
 * Copy row.//w w  w. j a  v a 2  s  .c  om
 *
 * @param _srcSheet the src sheet
 * @param _destSheet the dest sheet
 * @param _srcRow the src row
 * @param _destRow the dest row
 * @param _styleMap the style map
 */
protected void copyRow(final Sheet _srcSheet, final Sheet _destSheet, final Row _srcRow, final Row _destRow,
        final Map<Integer, CellStyle> _styleMap) {
    final Set<CellRangeAddressWrapper> mergedRegions = new TreeSet<>();
    _destRow.setHeight(_srcRow.getHeight());
    final int deltaRows = _destRow.getRowNum() - _srcRow.getRowNum();
    for (int j = _srcRow.getFirstCellNum(); j <= _srcRow.getLastCellNum(); j++) {
        final Cell oldCell = _srcRow.getCell(j); // ancienne cell
        Cell newCell = _destRow.getCell(j); // new cell
        if (oldCell != null) {
            if (newCell == null) {
                newCell = _destRow.createCell(j);
            }
            copyCell(oldCell, newCell, _styleMap);
            final CellRangeAddress mergedRegion = getMergedRegion(_srcSheet, _srcRow.getRowNum(),
                    (short) oldCell.getColumnIndex());

            if (mergedRegion != null) {
                final CellRangeAddress newMergedRegion = new CellRangeAddress(
                        mergedRegion.getFirstRow() + deltaRows, mergedRegion.getLastRow() + deltaRows,
                        mergedRegion.getFirstColumn(), mergedRegion.getLastColumn());
                final CellRangeAddressWrapper wrapper = new CellRangeAddressWrapper(newMergedRegion);
                if (isNewMergedRegion(wrapper, mergedRegions)) {
                    mergedRegions.add(wrapper);
                    _destSheet.addMergedRegion(wrapper.range);
                }
            }
        }
    }
}