Example usage for org.apache.poi.hssf.record RowRecord getRowNumber

List of usage examples for org.apache.poi.hssf.record RowRecord getRowNumber

Introduction

In this page you can find the example usage for org.apache.poi.hssf.record RowRecord getRowNumber.

Prototype

public int getRowNumber() 

Source Link

Document

get the logical row number for this row (0 based index)

Usage

From source file:com.netxforge.netxstudio.screens.f4.support.XLSService.java

License:Open Source License

/**
 * This method listens for incoming records and handles them as required.
 * //  w  ww.  j  a  v a  2 s  .  com
 * @param record
 *            The record that was found while reading.
 */
private int processRecordInternal(Record record) {

    if (currentMonitor.isCanceled()) {
        // we should interrupt the process here.
        return ABORTED;
    }

    // Produce a multiple kvp for the .xls

    switch (record.getSid()) {
    // the BOFRecord can represent either the beginning of a sheet or the
    // workbook
    case BOFRecord.sid:
        BOFRecord bof = (BOFRecord) record;
        if (bof.getType() == BOFRecord.TYPE_WORKBOOK) {
            System.out.println("Encountered workbook");
            // assigned to the class level member
        } else if (bof.getType() == BOFRecord.TYPE_WORKSHEET) {

            currentRecordMap = Lists.newArrayListWithExpectedSize(32);
            // fill the list with at least x map entries.
            for (int i = 0; i < MAX_VISIBLE_ROWS; i++) {
                Map<Integer, Tuple> map = Maps.newHashMap();
                currentRecordMap.add(map);
            }

            sheets.add(currentRecordMap);
            System.out.println("Encountered sheet reference, changing sheet...");
        }
        break;
    case BoundSheetRecord.sid:
        BoundSheetRecord bsr = (BoundSheetRecord) record;
        System.out.println("New sheet named: " + bsr.getSheetname());

        break;

    // Row records come in batch (32) before the actual cell records.

    case RowRecord.sid:

        RowRecord rowrec = (RowRecord) record;
        // Look for our header row, when found we have to interpret the
        // values.

        System.out.println("Row found" + rowrec.getRowNumber() + ", first column at " + rowrec.getFirstCol()
                + " last column at " + rowrec.getLastCol());
        break;

    // SSTRecords store a array of unique strings used in Excel.
    case SSTRecord.sid:
        currentSStrec = (SSTRecord) record;
        // for (int k = 0; k < currentSStrec.getNumUniqueStrings(); k++) {
        // System.out.println("String table value " + k + " = "
        // + currentSStrec.getString(k));
        // }
        break;
    case DateWindow1904Record.sid: {
        System.out.println("Hitting Date record. ");

    }
        break;
    case NumberRecord.sid:
    case LabelSSTRecord.sid:

        int column = -1;
        int row = -1;
        Object value = null;

        if (record.getSid() == NumberRecord.sid) {
            NumberRecord numrec = (NumberRecord) record;

            double numValue = numrec.getValue();
            column = numrec.getColumn();
            row = numrec.getRow();

            value = this.formatNumberDateCell(numrec, numValue);

            // DEBUG
            System.out.println("Number:Cell found with value " + value + " at: [" + row + "," + column + "]");

        }
        if (record.getSid() == LabelSSTRecord.sid) {
            LabelSSTRecord lrec = (LabelSSTRecord) record;
            value = currentSStrec.getString(lrec.getSSTIndex()).toString();
            column = lrec.getColumn();
            row = lrec.getRow();

            // DEBUG
            System.out.println("String:Cell found with value " + value + " at: [" + row + "," + column + "]");
        }

        if (value != null && row != -1 && column != -1) {
            Tuple t = new Tuple(column, value);

            if (currentRecordMap.size() < row) {
                Map<Integer, Tuple> map = Maps.newHashMap();
                currentRecordMap.add(map);
            }
            currentRecordMap.get(row).put(column, t);

        } else {
            System.err.println("Incomplete cell encountered" + "v=" + value + " r=" + row + " c=" + column);
        }
        // TODO, do a more gracefull check with a switch break to proceed,
        // or other strategy if this fails.
        // assert row > 0 && column > 0 && value != null;

        if (column == 0) {
            currentMonitor.worked(1);
            currentRowProgress = true; // reset for the next row.
            processedRows++; // Update the number of processed rows.

        } else {
            // Did we have a failure on a previous column, if so skip this
            // column until the next row.
            if (!currentRowProgress) {
                break;
            }
            // We have an undefined column.
            this.currentRowProgress = false;

        }

        break;
    }
    return OK;
}

