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.hellojavaer.poi.excel.utils.ExcelUtils.java

License:Apache License

private static Object _readCell(Cell cell) {
    if (cell == null) {
        return null;
    }//from   ww  w.jav  a2s . com
    int cellType = cell.getCellType();
    Object value = null;
    switch (cellType) {
    case Cell.CELL_TYPE_BLANK:
        value = null;
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        boolean bool = cell.getBooleanCellValue();
        value = bool;
        break;
    case Cell.CELL_TYPE_ERROR:
        // cell.getErrorCellValue();
        ExcelReadException e = new ExcelReadException("Cell type error");
        e.setRowIndex(cell.getRowIndex());
        e.setColIndex(cell.getColumnIndex());
        e.setCode(ExcelReadException.CODE_OF_CELL_ERROR);
        throw e;
    case Cell.CELL_TYPE_FORMULA:
        value = cell.getCellFormula();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        Object inputValue = null;//
        double doubleVal = cell.getNumericCellValue();
        if (DateUtil.isCellDateFormatted(cell)) {
            inputValue = DateUtil.getJavaDate(doubleVal);
        } else {
            long longVal = Math.round(cell.getNumericCellValue());
            if (Double.parseDouble(longVal + ".0") == doubleVal) {
                inputValue = longVal;
            } else {
                inputValue = doubleVal;
            }
        }
        value = inputValue;
        break;
    case Cell.CELL_TYPE_STRING:
        value = cell.getStringCellValue();
        break;
    default:
        throw new RuntimeException("unsupport cell type " + cellType);
    }
    return value;
}

From source file:org.joeffice.spreadsheet.actions.DeleteCellsAction.java

License:Apache License

@Override
public void actionPerformed(ActionEvent ae) {
    SpreadsheetTopComponent currentTopComponent = OfficeTopComponent
            .getSelectedComponent(SpreadsheetTopComponent.class);
    if (currentTopComponent != null) {
        JTable currentTable = currentTopComponent.getSelectedTable();
        SheetTableModel tableModel = (SheetTableModel) currentTable.getModel();
        List<Cell> selectedCells = CellUtils.getSelectedCells(currentTable);
        for (Cell cell : selectedCells) {
            cell.setCellValue("");
            tableModel.fireTableCellUpdated(cell.getRowIndex(), cell.getColumnIndex());
        }//w ww.ja  v  a 2s.  co m
    }
}

From source file:org.joeffice.spreadsheet.actions.FormatAction.java

License:Apache License

@Override
public void actionPerformed(ActionEvent ae) {
    SpreadsheetTopComponent currentTopComponent = OfficeTopComponent
            .getSelectedComponent(SpreadsheetTopComponent.class);
    if (currentTopComponent != null) {
        JTable currentTable = currentTopComponent.getSelectedTable();
        SheetTableModel tableModel = (SheetTableModel) currentTable.getModel();
        List<Cell> selectedCells = CellUtils.getSelectedCells(currentTable);
        if (selectedCells.isEmpty()) {
            return;
        }/*from   w w  w . j  a  v  a2  s.  co m*/
        if (choosePattern) {
            pattern = askFromList();
        } else if (definePattern) {
            pattern = askFromInputField();
        }
        if (pattern == null) {
            return;
        }
        Workbook workbook = selectedCells.get(0).getSheet().getWorkbook();
        DataFormat format = workbook.createDataFormat();
        short formatIndex = format.getFormat(pattern);
        for (Cell cell : selectedCells) {
            cell.getCellStyle().setDataFormat(formatIndex);
            tableModel.fireTableCellUpdated(cell.getRowIndex(), cell.getColumnIndex());
        }
    }
}

From source file:org.joeffice.spreadsheet.actions.SetBordersAction.java

License:Apache License

public void setBorder(JTable currentTable, short thickness, short color) {
    SheetTableModel tableModel = (SheetTableModel) currentTable.getModel();
    List<Cell> selectedCells = CellUtils.getSelectedCells(currentTable, true);
    for (Cell cell : selectedCells) {
        Workbook workbook = cell.getSheet().getWorkbook();
        CellUtil.setCellStyleProperty(cell, workbook, CellUtil.BORDER_TOP, thickness);
        CellUtil.setCellStyleProperty(cell, workbook, CellUtil.TOP_BORDER_COLOR, color);
        CellUtil.setCellStyleProperty(cell, workbook, CellUtil.BORDER_LEFT, thickness);
        CellUtil.setCellStyleProperty(cell, workbook, CellUtil.LEFT_BORDER_COLOR, color);
        CellUtil.setCellStyleProperty(cell, workbook, CellUtil.BORDER_BOTTOM, thickness);
        CellUtil.setCellStyleProperty(cell, workbook, CellUtil.BOTTOM_BORDER_COLOR, color);
        CellUtil.setCellStyleProperty(cell, workbook, CellUtil.BORDER_RIGHT, thickness);
        CellUtil.setCellStyleProperty(cell, workbook, CellUtil.RIGHT_BORDER_COLOR, color);
        tableModel.fireTableCellUpdated(cell.getRowIndex(), cell.getColumnIndex());
    }/*from w ww .j  a  v a  2 s. c o  m*/
}

From source file:org.joeffice.spreadsheet.TableStyleable.java

License:Apache License

