Example usage for org.apache.poi.hssf.usermodel HSSFCell getRowIndex

List of usage examples for org.apache.poi.hssf.usermodel HSSFCell getRowIndex

Introduction

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

Prototype

@Override
public int getRowIndex() 

Source Link

Usage

From source file:blueprint.sdk.experimental.util.XlsReader.java

License:Open Source License

/**
 * create error message for cell//from www  .j  ava2s.c  om
 *
 * @param cell target cell
 * @return error message
 */
protected String createTypeErrMsg(final HSSFCell cell) {
    return createTypeErrMsg(wbook.getSheetIndex(cell.getSheet()), cell.getRowIndex(), cell.getColumnIndex());
}

From source file:cn.trymore.core.util.excel.PoiExcelParser.java

License:Open Source License

private Boolean isCellInConditionalFormat(HSSFSheetConditionalFormatting cfms, HSSFCell cell) {
    if ((cell != null) && (cfms != null)) {
        int rowIdx = cell.getRowIndex();
        int columnIdx = cell.getColumnIndex();
        if (cfms.getNumConditionalFormattings() > 0) {
            int i = 0;
            for (int len = cfms.getNumConditionalFormattings(); i < len; ++i) {

                if (cfms.getConditionalFormattingAt(i).getNumberOfRules() <= 0) {
                    continue;
                }// ww w .  ja  va  2 s .c o  m

                for (int k = 0; k < cfms.getConditionalFormattingAt(i).getFormattingRanges().length; ++k) {

                    if (!(cfms.getConditionalFormattingAt(i).getFormattingRanges()[k].isInRange(rowIdx,
                            columnIdx))) {
                        continue;
                    }

                    for (int j = 0, size = cfms.getConditionalFormattingAt(i)
                            .getNumberOfRules(); j < size; ++j) {
                        HSSFConditionalFormattingRule rule = cfms.getConditionalFormattingAt(i).getRule(j);
                        handleContionalFormatCell(rule, cell);
                    }
                    return Boolean.valueOf(true);
                }
            }
        }
    }
    return Boolean.valueOf(false);
}

From source file:com.asakusafw.testtools.excel.ExcelUtils.java

License:Apache License

/**
 * ???????Exception??// w w  w .  j  av  a2  s. co  m
 * @param cell ?
 * @param msg 
 * @return Exception?
 */
private String createExceptionMsg(HSSFCell cell, String msg) {
    int col = cell.getColumnIndex();
    int rownum = cell.getRowIndex();
    String sheetName = cell.getSheet().getSheetName();
    String fmt = "%s, filename = %s, sheet = %s, row = %d, col = %d";
    String ret = String.format(fmt, msg, filename, sheetName, rownum + 1, col + 1);
    return ret;

}

From source file:com.asakusafw.testtools.excel.ExcelUtils.java

License:Apache License

/**
 * ?????????Exception???//from w  w  w  . jav  a  2  s  . c  om
 * @param cell 
 * @param expect ??
 * @return Exception?
 */
private String createCellTypeMismatchExceptionMsg(HSSFCell cell, String expect) {
    int col = cell.getColumnIndex();
    int rownum = cell.getRowIndex();
    String sheetName = cell.getSheet().getSheetName();
    String actual;
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        actual = "";
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        actual = "?";
        break;
    case Cell.CELL_TYPE_ERROR:
        actual = "";
        break;
    case Cell.CELL_TYPE_FORMULA:
        actual = "?";
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            actual = "";
        } else {
            actual = "";
        }
        break;
    case Cell.CELL_TYPE_STRING:
        actual = "";
        break;
    default:
        actual = "?";
        break;
    }
    String fmt = "Excel CELL??????????"
            + "expect = %s, actual = %s,  filename = %s, sheet = %s, row = %d, col = %d";
    String ret = String.format(fmt, expect, actual, filename, sheetName, rownum + 1, col + 1);
    return ret;
}

From source file:com.haulmont.yarg.formatters.impl.inline.AbstractInliner.java

License:Apache License

