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

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

Introduction

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

Prototype

short getLastCellNum();

Source Link

Document

Gets the index of the last cell contained in this row PLUS ONE.

Usage

From source file:dk.cubing.liveresults.uploader.parser.WcaParser.java

License:Open Source License

/**
 * @param sheet/*  ww w.  j  ava 2s .  c om*/
 * @throws IllegalStateException
 */
private void parseEventNames(Sheet sheet) throws IllegalStateException {
    if (eventNames.isEmpty()) {
        Row row = sheet.getRow(2);
        if (row != null) {
            for (int i = 7; i < row.getLastCellNum(); i++) {
                Cell cell = row.getCell(i);
                if (cell != null && cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    String eventName = StringUtil.ucfirst(cell.getStringCellValue());
                    log.debug("Event name: {}", eventName);
                    eventNames.add(eventName);
                }
            }
        }
    }
}

From source file:ec.edu.chyc.manejopersonal.managebean.ExcelCustomExporter.java

License:Apache License

@Override
protected void addColumnValue(Row row, UIComponent component, String type) {
    int cellIndex = row.getLastCellNum() == -1 ? 0 : row.getLastCellNum();
    Cell cell = row.createCell(cellIndex);

    String value = "";
    if (component != null && component instanceof UIPanel) {

        for (UIComponent childComponent : component.getChildren()) {
            if (childComponent.isRendered()) {
                String valChild = exportValue(FacesContext.getCurrentInstance(), childComponent);

                if (valChild != null && !valChild.isEmpty()) {
                    valChild = valChild.replace("<br/>", " ");
                    value += valChild + "\n";
                }/*from  w  ww  .j  a  v  a2 s . c o m*/
            }
        }
    } else {
        value = component == null ? "" : exportValue(FacesContext.getCurrentInstance(), component);
    }

    cell.setCellValue(new XSSFRichTextString(value));
    if (type.equalsIgnoreCase("facet")) {
        addFacetAlignments(component, cell);
    } else {
        addColumnAlignments(component, cell);
    }

}

From source file:ec.edu.chyc.manejopersonal.managebean.ExcelCustomExporter.java

License:Apache License

@Override
protected void addColumnValue(Row row, List<UIComponent> components, String type) {
    int cellIndex = row.getLastCellNum() == -1 ? 0 : row.getLastCellNum();
    Cell cell = row.createCell(cellIndex);
    StringBuilder builder = new StringBuilder();
    FacesContext context = FacesContext.getCurrentInstance();

    for (UIComponent component : components) {
        if (component.isRendered()) {
            if (component instanceof HtmlPanelGroup) {
                for (UIComponent childComponent : component.getChildren()) {
                    if (component.isRendered()) {
                        String valChild = exportValue(context, childComponent);

                        if (valChild != null && !valChild.isEmpty()) {
                            valChild = valChild.replace("<br/>", " ");
                            builder.append(valChild).append("\n");
                        }/*  w w  w  .j  a  va 2s.  c o  m*/
                    }
                }
            } else {
                String value = exportValue(context, component);

                if (value != null) {
                    builder.append(value);
                }
            }
        }
    }

    cell.setCellValue(new XSSFRichTextString(builder.toString()));

    if (type.equalsIgnoreCase("facet")) {
        for (UIComponent component : components) {
            addFacetAlignments(component, cell);
        }
    } else {
        for (UIComponent component : components) {
            addColumnAlignments(component, cell);
        }
    }

}

From source file:ec.util.spreadsheet.poi.PoiSheet.java

License:EUPL

public PoiSheet(@Nonnull Sheet sheet) {
    this.sheet = sheet;
    this.flyweightCell = new PoiCell();
    int maxRow = 0;
    int maxColumn = 0;
    Iterator<Row> rowIterator = sheet.rowIterator();
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        maxRow = row.getRowNum() + 1;/*from   ww w . j a va 2s .  c  o  m*/
        short lastCellNum = row.getLastCellNum();
        if (lastCellNum > maxColumn) {
            maxColumn = lastCellNum;
        }
    }
    this.rowCount = maxRow;
    this.columnCount = maxColumn;
}

From source file:edu.isi.karma.imp.excel.ToCSV.java

License:Apache License

/**
 * Called to convert a row of cells into a line of data that can later be
 * output to the CSV file./* w w  w  . j a v a2s .c o  m*/
 * 
 * @param row
 *            An instance of either the HSSFRow or XSSFRow classes that
 *            encapsulates information about a row of cells recovered from
 *            an Excel workbook.
 */
private void rowToCSV(Row row) {
    Cell cell;
    int lastCellNum;
    ArrayList<String> csvLine = new ArrayList<>();

    // Check to ensure that a row was recovered from the sheet as it is
    // possible that one or more rows between other populated rows could be
    // missing - blank. If the row does contain cells then...
    if (row != null) {

        // Get the index for the right most cell on the row and then
        // step along the row from left to right recovering the contents
        // of each cell, converting that into a formatted String and
        // then storing the String into the csvLine ArrayList.
        lastCellNum = row.getLastCellNum();
        for (int i = 0; i <= lastCellNum; i++) {
            cell = row.getCell(i);
            if (cell == null) {
                csvLine.add("");
            } else {
                if (cell.getCellType() != Cell.CELL_TYPE_FORMULA) {
                    csvLine.add(this.formatter.formatCellValue(cell).replace("\n", " ").replace("\r", " "));
                } else {
                    csvLine.add(this.formatter.formatCellValue(cell, this.evaluator).replace("\n", " ")
                            .replace("\r", " "));
                }
            }
        }
        // Make a note of the index number of the right most cell. This
        // value
        // will later be used to ensure that the matrix of data in the CSV
        // file
        // is square.
        if (lastCellNum > this.maxRowWidth) {
            this.maxRowWidth = lastCellNum;
        }
    }
    this.csvData.add(csvLine);
}