From source file:org.tonguetied.datatransfer.importing.ExcelKeywordParser.java

License:Apache License

public void processRecord(Record record) {
    if (record == null) {
        if (logger.isInfoEnabled())
            logger.info("no record to process");
    } else {/*from www.j  av  a2s  .  c  om*/
        switch (record.getSid()) {
        // the BOFRecord can represent either the beginning of a sheet 
        // or the workbook
        case BOFRecord.sid:
            if (!(record instanceof BOFRecord))
                throw new ImportException("unknown excel element", null);
            final BOFRecord bof = (BOFRecord) record;
            if (bof.getType() == BOFRecord.TYPE_WORKBOOK) {
                if (logger.isInfoEnabled())
                    logger.info("Processing excel workbook");
                // assigned to the class level member
            } else if (bof.getType() == BOFRecord.TYPE_WORKSHEET) {
                if (logger.isInfoEnabled())
                    logger.info("recordsize = " + bof.getRecordSize() + ", required version = "
                            + bof.getRequiredVersion());
            }
            break;
        case BoundSheetRecord.sid:
            if (!(record instanceof BoundSheetRecord))
                throw new ImportException("unknown excel element", null);
            final BoundSheetRecord bsr = (BoundSheetRecord) record;
            // sheets named have no impact on generating query
            if (logger.isDebugEnabled())
                logger.debug("processing sheet: " + bsr.getSheetname());
            break;
        case RowRecord.sid:
            if (!(record instanceof RowRecord))
                throw new ImportException("unknown excel element", null);
            if (logger.isDebugEnabled()) {
                final RowRecord rowrec = (RowRecord) record;
                logger.debug("processing row: " + rowrec.getRowNumber());
            }
            break;
        case NumberRecord.sid:
            if (!(record instanceof NumberRecord))
                throw new ImportException("unknown excel element", null);
            final NumberRecord numrec = (NumberRecord) record;
            logger.warn("Cell [" + numrec.getRow() + "," + numrec.getColumn()
                    + "] expecting a string value not numeric: " + numrec.getValue() + ". Ignoring value");
            break;
        case SSTRecord.sid:
            if (!(record instanceof SSTRecord))
                throw new ImportException("unknown excel element", null);
            // SSTRecords store a array of unique strings used in Excel.
            sstrec = (SSTRecord) record;
            if (logger.isDebugEnabled()) {
                logger.debug("file contains " + sstrec.getNumUniqueStrings() + " unique strings");
            }
            break;
        case LabelSSTRecord.sid:
            if (!(record instanceof LabelSSTRecord))
                throw new ImportException("unknown excel element", null);
            final LabelSSTRecord lrec = (LabelSSTRecord) record;
            if (lrec.getRow() != 0) {
                if (lrec.getColumn() == 0) {
                    evaluateRowType(lrec);
                } else {
                    final String cellValue = sstrec.getString(lrec.getSSTIndex()).getString();
                    if (lrec.getColumn() == 1) {
                        switch (rowType) {
                        case keyword:
                            // there were no translations for the previous keyword, so add to keywords
                            if (keyword != null && keyword.getTranslations().isEmpty())
                                keywords.put(keyword.getKeyword(), keyword);
                            loadKeyword(cellValue);
                            break;
                        case context:
                            if (StringUtils.isNotBlank(cellValue))
                                keyword.setContext(cellValue);
                            break;
                        default:
                            break;
                        }
                    } else if (lrec.getColumn() == 2) {
                        baseTranslation = new Translation();
                        baseTranslation.setKeyword(keyword);
                        final LanguageCode code = ImporterUtils.evaluateLanguageCode(cellValue, errorCodes);
                        Language language = null;
                        if (code != null) {
                            language = keywordService.getLanguage(code);
                            if (language == null)
                                errorCodes.add(ImportErrorCode.unknownLanguage);
                        }
                        baseTranslation.setLanguage(language);
                    } else if (lrec.getColumn() == 4) {
                        final CountryCode code = ImporterUtils.evaluateCountryCode(cellValue, errorCodes);
                        Country country = null;
                        if (code != null) {
                            country = keywordService.getCountry(code);
                            if (country == null)
                                errorCodes.add(ImportErrorCode.unknownCountry);
                        }
                        baseTranslation.setCountry(country);
                    } else if (lrec.getColumn() == 6) {
                        final Bundle bundle = keywordService.getBundleByName(cellValue);
                        if (bundle == null)
                            errorCodes.add(ImportErrorCode.unknownBundle);
                        baseTranslation.setBundle(bundle);
                    } else if (lrec.getColumn() == 7) {
                        final TranslationState state = ImporterUtils.evaluateTranslationState(cellValue,
                                errorCodes);
                        baseTranslation.setState(state);
                    } else if (lrec.getColumn() == 8) {
                        baseTranslation.setValue(cellValue);
                        keyword.addTranslation(baseTranslation);
                        keywords.put(keyword.getKeyword(), keyword);
                    }
                }

            }
            break;
        default:
            break;
        }
    }
}

