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.CellControlsUtility.java

License:MIT License

/**
 * Setup control attributes.//w ww .java  2 s . c om
 *
 * @param originRowIndex
 *            the origin row index
 * @param fcell
 *            the fcell
 * @param poiCell
 *            the poi cell
 * @param sheetConfig
 *            the sheet config
 * @param cellAttributesMap
 *            the cell attributes map
 */
public static void setupControlAttributes(final int originRowIndex, final FacesCell fcell, final Cell poiCell,
        final SheetConfiguration sheetConfig, final CellAttributesMap cellAttributesMap) {
    int rowIndex = originRowIndex;
    if (rowIndex < 0) {
        rowIndex = poiCell.getRowIndex();
    }

    String skey = poiCell.getSheet().getSheetName() + "!"
            + CellUtility.getCellIndexNumberKey(poiCell.getColumnIndex(), rowIndex);

    Map<String, String> commentMap = cellAttributesMap.getTemplateCommentMap().get("$$");
    if (commentMap != null) {
        String comment = commentMap.get(skey);
        if (comment != null) {
            CommandUtility.createCellComment(poiCell, comment, sheetConfig.getFinalCommentMap());
        }
    }

    String widgetType = cellAttributesMap.getCellInputType().get(skey);
    if (widgetType != null) {
        fcell.setControl(widgetType.toLowerCase());

        fcell.setInputAttrs(cellAttributesMap.getCellInputAttributes().get(skey));
        fcell.setSelectItemAttrs(cellAttributesMap.getCellSelectItemsAttributes().get(skey));
        fcell.setDatePattern(cellAttributesMap.getCellDatePattern().get(skey));
    }

}

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

License:MIT License

/**
 * return cell value with format.//from   w  ww. j a va 2 s  .  c  o  m
 * 
 * @param poiCell
 *            cell.
 * @param formulaEvaluator
 *            formula evaluator.
 * @param dataFormatter
 *            data formatter.
 * @return cell string value with format.
 */
@SuppressWarnings("deprecation")
public static String getCellValueWithFormat(final Cell poiCell, final FormulaEvaluator formulaEvaluator,
        final DataFormatter dataFormatter) {

    if (poiCell == null) {
        return null;
    }

    String result;
    try {
        CellType cellType = poiCell.getCellTypeEnum();
        if (cellType == CellType.FORMULA) {
            cellType = formulaEvaluator.evaluate(poiCell).getCellTypeEnum();
        }
        if (cellType == CellType.ERROR) {
            result = "";
        } else {
            result = dataFormatter.formatCellValue(poiCell, formulaEvaluator);
        }
    } catch (Exception e) {
        LOG.log(Level.SEVERE,
                "Web Form WebFormHelper getCellValue Error row = " + poiCell.getRowIndex() + " column = "
                        + poiCell.getColumnIndex() + " error = " + e.getLocalizedMessage()
                        + "; Change return result to blank",
                e);
        result = "";
    }

    return result;
}

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

License:MIT License

/**
 * clone existing comments into new cell comment.
 * /*from   w w w  .j av  a 2  s.  c  om*/
 * @param sourceCell
 *            source cell.
 * @param newCell
 *            target cell.
 */

public static void cloneComment(final Cell sourceCell, final Cell newCell) {

    XSSFSheet sheet = (XSSFSheet) newCell.getSheet();
    CreationHelper factory = sheet.getWorkbook().getCreationHelper();
    Drawing drawing = sheet.createDrawingPatriarch();
    XSSFComment sourceComment = (XSSFComment) sourceCell.getCellComment();
    // Below code are from POI busy manual.
    // When the comment box is visible, have it show in a 1x3 space
    ClientAnchor anchor = createCommentAnchor(newCell, factory);
    // Create the comment and set the text+author
    Comment comment = drawing.createCellComment(anchor);
    RichTextString str = factory.createRichTextString(sourceComment.getString().toString());
    comment.setString(str);
    comment.setAuthor(sourceComment.getAuthor());
    // Assign the comment to the cell
    newCell.setCellComment(comment);
    comment.setColumn(newCell.getColumnIndex());
    comment.setRow(newCell.getRowIndex());
    // As POI doesn't has well support for comments,
    // So we have to use low level api to match the comments.
    matchCommentSettings(newCell, sourceCell);
}

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

