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

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

Introduction

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

Prototype

int getRowIndex();

Source Link

Document

Returns row index of a row in the sheet that contains this cell

Usage

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

License:MIT License

/**
 * Parses the save attr./*www .  ja  v a 2  s .  c o  m*/
 *
 * @param cell
 *            the cell
 * @param saveCommentsMap
 *            the save comments map
 * @return the string
 */
public static String parseSaveAttr(final Cell cell, final Map<String, String> saveCommentsMap) {
    if (cell != null) {
        String key = cell.getSheet().getSheetName() + "!"
                + CellUtility.getCellIndexNumberKey(cell.getColumnIndex(), cell.getRowIndex());
        String saveAttr = null;
        if (saveCommentsMap != null) {
            saveAttr = ParserUtility.getStringBetweenBracket(saveCommentsMap.get(key));
        }
        if ((saveAttr == null) && (cell.getCellTypeEnum() == CellType.STRING)) {
            saveAttr = SaveAttrsUtility.parseSaveAttrString(cell.getStringCellValue());
        }
        if ((saveAttr != null) && (!saveAttr.isEmpty())) {
            return TieConstants.CELL_ADDR_PRE_FIX + cell.getColumnIndex() + "=" + saveAttr + ",";
        }
    }
    return "";
}

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

License:MIT License

/**
 * return full name for cell with sheet name and $ format e.g. Sheet1$A$1
 * /*w ww  .ja v  a 2s .com*/
 * @param sheet1
 *            sheet
 * @param cell
 *            cell
 * @return String full cell reference name
 */

public static String getFullCellRefName(final Sheet sheet1, final Cell cell) {
    if ((sheet1 != null) && (cell != null)) {
        return sheet1.getSheetName() + "!$" + getExcelColumnName(cell.getColumnIndex()) + "$"
                + (cell.getRowIndex() + 1);
    }
    return null;
}

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

License:MIT License

/**
 * Cell compare to.//  w w  w .  j ava2  s.c o m
 *
 * @param thisCell
 *            the this cell
 * @param otherCell
 *            the other cell
 * @return the int
 */
public static int cellCompareTo(final Cell thisCell, final Cell otherCell) {
    int r = thisCell.getRowIndex() - otherCell.getRowIndex();
    if (r != 0) {
        return r;
    }

    r = thisCell.getColumnIndex() - otherCell.getColumnIndex();
    if (r != 0) {
        return r;
    }

    return 0;
}

From source file:org.wandora.application.tools.extractors.excel.AbstractExcelExtractor.java

License:Open Source License

public Topic getCellTopic(Cell cell, TopicMap tm) throws TopicMapException {
    String cellIdentifier = null;
    switch (CELL_TOPIC_IS_BASED_ON) {
    case CELL_VALUE: {
        cellIdentifier = getCellValueAsString(cell);
        break;//from ww  w  .j a va  2 s .c  o m
    }
    case CELL_SHEET_AND_LOCATION: {
        Sheet sheet = cell.getSheet();
        String sheetName = sheet.getSheetName();
        cellIdentifier = sheetName + "-" + cell.getColumnIndex() + "-" + cell.getRowIndex();
        break;
    }
    case CELL_LOCATION: {
        cellIdentifier = cell.getColumnIndex() + "-" + cell.getRowIndex();
        break;
    }
    case CELL_HASH: {
        cellIdentifier = Integer.toString(cell.hashCode());
        break;
    }
    }
    if (cellIdentifier != null) {
        String si = EXCEL_CELL_SI_PREFIX + "/" + urlEncode(cellIdentifier);
        Topic cellTopic = getOrCreateTopic(tm, si, cellIdentifier);
        cellTopic.addType(getCellTypeTopic(tm));
        return cellTopic;
    }
    return null;
}

From source file:org.wandora.application.tools.extractors.excel.AbstractExcelExtractor.java

License:Open Source License

public Topic getRowTopic(Cell cell, TopicMap tm) throws TopicMapException {
    Topic topic = getOrCreateTopic(tm,/*from w  w w . java  2 s. co m*/
            EXCEL_ROW_SI_PREFIX + "/" + urlEncode(Integer.toString(cell.getRowIndex())),
            "Excel row " + cell.getRowIndex());
    topic.addType(getRowTypeTopic(tm));
    return topic;
}