From source file:edu.si.services.beans.excel.ExcelToCSV.java

License:Apache License

/**
 * Called to convert a row of cells into a line of data that can later be
 * output to the CSV file./* w w w  .  j a va2 s .c  o  m*/
 *
 * @param row An instance of either the HSSFRow or XSSFRow classes that
 *            encapsulates information about a row of cells recovered from
 *            an Excel workbook.
 */
private void rowToCSV(Row row) {
    Cell cell = null;
    int lastCellNum = 0;
    ArrayList<String> csvLine = new ArrayList<String>();

    logger.debug("Converting row content to CSV format.");

    // Check to ensure that a row was recovered from the sheet as it is
    // possible that one or more rows between other populated rows could be
    // missing - blank. If the row does contain cells then...
    if (row != null) {
        // Get the index for the right most cell on the row and then
        // step along the row from left to right recovering the contents
        // of each cell, converting that into a formatted String and
        // then storing the String into the csvLine ArrayList.
        lastCellNum = row.getLastCellNum();
        for (int i = 0; i <= lastCellNum; i++) {
            cell = row.getCell(i);
            if (cell == null) {
                csvLine.add("");
            } else {
                if (cell.getCellType() != Cell.CELL_TYPE_FORMULA) {
                    csvLine.add(this.formatter.formatCellValue(cell));
                } else {
                    csvLine.add(this.formatter.formatCellValue(cell, this.evaluator));
                }
            }
        }

        // Make a note of the index number of the right most cell. This value
        // will later be used to ensure that the matrix of data in the CSV file
        // is square.
        if (lastCellNum > this.maxRowWidth) {
            this.maxRowWidth = lastCellNum;
        }
    }
    this.csvData.add(csvLine);
}

From source file:edu.si.sidora.excel2tabular.FilteredSheet.java

License:Apache License

/**
 * Examine a sheet for data rows and record the results.
 *//*from ww  w .  j av  a2 s.  c o m*/
private void findDataRows() {
    initializing = true;

    // begin by assuming that all rows might be data rows
    final int lastRowIndex = sheet.getLastRowNum();
    final int firstRowIndex = sheet.getFirstRowNum();
    dataRange = closed(firstRowIndex, lastRowIndex);

    // Because the rows in a sheet are not ordered by length, we will have to traverse all of them to find a row
    // of maximum length. This gives us an opportunity to record any blank rows at the same time.
    final Row maximalRow = compareByLengthAndRecordBlankRows.max(this);
    final int maximalRowIndex = maximalRow.getRowNum();

    if (isBlankRow(maximalRow)) {
        log.trace("The maximal row was empty, so this sheet has no data.");
        dataRange = EMPTY_RANGE;
        return;
    }
    log.trace("Found index of maximally long data row at: {} with length: {}", maximalRowIndex,
            maximalRow.getLastCellNum());
    // search for nearest blank row after the maximal row
    final Integer nextBlankRowIndex = blankRows.higher(maximalRowIndex);
    final int lastDataRowIndex = nextBlankRowIndex == null ? lastRowIndex : nextBlankRowIndex - 1;

    // search for nearest blank row before the maximal row
    final Integer previousBlankRowIndex = blankRows.lower(maximalRowIndex);
    final int firstDataRowIndex = previousBlankRowIndex == null ? firstRowIndex : previousBlankRowIndex + 1;
    dataRange = closed(firstDataRowIndex, lastDataRowIndex);
    log.trace("Found data range: {}", dataRange);
    initializing = false;
    initialized = true;
}

From source file:edu.si.sidora.excel2tabular.TabularRow.java

License:Apache License

/**
 * @param row/*from   w w w .j a  v  a2  s.com*/
 * @param quote
 * @param delimiter
 */
public TabularRow(final Row row, final String quote, final String delimiter) {
    this.row = row;
    this.numCells = row.getLastCellNum();
    this.quote = quote;
    this.joiner = Joiner.on(delimiter);
}

From source file:eionet.gdem.conversion.excel.reader.ExcelReader.java

License:Mozilla Public License

/**
 * Check if row is empty or not./* www. java  2 s  .  c om*/
 *
 * @param row MS Excel row.
 * @return boolean
 */
public boolean isEmptyRow(Row row) {
    if (row == null) {
        return true;
    }

    for (int j = 0; j <= row.getLastCellNum(); j++) {
        Cell cell = row.getCell(j);
        if (cell == null) {
            continue;
        }
        if (!Utils.isNullStr(cellValueToString(cell, null))) {
            return false;
        }
    }
    return true;
}

From source file:eionet.gdem.conversion.excel.reader.ExcelReader.java

License:Mozilla Public License

/**
 * Method goes through 4 rows and search the best fit of XML Schema. The deault row is 4.
 *
 * @param schemaSheet Schema sheet name.
 * @return schema URL.//from   w  ww. j av a 2 s. com
 */
private String findSchemaFromSheet(Sheet schemaSheet) {
    Row schemaRow = null;
    Cell schemaCell = null;

    for (int i = 3; i > -1; i--) {
        if (schemaSheet.getLastRowNum() < i) {
            continue;
        }
        schemaRow = schemaSheet.getRow(i);
        if (schemaRow == null) {
            continue;
        }
        if (schemaRow.getLastCellNum() < 0) {
            continue;
        }
        schemaCell = schemaRow.getCell(0);
        String val = schemaCell.getRichStringCellValue().toString();

        if (val.startsWith("http://") && val.toLowerCase().indexOf("/getschema") > 0 && Utils.isURL(val)) {
            return val;
        }
    }
    return null;
}