License:MIT License

/**
 * Creates the comment anchor.// ww  w .j  av  a 2s.c o m
 *
 * @param newCell the new cell
 * @param factory the factory
 * @return the client anchor
 */
private static ClientAnchor createCommentAnchor(final Cell newCell, CreationHelper factory) {
    ClientAnchor anchor = factory.createClientAnchor();
    anchor.setCol1(newCell.getColumnIndex());
    anchor.setCol2(newCell.getColumnIndex() + 1);
    anchor.setRow1(newCell.getRowIndex());
    anchor.setRow2(newCell.getRowIndex() + 3);
    return anchor;
}

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

License:MIT License

/**
 * Creates the or insert comment./*from   w  w w.  ja  v  a 2s  . co  m*/
 *
 * @param cell the cell
 * @param commentStr the comment str
 */
public static void createOrInsertComment(final Cell cell, final String commentStr) {

    XSSFSheet sheet = (XSSFSheet) cell.getSheet();
    CreationHelper factory = sheet.getWorkbook().getCreationHelper();
    Drawing drawing = sheet.createDrawingPatriarch();
    Comment comment = cell.getCellComment();
    String originStr = "";
    if (comment == null) {
        // Below code are from POI busy manual.
        // When the comment box is visible, have it show in a 1x3 space
        ClientAnchor anchor = createCommentAnchor(cell, factory);
        // Create the comment and set the text+author
        comment = drawing.createCellComment(anchor);
    } else {
        originStr = comment.getString().getString() + "\n";
    }
    originStr += commentStr;
    RichTextString str = factory.createRichTextString(originStr);
    comment.setString(str);
    comment.setAuthor("");
    // Assign the comment to the cell
    cell.setCellComment(comment);
    comment.setColumn(cell.getColumnIndex());
    comment.setRow(cell.getRowIndex());
}

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

License:MIT License

/**
 * Use low level API to match the comments setting.
 * //w  w w. j  a v a 2 s. c  o  m
 * @param newCell
 *            target cell.
 * @param sourceCell
 *            source cell.
 */
private static void matchCommentSettings(final Cell newCell, final Cell sourceCell) {
    try {
        XSSFVMLDrawing sourceVml = getVmlDrawingFromCell(sourceCell);
        XSSFVMLDrawing targetVml = getVmlDrawingFromCell(newCell);
        CTShape sourceCtShape = getCtShapeFromVml(sourceCell, sourceVml);
        CTShape targetCtShape = getCtShapeFromVml(newCell, targetVml);
        targetCtShape.setType(sourceCtShape.getType());
        CTClientData sourceClientData = sourceCtShape.getClientDataArray(0);
        CTClientData targetClientData = targetCtShape.getClientDataArray(0);
        String[] anchorArray = sourceClientData.getAnchorList().get(0).split(",");
        int shiftRows = newCell.getRowIndex() - sourceCell.getRowIndex();
        /*
         * AchorArray mappings: 0->col1 1->dx1 2->row1 3->dy1 4->col2 5->dx2 6-> row2
         * 7->dy2
         */
        anchorArray[2] = Integer.toString(Integer.parseInt(anchorArray[2].trim()) + shiftRows);
        anchorArray[6] = Integer.toString(Integer.parseInt(anchorArray[6].trim()) + shiftRows);
        targetClientData.getAnchorList().set(0, FacesUtility.strJoin(anchorArray, ","));
    } catch (Exception e) {
        LOG.log(Level.SEVERE, "matchCommentSettings error = " + e.getLocalizedMessage(), e);
    }
}

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

License:MIT License

/**
 * Find CtShape from vml object. This class use reflection to invoke the
 * protected method in POI.// www . j a  v  a 2 s.c  o  m
 *
 * @param sourceCell            cell.
 * @param sourceVml            vml.
 * @return ctShape.
 * @throws ReflectiveOperationException the reflective operation exception
 * @throws SecurityException the security exception
 */
