Example usage for org.apache.poi.hssf.usermodel HSSFRow getLastCellNum

List of usage examples for org.apache.poi.hssf.usermodel HSSFRow getLastCellNum

Introduction

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

Prototype

@Override
public short getLastCellNum() 

Source Link

Document

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

Usage

From source file:fr.univrouen.poste.services.ExcelParser.java

License:Apache License

public List<List<String>> getCells(InputStream xslFileInput) {

    List<List<String>> cellVectorHolder = new Vector<List<String>>();

    try {//from   w ww.  j a  v  a 2  s .  c o m

        POIFSFileSystem fileSystem = new POIFSFileSystem(xslFileInput);
        HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
        HSSFSheet sheet = workBook.getSheetAt(0);
        Iterator<Row> rowIter = sheet.rowIterator();

        while (rowIter.hasNext()) {
            HSSFRow myRow = (HSSFRow) rowIter.next();
            List<String> cellStoreVector = new Vector<String>();
            // take care of blank cell !
            // @see http://stackoverflow.com/questions/4929646/how-to-get-an-excel-blank-cell-value-in-apache-poi
            int max = myRow.getLastCellNum();
            for (int i = 0; i < max; i++) {
                HSSFCell myCell = (HSSFCell) myRow.getCell(i, Row.CREATE_NULL_AS_BLANK);
                if (Cell.CELL_TYPE_STRING == myCell.getCellType())
                    cellStoreVector.add(myCell.getStringCellValue());
                else if ((Cell.CELL_TYPE_NUMERIC == myCell.getCellType()))
                    cellStoreVector.add(Long.toString(new Double(myCell.getNumericCellValue()).longValue()));
                else if ((Cell.CELL_TYPE_BLANK == myCell.getCellType()))
                    cellStoreVector.add("");
                else {
                    logger.debug("This cell is not numeric or string ... : " + myCell + " \n ... cellType : "
                            + myCell.getCellType());
                    cellStoreVector.add("");
                }
            }
            cellVectorHolder.add(cellStoreVector);
        }
    } catch (Exception e) {
        logger.error("Error during parsing the XSL File", e);
        throw new RuntimeException("Error during parsing the XSL File", e);
    }

    return cellVectorHolder;
}

From source file:framework.retrieval.engine.index.create.impl.file.parse.ExcelFileContentParser.java

License:Apache License