From source file:util.XLSReader.java

License:Open Source License

/**
 * This method listens for incoming records and handles them as required.
 * /* ww  w .jav  a2  s  .c  o  m*/
 * @param record
 */
public void processRecord(final Record record) {
    switch (record.getSid()) {

    // the BOFRecord can represent either the beginning of a sheet or the workbook
    case BOFRecord.sid:
        final BOFRecord bof = (BOFRecord) record;
        if (bof.getType() == BOFRecord.TYPE_WORKBOOK) {
            LOG.debug("Encountered workbook");
            // assigned to the class level member
        } else if (bof.getType() == BOFRecord.TYPE_WORKSHEET) {
            LOG.debug("Encountered sheet reference");
        }
        break;
    case BoundSheetRecord.sid:
        final BoundSheetRecord bsr = (BoundSheetRecord) record;
        LOG.debug("New sheet named: " + bsr.getSheetname());
        break;
    case RowRecord.sid:
        final RowRecord rowrec = (RowRecord) record;
        // counts for all rows
        LOG.debug("Row at " + rowrec.getRowNumber() + " found, first column at " + rowrec.getFirstCol()
                + " last column at " + rowrec.getLastCol() + ". Total number of columns: "
                + rowrec.getRecordSize());
        break;
    case NumberRecord.sid:
        // cells with numbers
        final NumberRecord numrec = (NumberRecord) record;
        if (getLastrow() < numrec.getRow()) { // start of a new record / row
            if (numrec.getRow() > 0) { // put the old record / row into result
                // make a copy, because the reference will be reused
                final ArrayList<String> copy = new ArrayList<String>();
                setLastcol(-1); // reset column count
                copy.addAll(rowcontent);
                result.add(copy);
            }
            rowcontent = new ArrayList<String>();
        }

        // set current row number
        setLastrow(numrec.getRow());

        // add empty cell contents for the missing cells
        addEmptyCells(numrec.getColumn() - getLastcol());

        // set current column number
        setLastcol(numrec.getColumn());

        // add cell content into rowcontent...
        if (numrec.getColumn() == 3 || numrec.getColumn() == 4) {
            //... with possible float point numbers for Location Name (3) and Shelfmark (4)...
            rowcontent.add(String.valueOf(numrec.getValue()));
        } else {
            //.. and without float point numbers for all other cells...
            rowcontent.add(String.valueOf(new Double(numrec.getValue()).longValue()));
        }

        break;
    // SSTRecords store a array of unique strings used in Excel.
    case SSTRecord.sid:
        sstrec = (SSTRecord) record;
        for (int k = 0; k < sstrec.getNumUniqueStrings(); k++) {
            LOG.debug("String table value " + k + " = " + sstrec.getString(k));
        }
        break;
    case LabelSSTRecord.sid:
        // cells with strings
        final LabelSSTRecord lrec = (LabelSSTRecord) record;
        if (getLastrow() < lrec.getRow()) { // start of a new record / row
            if (lrec.getRow() > 0) { // put the old record / row into result
                // make a copy, because the reference will be reused
                final ArrayList<String> copy = new ArrayList<String>();
                setLastcol(-1); // reset column count
                copy.addAll(rowcontent);
                result.add(copy);
            }
            rowcontent = new ArrayList<String>();
        }

        // set current row number
        setLastrow(lrec.getRow());

        // add empty cell contents for the missing cells
        addEmptyCells(lrec.getColumn() - getLastcol());

        // set current column number
        setLastcol(lrec.getColumn());

        // add cell content into rowcontent
        rowcontent.add(sstrec.getString(lrec.getSSTIndex()).toString());

        break;
    }
}