@Override
public void inlineToXls(HSSFPatriarch patriarch, HSSFCell resultCell, Object paramValue,
        Matcher paramsMatcher) {/*from   www . j a  v a  2 s. c o m*/
    try {
        Image image = new Image(paramValue, paramsMatcher);
        if (image.isValid()) {
            HSSFSheet sheet = resultCell.getSheet();
            HSSFWorkbook workbook = sheet.getWorkbook();

            int pictureIdx = workbook.addPicture(image.imageContent, Workbook.PICTURE_TYPE_JPEG);

            CreationHelper helper = workbook.getCreationHelper();
            ClientAnchor anchor = helper.createClientAnchor();
            anchor.setCol1(resultCell.getColumnIndex());
            anchor.setRow1(resultCell.getRowIndex());
            anchor.setCol2(resultCell.getColumnIndex());
            anchor.setRow2(resultCell.getRowIndex());
            if (patriarch == null) {
                throw new IllegalArgumentException(String.format(
                        "No HSSFPatriarch object provided. Charts on this sheet could cause this effect. Please check sheet %s",
                        resultCell.getSheet().getSheetName()));
            }
            HSSFPicture picture = patriarch.createPicture(anchor, pictureIdx);
            Dimension size = ImageUtils.getDimensionFromAnchor(picture);
            double actualHeight = size.getHeight() / EMU_PER_PIXEL;
            double actualWidth = size.getWidth() / EMU_PER_PIXEL;
            picture.resize((double) image.width / actualWidth, (double) image.height / actualHeight);
        }
    } catch (IllegalArgumentException e) {
        throw new ReportFormattingException("An error occurred while inserting bitmap to xls file", e);
    }
}

From source file:com.haulmont.yarg.formatters.impl.xls.hints.CustomCellStyleHint.java

License:Apache License

@Override
public void apply() {
    for (DataObject dataObject : data) {
        HSSFCell templateCell = dataObject.templateCell;
        HSSFCell resultCell = dataObject.resultCell;
        BandData bandData = dataObject.bandData;

        HSSFWorkbook resultWorkbook = resultCell.getSheet().getWorkbook();
        HSSFWorkbook templateWorkbook = templateCell.getSheet().getWorkbook();

        String templateCellValue = templateCell.getStringCellValue();

        Matcher matcher = pattern.matcher(templateCellValue);
        if (matcher.find()) {
            String paramName = matcher.group(1);
            String styleName = (String) bandData.getParameterValue(paramName);
            if (styleName == null)
                continue;

            HSSFCellStyle cellStyle = styleCache.getStyleByName(styleName);
            if (cellStyle == null)
                continue;

            HSSFCellStyle resultStyle = styleCache.getNamedCachedStyle(cellStyle);

            if (resultStyle == null) {
                HSSFCellStyle newStyle = resultWorkbook.createCellStyle();
                // color
                newStyle.setFillBackgroundColor(cellStyle.getFillBackgroundColor());
                newStyle.setFillForegroundColor(cellStyle.getFillForegroundColor());
                newStyle.setFillPattern(cellStyle.getFillPattern());

                // borders
                newStyle.setBorderLeft(cellStyle.getBorderLeft());
                newStyle.setBorderRight(cellStyle.getBorderRight());
                newStyle.setBorderTop(cellStyle.getBorderTop());
                newStyle.setBorderBottom(cellStyle.getBorderBottom());

                // border colors
                newStyle.setLeftBorderColor(cellStyle.getLeftBorderColor());
                newStyle.setRightBorderColor(cellStyle.getRightBorderColor());
                newStyle.setBottomBorderColor(cellStyle.getBottomBorderColor());
                newStyle.setTopBorderColor(cellStyle.getTopBorderColor());

                // alignment
                newStyle.setAlignment(cellStyle.getAlignment());
                newStyle.setVerticalAlignment(cellStyle.getVerticalAlignment());
                // misc
                DataFormat dataFormat = resultWorkbook.getCreationHelper().createDataFormat();
                newStyle.setDataFormat(dataFormat.getFormat(cellStyle.getDataFormatString()));
                newStyle.setHidden(cellStyle.getHidden());
                newStyle.setLocked(cellStyle.getLocked());
                newStyle.setIndention(cellStyle.getIndention());
                newStyle.setRotation(cellStyle.getRotation());
                newStyle.setWrapText(cellStyle.getWrapText());
                // font
                HSSFFont cellFont = cellStyle.getFont(templateWorkbook);
                HSSFFont newFont = fontCache.getFontByTemplate(cellFont);

                if (newFont == null) {
                    newFont = resultWorkbook.createFont();

                    newFont.setFontName(cellFont.getFontName());
                    newFont.setItalic(cellFont.getItalic());
                    newFont.setStrikeout(cellFont.getStrikeout());
                    newFont.setTypeOffset(cellFont.getTypeOffset());
                    newFont.setBoldweight(cellFont.getBoldweight());
                    newFont.setCharSet(cellFont.getCharSet());
                    newFont.setColor(cellFont.getColor());
                    newFont.setUnderline(cellFont.getUnderline());
                    newFont.setFontHeight(cellFont.getFontHeight());
                    newFont.setFontHeightInPoints(cellFont.getFontHeightInPoints());
                    fontCache.addCachedFont(cellFont, newFont);
                }/*from   w  w w .  j  a  v  a  2 s .co  m*/
                newStyle.setFont(newFont);

                resultStyle = newStyle;
                styleCache.addCachedNamedStyle(cellStyle, resultStyle);
            }

            fixNeighbourCellBorders(cellStyle, resultCell);

            resultCell.setCellStyle(resultStyle);

            Sheet sheet = resultCell.getSheet();
            for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
                CellRangeAddress mergedRegion = sheet.getMergedRegion(i);
                if (mergedRegion.isInRange(resultCell.getRowIndex(), resultCell.getColumnIndex())) {

                    int firstRow = mergedRegion.getFirstRow();
                    int lastRow = mergedRegion.getLastRow();
                    int firstCol = mergedRegion.getFirstColumn();
                    int lastCol = mergedRegion.getLastColumn();

                    for (int row = firstRow; row <= lastRow; row++)
                        for (int col = firstCol; col <= lastCol; col++)
                            sheet.getRow(row).getCell(col).setCellStyle(resultStyle);

                    // cell includes only in one merged region
                    break;
                }
            }
        }
    }
}