public String getContent(RFileDocument document, String charsetName) {

    InputStream fileInputStream = null;
    StringBuffer content = new StringBuffer();

    try {/*  w  ww .  j a  v a2  s  .  co m*/
        fileInputStream = new FileInputStream(document.getFile());
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);// Excel

        if (workbook != null) {
            int numSheetsNumber = workbook.getNumberOfSheets();
            for (int numSheets = 0; numSheets < numSheetsNumber; numSheets++) {
                HSSFSheet aSheet = workbook.getSheetAt(numSheets);// sheet
                if (aSheet != null) {
                    int lastRowNum = aSheet.getLastRowNum();
                    for (int rowNumOfSheet = 0; rowNumOfSheet <= lastRowNum; rowNumOfSheet++) {
                        HSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 
                        if (aRow != null) {
                            int lastCellNum = aRow.getLastCellNum();
                            for (int cellNumOfRow = 0; cellNumOfRow <= lastCellNum; cellNumOfRow++) {
                                HSSFCell aCell = aRow.getCell(cellNumOfRow);// 
                                if (aCell != null) {
                                    int cellType = aCell.getCellType();
                                    if (cellType == HSSFCell.CELL_TYPE_STRING) {
                                        String value = StringClass.getString(aCell.getStringCellValue());
                                        content.append(value);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        RetrievalUtil.errorLog(log, document.getFile().getAbsolutePath(), e);
    } finally {
        if (fileInputStream != null) {
            try {
                fileInputStream.close();
            } catch (Exception e) {

            }
        }
    }

    return content.toString();

}

From source file:gov.nih.nci.evs.browser.utils.ResolvedValueSetIteratorHolder.java

License:Open Source License

/**
 * (Each Excel sheet row becomes an HTML table row) Generates an HTML table
 * row which has the same height as the Excel row.
 *
 * @param row// w  w w.jav  a 2  s.  com
 *            The Excel row.
 */
private void tr(final HSSFRow row) {
    if (row == null) {
        return;
    }
    out.append("<tr ");
    // Find merged cells in current row.
    for (int i = 0; i < row.getSheet().getNumMergedRegions(); ++i) {
        final CellRangeAddress merge = row.getSheet().getMergedRegion(i);
        if (merge == null)
            return;
        if (rowIndex >= merge.getFirstRow() && rowIndex <= merge.getLastRow()) {
            mergeStart = merge.getFirstColumn();
            mergeEnd = merge.getLastColumn();
            break;
        }
    }
    out.append("style='");
    if (row.getHeight() != -1) {
        out.append("height: ").append(Math.round(row.getHeight() / 20.0 * 1.33333)).append("px; ");
    }
    out.append("'>\n");
    for (colIndex = 0; colIndex < row.getLastCellNum(); ++colIndex) {
        td(row.getCell(colIndex));
    }
    out.append("</tr>\n");
}

From source file:gov.nih.nci.evs.browser.utils.ResolvedValueSetIteratorHolder.java

License:Open Source License

private void tr(final HSSFRow row, StringBuffer buf) {
    if (row == null) {
        return;/*  www.  j  a va 2  s  .c  o  m*/
    }
    buf.append("<tr ");
    // Find merged cells in current row.
    for (int i = 0; i < row.getSheet().getNumMergedRegions(); ++i) {
        final CellRangeAddress merge = row.getSheet().getMergedRegion(i);
        if (merge == null)
            return;
        if (rowIndex >= merge.getFirstRow() && rowIndex <= merge.getLastRow()) {
            mergeStart = merge.getFirstColumn();
            mergeEnd = merge.getLastColumn();
            break;
        }
    }
    buf.append("style='");
    if (row.getHeight() != -1) {
        buf.append("height: ").append(Math.round(row.getHeight() / 20.0 * 1.33333)).append("px; ");

    }
    buf.append("'>");
    for (colIndex = 0; colIndex < row.getLastCellNum(); ++colIndex) {
        td(row.getCell(colIndex));
        td(row.getCell(colIndex), buf);
    }
    buf.append("</tr>");
}

From source file:gov.nih.nci.evs.browser.utils.ResolvedValueSetIteratorHolder.java

License:Open Source License

private void getRowData(Vector v, final HSSFRow row) {
    if (row == null) {
        return;//from w w  w.  jav a  2  s. c  o m
    }

    for (int i = 0; i < row.getSheet().getNumMergedRegions(); ++i) {
        final CellRangeAddress merge = row.getSheet().getMergedRegion(i);
        if (merge == null)
            return;
        if (rowIndex >= merge.getFirstRow() && rowIndex <= merge.getLastRow()) {
            mergeStart = merge.getFirstColumn();
            mergeEnd = merge.getLastColumn();
            break;
        }
    }

    StringBuffer buf = new StringBuffer();
    for (colIndex = 0; colIndex < row.getLastCellNum(); ++colIndex) {
        if (colIndex > 0) {
            buf.append("|");
        }
        String val = getCellData(row.getCell(colIndex));
        if (val != null) {
            buf.append(val);
        }
    }
    v.add(buf.toString());
}

From source file:guineu.data.parser.impl.GCGCParserXLS.java

License:Open Source License

public void fillData() {
    try {//from ww  w . jav a2  s  . c o m
        book = this.openExcel(DatasetName);
        HSSFSheet sheet;
        try {
            sheet = book.getSheet(sheetName);
        } catch (Exception exception) {
            sheet = book.getSheetAt(0);
        }

        int initRow = this.getRowInit(sheet);

        if (initRow > -1) {
            numberRows = this.getNumberRows(initRow, sheet);
            HSSFRow row = sheet.getRow(initRow);

            for (int i = 0; i < row.getLastCellNum(); i++) {
                HSSFCell cell = row.getCell(i);
                this.head.add(cell.toString());
            }
            this.readMetabolites(initRow + 1, numberRows, sheet);

            this.setExperimentsName(head);
        } else {
            this.dataset = null;
        }
    } catch (IOException ex) {
        Logger.getLogger(GCGCParserXLS.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:guineu.data.parser.impl.GCGCParserXLS.java

License:Open Source License

/**
 * Reads lipid information of one row./*from  w ww  .  j  a v a  2  s  .c om*/
 * @param row
 * @param numberCols
 * @return
 */
public void readRow(HSSFRow row) {
    HSSFCell cell;
    SimplePeakListRowGCGC metabolite = new SimplePeakListRowGCGC();
    for (int i = 0; i < row.getLastCellNum(); i++) {
        try {
            String title = head.get(i);
            if (title == null) {
                continue;
            }
            cell = row.getCell(i);
            boolean isfound = false;

            for (GCGCColumnName field : GCGCColumnName.values()) {
                if (title.matches(field.getRegularExpression())) {
                    metabolite.setVar(field.getSetFunctionName(),
                            this.getType(cell.toString(), field.getType()));
                    isfound = true;
                    break;
                }
            }

            if (!isfound) {
                try {
                    metabolite.setPeak(title, cell.getNumericCellValue());
                } catch (Exception e) {
                    metabolite.setPeak(title, 0.0);
                }
            }

            if (metabolite.getName() == null) {
                metabolite.setName("unknown");
            }
        } catch (Exception exception) {
            //exception.printStackTrace();
        }
    }
    this.dataset.addRow(metabolite);
}

From source file:guineu.data.parser.impl.LCMSParserXLS.java

License:Open Source License

public void fillData() {
    try {/*  ww  w  .  j a v a  2 s .  co  m*/
        book = this.openExcel(DatasetName);
        HSSFSheet sheet;
        try {
            sheet = book.getSheet(sheetName);
        } catch (Exception exception) {
            sheet = book.getSheetAt(0);
        }

        int initRow = this.getRowInit(sheet);

        if (initRow > -1) {
            numberRows = this.getNumberRows(initRow, sheet);
            HSSFRow row = sheet.getRow(initRow);

            for (int i = 0; i < row.getLastCellNum(); i++) {
                HSSFCell cell = row.getCell((short) i);
                this.head.add(cell.toString());
            }
            this.readLipids(initRow + 1, numberRows, sheet);

            this.setExperimentsName(head);
        } else {
            this.dataset = null;
        }
    } catch (IOException ex) {
        Logger.getLogger(LCMSParserXLS.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:guineu.data.parser.impl.LCMSParserXLS.java

License:Open Source License

/**
 * Reads lipid information of one row./*ww w .j a v a  2  s.  c o m*/
 * @param row
 * @param numberCols
 * @return
 */
public void readRow(HSSFRow row) {
    HSSFCell cell;
    SimplePeakListRowLCMS lipid = new SimplePeakListRowLCMS();
    for (int i = 0; i < row.getLastCellNum(); i++) {
        try {
            String title = head.get(i);
            if (title == null) {
                continue;
            }
            cell = row.getCell((short) i);
            boolean isfound = false;
            for (LCMSColumnName field : LCMSColumnName.values()) {
                if (title.matches(field.getRegularExpression())) {
                    if (field == LCMSColumnName.RT) {
                        double rt = cell.getNumericCellValue();
                        if (rt < 20) {
                            rt *= 60;
                            lipid.setVar(field.getSetFunctionName(), rt);
                        } else {
                            lipid.setVar(field.getSetFunctionName(), cell.getNumericCellValue());
                        }
                    } else {
                        lipid.setVar(field.getSetFunctionName(),
                                this.getType(cell.toString(), field.getType()));
                    }
                    isfound = true;
                    break;
                }
            }

            if (!isfound) {
                try {
                    lipid.setPeak(title, cell.getNumericCellValue());
                } catch (Exception e) {
                    if (cell.toString().matches(".*null.*|.*NA.*|.*N/A.*")) {
                        lipid.setPeak(title, 0.0);
                    } else if (cell.toString() != null) {
                        lipid.setPeak(title, cell.toString());
                    }
                }
            }

            if (i == 0 && (cell.getCellStyle().getFillForegroundColor() == 13)) {
                lipid.setStandard(1);
            }
            int DataType = this.v_type(book, row, cell);
            if (DataType == 0) {
                lipid.setControl(false);
                lipid.setName("z-non valid");
            } else {
                lipid.setControl(true);
            }

            if (lipid.getName() == null) {
                lipid.setName("unknown");
            }
            lipid.setLipidClass(String.valueOf(this.LipidClassLib.get_class(lipid.getName())));
        } catch (Exception exception) {
            //exception.printStackTrace();
        }
    }
    this.dataset.addRow(lipid);
}

From source file:guineu.modules.configuration.parameters.ParameterDialog.java

License:Open Source License

public void readRows(HSSFSheet sheet) {
    try {//from  w ww  .  j  a  v  a2  s. c o  m
        Iterator rowIt = sheet.rowIterator();
        HSSFRow row = (HSSFRow) rowIt.next();

        for (int i = 1; i < row.getLastCellNum(); i++) {
            HSSFCell cell = row.getCell(i);
            model.addColumn(cell.toString());
        }
        ((ParameterDataModel) model).fireTableStructureChanged();
        table.getColumnModel().getColumn(0).setMinWidth(300);
        int rowIndex = -1;
        while (rowIt.hasNext()) {
            row = (HSSFRow) rowIt.next();
            HSSFCell cell;
            cell = row.getCell(0);
            for (int e = 0; e < model.getRowCount(); e++) {
                String sampleName = model.getValueAt(e, 0);
                if (sampleName.equals(cell.toString())) {
                    rowIndex = e;
                }
            }
            for (int i = 1; i < row.getLastCellNum(); i++) {
                cell = row.getCell(i);
                model.setValueAt(cell.toString(), rowIndex, i);
            }
        }
        ((ParameterDataModel) model).addParameters(dataset);
        ((ParameterDataModel) model).fireTableDataChanged();
    } catch (Exception e) {
        e.printStackTrace();
    }
}