From source file:org.wandora.application.tools.extractors.excel.ExcelAdjacencyMatrixExtractor.java

License:Open Source License

public void processRow(Row row, TopicMap tm) {
    Association a = null;//w  w w  .  j  a v  a 2  s .  c  o m

    try {
        Cell firstColumnCell = row.getCell(0);
        if (firstColumnCell != null) {
            if (getCellValueAsString(firstColumnCell) != null) {
                Topic cellTopic = getCellTopic(firstColumnCell, tm);
                rowLabels.put(Integer.toString(firstColumnCell.getRowIndex()),
                        cellTopic.getOneSubjectIdentifier().toExternalForm());
            } else {
                return;
            }
        }

        Iterator<Cell> cellIterator = row.cellIterator();
        while (cellIterator.hasNext() && !forceStop()) {
            Cell cell = cellIterator.next();
            if (cell.getColumnIndex() > 0) {
                processCell(cell, tm);
            }
        }
    } catch (TopicMapException ex) {
        log(ex);
    } catch (Exception ex) {
        log(ex);
    }
}

From source file:org.wandora.application.tools.extractors.excel.ExcelAdjacencyMatrixExtractor.java

License:Open Source License

public void processCell(Cell cell, TopicMap tm) {
    if (cell != null) {
        try {/*  ww  w  .  j  a  v a 2 s  . c  o m*/

            String rowLabel = rowLabels.get(Integer.toString(cell.getRowIndex()));
            String columnLabel = columnLabels.get(Integer.toString(cell.getColumnIndex()));
            if (rowLabel != null && columnLabel != null) {
                if (hasValue(cell)) {
                    Topic rowTopic = tm.getTopic(rowLabel);
                    Topic columnTopic = tm.getTopic(columnLabel);
                    if (rowTopic != null && columnTopic != null) {
                        Association a = tm.createAssociation(getAssociationTypeTopic(cell, tm));
                        a.addPlayer(rowTopic, getRowTypeTopic(tm));
                        a.addPlayer(columnTopic, getColumnTypeTopic(tm));
                        if (ADD_CELL_VALUE_AS_PLAYER) {
                            Topic cellTopic = getCellTopic(cell, tm);
                            Topic cellType = getCellTypeTopic(tm);
                            if (cellTopic != null && cellType != null) {
                                a.addPlayer(cellTopic, cellType);
                            }
                        }
                        if (ADD_CELL_COLOR_AS_PLAYER) {
                            Topic cellColorTopic = getColorTopic(cell, tm);
                            Topic cellType = getColorTypeTopic(tm);
                            if (cellColorTopic != null && cellType != null) {
                                a.addPlayer(cellColorTopic, cellType);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            log(e);
        }
    }
}

From source file:org.waterforpeople.mapping.dataexport.FixedFormatRawDataImporter.java

License:Open Source License

@Override
public void executeImport(File file, String serverBase, Map<String, String> criteria) {
    try {/*from   ww  w .  ja va 2 s  .  com*/
        DateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss z");
        setSurveyId(criteria);
        Sheet sheet1 = getDataSheet(file);
        for (Row row : sheet1) {
            String localeId = null;
            String dateString = null;
            StringBuilder sb = new StringBuilder();
            StringBuilder valueBuilder = new StringBuilder();
            int valueCount = 0;
            sb.append("action=" + RawDataImportRequest.SAVE_FIXED_FIELD_SURVEY_INSTANCE_ACTION + "&"
                    + RawDataImportRequest.SURVEY_ID_PARAM + "=" + getSurveyId() + "&");
            for (Cell cell : row) {

                if (cell.getColumnIndex() == 0 && cell.getRowIndex() > 0) {
                    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        localeId = new Double(cell.getNumericCellValue()).intValue() + "";
                        sb.append(RawDataImportRequest.LOCALE_ID_PARAM + "=" + localeId + "&");
                    }
                }
                if (cell.getColumnIndex() == 1 && cell.getRowIndex() > 0) {
                    if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                        dateString = cell.getStringCellValue();
                    } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
                        dateString = df.format(date);
                    }
                    if (dateString != null) {
                        sb.append(RawDataImportRequest.COLLECTION_DATE_PARAM + "="
                                + URLEncoder.encode(dateString, "UTF-8") + "&");
                    }
                }
                String value = null;
                boolean hasValue = false;

                if (cell.getRowIndex() > 0 && cell.getColumnIndex() > 1) {
                    if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                        value = cell.getStringCellValue().trim();
                        if (value.contains("|")) {
                            value = value.replaceAll("\\|", "^^");
                        }
                        sb.append(URLEncoder.encode(value, "UTF-8"));
                        hasValue = true;
                    } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        value = new Double(cell.getNumericCellValue()).toString().trim();
                        hasValue = true;
                    } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                        value = new Boolean(cell.getBooleanCellValue()).toString().trim();
                        hasValue = true;
                    }
                }
                if (hasValue) {
                    if (valueCount > 0) {
                        valueBuilder.append(RawDataImportRequest.FIELD_VAL_DELIMITER);
                    }
                    valueBuilder.append(value);
                    valueCount++;
                }
            }
            if (valueCount > 0) {
                sb.append(RawDataImportRequest.FIXED_FIELD_VALUE_PARAM + "=" + valueBuilder.toString());
                invokeUrl(serverBase, sb.toString(), true, criteria.get(KEY_PARAM));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cleanup();
    }
}

From source file:plugins.excel.client.util.ExcelReader.java

License:Microsoft Reference Source License

private Object getCellValue(Cell c, int targetType) {
    int cellType = c.getCellType();
    Object val;

    try {//www  .  j av a2 s.c  o m
        switch (cellType) {
        case (Cell.CELL_TYPE_STRING):
        case (Cell.CELL_TYPE_FORMULA):
            val = c.getStringCellValue();

            switch (targetType) {
            case Types.BOOLEAN:
                return Boolean.parseBoolean((String) val);
            case Types.DOUBLE:
                return Double.parseDouble((String) val);
            case Types.INTEGER:
                return Integer.parseInt((String) val);
            case Types.VARCHAR:
                return (String) val;
            case Types.DATE:
                SimpleDateFormat sdf = new SimpleDateFormat();
                try {
                    return sdf.parse((String) val);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            break;
        case (Cell.CELL_TYPE_NUMERIC):
            if (DateUtil.isCellDateFormatted(c)) {
                val = c.getDateCellValue();

                switch (targetType) {
                case Types.BOOLEAN:
                    return (((Date) val).getTime() > 0);
                case Types.DOUBLE:
                    return (double) ((Date) val).getTime();
                case Types.INTEGER:
                    return ((Date) val).getTime();
                case Types.VARCHAR:
                    DateFormat df = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
                    return df.format((Date) val);
                case Types.DATE:
                    return (Date) val;
                }
            } else {
                val = c.getNumericCellValue();

                switch (targetType) {
                case Types.BOOLEAN:
                    return ((double) val > 0.0);
                case Types.DOUBLE:
                    return (double) val;
                case Types.INTEGER:
                    return (long) val;
                case Types.VARCHAR:
                    return new Double((double) val).toString();
                case Types.DATE:
                    Date d = new Date();
                    d.setTime((long) val);
                    return d;
                }
            }
            break;
        case (Cell.CELL_TYPE_ERROR):
            val = c.getErrorCellValue();

            switch (targetType) {
            case Types.BOOLEAN:
                return ((int) val > 0);
            case Types.DOUBLE:
                return (double) val;
            case Types.INTEGER:
                return (int) val;
            case Types.VARCHAR:
                return new Integer((int) val).toString();
            case Types.DATE:
                Date d = new Date();
                d.setTime((long) val);
                return d;
            }
            break;
        case (Cell.CELL_TYPE_BOOLEAN):
            val = c.getBooleanCellValue();

            switch (targetType) {
            case Types.BOOLEAN:
                return (boolean) val;
            case Types.DOUBLE:
                return (double) (((boolean) val ? 1 : 0));
            case Types.INTEGER:
                return (int) (((boolean) val ? 1 : 0));
            case Types.VARCHAR:
                return new Boolean((boolean) val).toString();
            case Types.DATE:
                Date d = new Date();
                d.setTime((long) (((boolean) val ? 1 : 0)));
                return d;
            }
            break;
        }
    } catch (IllegalStateException e) {
        Dialog.msgBox(
                "Could not import cell r:" + c.getRowIndex() + " c: " + c.getColumnIndex()
                        + " because of data type errors in the sheet",
                "Import Excel File", Dialog.ERROR_MESSAGE);
    }
    return null;
}

From source file:ru.icc.cells.ssdc.DataLoader.java

License:Apache License

public CTable nextTable() {
    if (null == sheet)
        throw new IllegalStateException("The sheet is not initialized");

    CPoint refPnt = findRefPoint(sheet, rowIndex);
    if (null == refPnt)
        return null;

    CPoint endPnt = findEndPoint(sheet, refPnt.r);
    if (null == endPnt)
        return null;

    int numOfCols = endPnt.c - refPnt.c + 1;
    int numOfRows = endPnt.r - refPnt.r + 1;
    CTable table = new CTable(numOfRows, numOfCols);

    //CCell cell;
    Cell excelCell;
    Row row = null;/*from  w w  w  .  j a  va 2s .co m*/
    CellRangeAddress cellRangeAddress = null;
    boolean isCell = false;

    int refRowAdr = refPnt.r;
    int endRowAdr = endPnt.r;
    int refColAdr = refPnt.c;
    int endColAdr = endPnt.c;

    for (int i = refRowAdr; i <= endRowAdr; i++) {
        row = sheet.getRow(i);

        // TODO   ?? ? ?,  r == null
        if (null == row)
            continue;

        for (int j = refColAdr; j <= endColAdr; j++) {
            // TODO   ?? ? ?,  excelCell == null
            excelCell = row.getCell(j, Row.CREATE_NULL_AS_BLANK);

            int colAdr = excelCell.getColumnIndex() - refColAdr + 1;
            int rowAdr = excelCell.getRowIndex() - refRowAdr + 1;

            int cl = colAdr;
            int cr = colAdr;
            int rt = rowAdr;
            int rb = rowAdr;

            isCell = true;

            for (int k = 0; k < sheet.getNumMergedRegions(); k++) {
                cellRangeAddress = sheet.getMergedRegion(k);
                if (cellRangeAddress.getFirstColumn() == excelCell.getColumnIndex()
                        && cellRangeAddress.getFirstRow() == excelCell.getRowIndex()) {
                    cr = cellRangeAddress.getLastColumn() - refColAdr + 1;
                    rb = cellRangeAddress.getLastRow() - refRowAdr + 1;
                    break;
                }

                if (cellRangeAddress.getFirstColumn() <= excelCell.getColumnIndex()
                        && excelCell.getColumnIndex() <= cellRangeAddress.getLastColumn()
                        && cellRangeAddress.getFirstRow() <= excelCell.getRowIndex()
                        && excelCell.getRowIndex() <= cellRangeAddress.getLastRow()) {
                    isCell = false;
                }
            }
            if (isCell) {
                CCell cell = table.newCell();

                cell.setCl(cl);
                cell.setRt(rt);
                cell.setCr(cr);
                cell.setRb(rb);

                fillCell(cell, excelCell);
            }
        }
    }

    this.rowIndex = endPnt.r + 1;

    //  ? 
    /*
    CPoint namePnt = this.findPreviousPoint( this.sheet, TBL_NAME, refPnt.r - 1 );
    if ( null != namePnt )
    {
    row = sheet.getRow( namePnt.r);
    //excelCell = r.getCell( namePnt.c + 1 );
    excelCell = row.getCell( namePnt.c + 1, Row.CREATE_NULL_AS_BLANK );
    String name = extractCellValue( excelCell );
    //table.getContext().setName( name );
    }
            
    CPoint measurePnt = this.findPreviousPoint( this.sheet, TBL_MEASURE, refPnt.r - 1 );
    if ( null != measurePnt )
    {
    row = sheet.getRow( measurePnt.r);
    //excelCell = r.getCell( measurePnt.c + 1 );
    excelCell = row.getCell( measurePnt.c + 1, Row.CREATE_NULL_AS_BLANK );
    String measure = extractCellValue( excelCell );
    //table.getContext().setMeasure( measure );
    }
    */

    table.setSrcWorkbookFile(sourceWorkbookFile);
    table.setSrcSheetName(sheet.getSheetName());

    CellReference cellRef;
    cellRef = new CellReference(refPnt.r, refPnt.c);
    table.setSrcStartCellRef(cellRef.formatAsString());
    cellRef = new CellReference(endPnt.r, endPnt.c);
    table.setSrcEndCellRef(cellRef.formatAsString());

    return table;
}