From source file:com.haulmont.yarg.formatters.impl.xls.hints.CustomCellStyleHint.java

License:Apache License

private void fixNeighbourCellBorders(HSSFCellStyle cellStyle, HSSFCell resultCell) {
    HSSFSheet sheet = resultCell.getRow().getSheet();
    // disable neighboring cells border
    int columnIndex = resultCell.getColumnIndex();
    int rowIndex = resultCell.getRowIndex();
    // fix left border
    fixLeftBorder(cellStyle, sheet, columnIndex, resultCell);

    // fix right border
    fixRightBorder(cellStyle, sheet, columnIndex, resultCell);

    // fix up border
    fixUpBorder(cellStyle, sheet, columnIndex, rowIndex, resultCell);

    // fix down border
    fixDownBorder(cellStyle, sheet, columnIndex, rowIndex, resultCell);
}

From source file:com.haulmont.yarg.formatters.impl.xls.hints.CustomCellStyleHint.java

License:Apache License

private void fixLeftBorder(HSSFCellStyle cellStyle, HSSFSheet sheet, int columnIndex, HSSFCell resultCell) {
    if (columnIndex > 1) {
        fixLeftCell(sheet, resultCell.getRowIndex(), columnIndex - 1, cellStyle);
        // fix merged left border
        for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
            CellRangeAddress mergedRegion = sheet.getMergedRegion(i);
            if (mergedRegion.isInRange(resultCell.getRowIndex(), resultCell.getColumnIndex())) {
                int firstRow = mergedRegion.getFirstRow();
                int lastRow = mergedRegion.getLastRow();

                for (int leftIndex = firstRow; leftIndex <= lastRow; leftIndex++) {
                    fixLeftCell(sheet, leftIndex, columnIndex - 1, cellStyle);
                }/*from  w  w w . ja  va  2s.  c  o  m*/
                break;
            }
        }
    }
}

From source file:com.haulmont.yarg.formatters.impl.xls.hints.CustomCellStyleHint.java

License:Apache License

private void fixRightBorder(HSSFCellStyle cellStyle, HSSFSheet sheet, int columnIndex, HSSFCell resultCell) {
    fixRightCell(sheet, resultCell.getRowIndex(), columnIndex + 1, cellStyle);
    // fix merged right border
    for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
        CellRangeAddress mergedRegion = sheet.getMergedRegion(i);
        if (mergedRegion.isInRange(resultCell.getRowIndex(), resultCell.getColumnIndex())) {
            int firstRow = mergedRegion.getFirstRow();
            int lastRow = mergedRegion.getLastRow();
            int regionWidth = mergedRegion.getLastColumn() - mergedRegion.getFirstColumn() + 1;

            for (int rightIndex = firstRow; rightIndex <= lastRow; rightIndex++) {
                fixRightCell(sheet, rightIndex, columnIndex + regionWidth, cellStyle);
            }//w  ww.  java  2 s.c o m
            break;
        }
    }
}

From source file:com.haulmont.yarg.formatters.impl.xls.hints.CustomCellStyleHint.java

License:Apache License

private void fixUpBorder(HSSFCellStyle cellStyle, HSSFSheet sheet, int columnIndex, int rowIndex,
        HSSFCell resultCell) {
    if (rowIndex > 0) {
        // fix simple up border
        fixUpCell(sheet, rowIndex - 1, columnIndex, cellStyle);
        // fix merged up border
        for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
            CellRangeAddress mergedRegion = sheet.getMergedRegion(i);
            if (mergedRegion.isInRange(resultCell.getRowIndex(), resultCell.getColumnIndex())) {
                int firstColumn = mergedRegion.getFirstColumn();
                int lastColumn = mergedRegion.getLastColumn();

                for (int upIndex = firstColumn; upIndex <= lastColumn; upIndex++) {
                    fixUpCell(sheet, rowIndex - 1, upIndex, cellStyle);
                }//from w  ww.  j a  va  2 s. com
                break;
            }
        }
    }
}