@SuppressWarnings("rawtypes")
private static CTShape getCtShapeFromVml(final Cell sourceCell, XSSFVMLDrawing sourceVml)
        throws ReflectiveOperationException {
    Method findshape;
    // int parameter
    Class[] paramInt = new Class[2];
    paramInt[0] = Integer.TYPE;
    paramInt[1] = Integer.TYPE;
    findshape = sourceVml.getClass().getDeclaredMethod("findCommentShape", paramInt);
    findshape.setAccessible(true);
    return (CTShape) findshape.invoke(sourceVml, sourceCell.getRowIndex(), sourceCell.getColumnIndex());
}

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

License:MIT License

/**
 * return cell index number key. e.g. $0$0 for A1 cell.
 * /*w w w.j  a v  a 2  s  . com*/
 * @param cell
 *            input cell.
 * @return string.
 */
public static String getCellIndexNumberKey(final Cell cell) {
    if (cell != null) {
        return TieConstants.CELL_ADDR_PRE_FIX + cell.getColumnIndex() + TieConstants.CELL_ADDR_PRE_FIX
                + cell.getRowIndex();
    }
    return null;
}

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

License:MIT License

/**
 * Gets the skey from poi cell.//from w  w  w. j  a v a  2s  .com
 *
 * @param poiCell the poi cell
 * @return the skey from poi cell
 */
public static String getSkeyFromPoiCell(final Cell poiCell) {
    return poiCell.getSheet().getSheetName() + "!"
            + CellUtility.getCellIndexNumberKey(poiCell.getColumnIndex(), poiCell.getRowIndex());
}

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

License:MIT License

/**
 * Gets the anchor size./*from w  w  w  . ja v a 2 s. c  o  m*/
 *
 * @param sheet1
 *            the sheet 1
 * @param fcell
 *            the fcell
 * @param cell
 *            the cell
 * @param anchor
 *            the anchor
 * @return the anchor size
 */
public static AnchorSize getAnchorSize(final Sheet sheet1, final FacesCell fcell, final Cell cell,
        final ClientAnchor anchor) {

    if (!(sheet1 instanceof XSSFSheet)) {
        return null;
    }
    double picWidth = 0.0;
    double picHeight = 0.0;
    int left = anchor.getDx1() / org.apache.poi.util.Units.EMU_PER_PIXEL;
    int top = (int) ((double) anchor.getDy1() / org.apache.poi.util.Units.EMU_PER_PIXEL
            / WebSheetUtility.PICTURE_HEIGHT_ADJUST);
    int right = anchor.getDx2() / org.apache.poi.util.Units.EMU_PER_PIXEL;
    int bottom = (int) ((double) anchor.getDy2() / org.apache.poi.util.Units.EMU_PER_PIXEL
            / WebSheetUtility.PICTURE_HEIGHT_ADJUST);

    double cellWidth = 0.0;
    double cellHeight = 0.0;

    if ((cell != null) && (fcell != null)) {
        for (int col = cell.getColumnIndex(); col < cell.getColumnIndex() + fcell.getColspan(); col++) {
            cellWidth += sheet1.getColumnWidthInPixels(col);
        }
        double lastCellWidth = sheet1.getColumnWidthInPixels(cell.getColumnIndex() + fcell.getColspan() - 1);

        for (int rowIndex = cell.getRowIndex(); rowIndex < cell.getRowIndex()
                + fcell.getRowspan(); rowIndex++) {
            cellHeight += WebSheetUtility.pointsToPixels(sheet1.getRow(rowIndex).getHeightInPoints());
        }
        double lastCellHeight = WebSheetUtility
                .pointsToPixels(sheet1.getRow(cell.getRowIndex() + fcell.getRowspan() - 1).getHeightInPoints());

        picWidth = cellWidth - lastCellWidth + right - left;
        picHeight = cellHeight - lastCellHeight + bottom - top;
    } else {
        for (short col = anchor.getCol1(); col < anchor.getCol2(); col++) {
            picWidth += sheet1.getColumnWidthInPixels(col);
        }
        for (int rowindex = anchor.getRow1(); rowindex < anchor.getRow2(); rowindex++) {
            Row row = sheet1.getRow(rowindex);
            if (row != null) {
                picHeight += WebSheetUtility.pointsToPixels(row.getHeightInPoints());
            }
        }
    }

    return new AnchorSize(left, top, (int) picWidth, (int) picHeight, cellWidth, cellHeight);

}