@Override
public void setFontAttributes(AttributedString attributes) {
    SpreadsheetTopComponent currentTopComponent = OfficeTopComponent
            .getSelectedComponent(SpreadsheetTopComponent.class);
    if (currentTopComponent != null) {
        JTable table = currentTopComponent.getSelectedTable();

        List<Cell> selectedCells = CellUtils.getSelectedCells(table);
        for (Cell cell : selectedCells) {
            AttributedCharacterIterator attributesIterator = attributes.getIterator();
            for (Attribute attribute : attributesIterator.getAllAttributeKeys()) {
                Object value = attributesIterator.getAttribute(attribute);
                addAttribute(attribute, value, cell);
                ((AbstractTableModel) table.getModel()).fireTableCellUpdated(cell.getRowIndex(),
                        cell.getColumnIndex());
            }/*from w  w w. ja v a  2 s . c o  m*/
        }
    }
}

From source file:org.nuclos.client.nuclet.generator.content.AbstractNucletContentGenerator.java

License:Open Source License

protected void error(Cell cell, Exception ex) {
    error(String.format("[Row %s, Column %s], %s", cell == null ? "null" : cell.getRowIndex() + 1,
            cell == null ? "null" : cell.getColumnIndex() + 1, ex.getMessage()));
}

From source file:org.pentaho.di.trans.steps.excelwriter.ExcelWriterStep.java

License:Apache License

/**
 * Set specified cell format//from w w  w .  j  a  v  a2s  .c  om
 *
 * @param excelFieldFormat the specified format
 * @param cell             the cell to set up format
 */
private void setDataFormat(String excelFieldFormat, Cell cell) {
    if (log.isDebug()) {
        logDebug(BaseMessages.getString(PKG, "ExcelWriterStep.Log.SetDataFormat", excelFieldFormat,
                CellReference.convertNumToColString(cell.getColumnIndex()), cell.getRowIndex()));
    }

    DataFormat format = data.wb.createDataFormat();
    short formatIndex = format.getFormat(excelFieldFormat);
    CellStyle style = data.wb.createCellStyle();
    style.cloneStyleFrom(cell.getCellStyle());
    style.setDataFormat(formatIndex);
    cell.setCellStyle(style);
}

From source file:org.specrunner.source.excel.SourceFactoryExcel.java

License:Open Source License

/**
 * Read header information.//  w  w  w  . jav a  2  s .  com
 * 
 * @param table
 *            The target table.
 * @param caption
 *            The caption element.
 * @param spanMap
 *            Map of span cells.
 * @param ignore
 *            Set of cells to ignore.
 * @param ite
 *            The row iterator.
 * @return The number of columns to read.
 */
protected int headers(Element table, Element caption, Map<String, Dimension> spanMap, Set<String> ignore,
        Iterator<Row> ite) {
    int result = 0;
    Element thead = new Element("thead");
    table.appendChild(thead);
    {
        Element tr = new Element("tr");
        thead.appendChild(tr);
        {
            if (ite.hasNext()) {
                Row row = ite.next();
                result = row.getLastCellNum();
                for (int i = 0; i < result; i++) {
                    Cell cell = row.getCell(i);
                    if (cell != null) {
                        String key = cell.getRowIndex() + "," + cell.getColumnIndex();
                        if (ignore.contains(key)) {
                            continue;
                        }
                        Element th = new Element("th");
                        tr.appendChild(th);
                        th.appendChild(String.valueOf(extractVal(cell)));
                        addAttributes(table, caption, tr, th, cell, spanMap.get(key));
                    }
                }
            }
        }
    }
    return result;
}

From source file:org.specrunner.source.excel.SourceFactoryExcel.java

License:Open Source License

/**
 * Read content of a table, using the rows iterator and the number of
 * columns.//  ww w. jav a  2s  .c om
 * 
 * @param table
 *            The table.
 * @param caption
 *            The table caption.
 * @param spanMap
 *            Map of span cells.
 * @param ignore
 *            Set of cells to ignore.
 * @param ite
 *            The row iterator.
 * @param columns
 *            The number of columns to read.
 */
protected void readBody(Element table, Element caption, Map<String, Dimension> spanMap, Set<String> ignore,
        Iterator<Row> ite, int columns) {
    Element tbody = new Element("tbody");
    table.appendChild(tbody);
    {
        while (ite.hasNext()) {
            Element tr = new Element("tr");
            tbody.appendChild(tr);
            {
                Row row = ite.next();
                // invalid lines return -1 in row.getFirstCellNum().
                if (row.getFirstCellNum() < 0) {
                    continue;
                }
                for (int k = 0; k < columns; k++) {
                    Cell cell = row.getCell(k);
                    String key = null;
                    if (cell != null) {
                        key = cell.getRowIndex() + "," + cell.getColumnIndex();
                        if (ignore.contains(key)) {
                            continue;
                        }
                    }
                    Element td = new Element("td");
                    tr.appendChild(td);
                    td.appendChild(String.valueOf(extractVal(cell)));
                    if (cell != null) {
                        addAttributes(table, caption, tr, td, cell, spanMap.get(key));
                    }
                }
            }
        }
    }
}

From source file:org.squashtest.tm.service.internal.batchimport.excel.TypeBasedCellValueCoercer.java

License:Open Source License

private CannotCoerceException cannotCoerceFunky(Cell cell) {
    return new CannotCoerceException("Cannot coerce cell [R," + cell.getRowIndex() + " C"
            + cell.getColumnIndex() + "] of unhandled type", Messages.ERROR_FUNKY_CELL_TYPE);

}