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

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

Introduction

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

Prototype

public HSSFSheet getSheet() 

Source Link

Document

Returns the HSSFSheet this cell belongs to

Usage

From source file:org.jxstar.report.util.ReportXlsUtil.java

/**
 * ??POI?ReportImageUtil/* w  ww.j  ava 2 s . c om*/
 * @param cell -- ?
 * @param bytes -- 
 */
public static void addImageToSheet(HSSFCell cell, byte[] bytes) {
    if (cell == null) {
        _log.showError("-----insertImageToSheet: cell is null!");
        return;
    }
    if (bytes == null || bytes.length == 0) {
        _log.showError("-----insertImageToSheet: bytes is null!");
        return;
    }

    //??
    HSSFSheet sheet = cell.getSheet();

    //?
    int firstRow = cell.getRowIndex();
    int lastRow = cell.getRowIndex();
    int firstCol = cell.getColumnIndex();
    int lastCol = cell.getColumnIndex();
    //??
    CellRangeAddress range = getMergedRegion(cell);
    if (range != null) {
        firstRow = range.getFirstRow();
        lastRow = range.getLastRow();
        firstCol = range.getFirstColumn();
        lastCol = range.getLastColumn();
    }
    _log.showDebug("---------image cells=[" + firstRow + "," + firstCol + "," + lastRow + "," + lastCol + "]");
    //????5??1023255
    HSSFClientAnchor anchor = new HSSFClientAnchor(10, 5, 1013, 250, (short) firstCol, firstRow,
            (short) lastCol, lastRow);
    anchor.setAnchorType(HSSFClientAnchor.MOVE_AND_RESIZE);

    //??
    HSSFPatriarch draw = sheet.getDrawingPatriarch();
    if (draw == null) {
        draw = sheet.createDrawingPatriarch();
    }

    //???
    sheet.getWorkbook().addPicture(bytes, HSSFWorkbook.PICTURE_TYPE_JPEG);

    //???????+1??
    List<HSSFPicture> lsPicture = getAllPicture(sheet);
    int index = lsPicture.size() + 1;
    _log.showDebug("---------new image index=" + index);

    draw.createPicture(anchor, index);
}

From source file:org.jxstar.report.util.ReportXlsUtil.java

/**
 * ????// w  w  w .jav  a 2s.  com
 * @param cell -- ?
 * @return
 */
private static CellRangeAddress getMergedRegion(HSSFCell cell) {
    HSSFSheet sheet = cell.getSheet();

    CellRangeAddress range = null;
    int mergedNum = sheet.getNumMergedRegions();
    for (int i = 0; i < mergedNum; i++) {
        range = sheet.getMergedRegion(i);
        if (range.getFirstColumn() == cell.getColumnIndex() && range.getFirstRow() == cell.getRowIndex()) {
            return range;
        }
    }
    return null;
}

From source file:org.jxstar.report.util.XlsToHtml.java

/**
 * ?xls?html?//from  ww w .  j  a  v a  2 s  .  co m
 * @param cell -- xls?
 * @return
 */
private String getCellStyle(HSSFCell cell) {
    HSSFCellStyle style = cell.getCellStyle();
    HSSFSheet sheet = cell.getSheet();
    String value = getCellValue(cell);

    //?
    StringBuilder sbStyle = new StringBuilder();

    //??
    sbStyle.append(getBorderStyle(style));

    //???
    sbStyle.append(getAlignStyle(style));

    //??
    HSSFFont font = style.getFont(sheet.getWorkbook());
    sbStyle.append(getFontStyle(font));

    //cell
    if (value == null || value.length() == 0) {
        sbStyle.append("color:red;");
    }

    return sbStyle.toString();
}

From source file:ro.nextreports.engine.exporter.util.XlsUtil.java

License:Apache License

/**
 * Copy a cell to another cell//from   ww  w.j  a  va 2  s  . com
 * 
 * @param oldCell cell to be copied
 * @param newCell cell to be created
 * @param styleMap style map
 */
public static void copyCell(HSSFCell oldCell, HSSFCell newCell, Map<Integer, HSSFCellStyle> styleMap) {
    if (styleMap != null) {
        if (oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()) {
            newCell.setCellStyle(oldCell.getCellStyle());
        } else {
            int stHashCode = oldCell.getCellStyle().hashCode();
            HSSFCellStyle newCellStyle = styleMap.get(stHashCode);
            if (newCellStyle == null) {
                newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
                newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
                styleMap.put(stHashCode, newCellStyle);
            }
            newCell.setCellStyle(newCellStyle);
        }
    }
    switch (oldCell.getCellType()) {
    case HSSFCell.CELL_TYPE_STRING:
        newCell.setCellValue(oldCell.getStringCellValue());
        break;
    case HSSFCell.CELL_TYPE_NUMERIC:
        newCell.setCellValue(oldCell.getNumericCellValue());
        break;
    case HSSFCell.CELL_TYPE_BLANK:
        newCell.setCellType(HSSFCell.CELL_TYPE_BLANK);
        break;
    case HSSFCell.CELL_TYPE_BOOLEAN:
        newCell.setCellValue(oldCell.getBooleanCellValue());
        break;
    case HSSFCell.CELL_TYPE_ERROR:
        newCell.setCellErrorValue(oldCell.getErrorCellValue());
        break;
    case HSSFCell.CELL_TYPE_FORMULA:
        newCell.setCellFormula(oldCell.getCellFormula());
        break;
    default:
        break;
    }

}

From source file:swift.selenium.WebHelper.java

License:Open Source License

/**
 * This method reads and returns data from each cell of a provided worksheet
 * //from  w ww. j  a  va2  s  .com
 * @param reqValue
 * @param reqSheet
 * @param rowIndex
 * @param inputHashTable
 * @return
 * @throws IOException
 */
@SuppressWarnings("null")
public static String getCellData(String reqValue, HSSFSheet reqSheet, int rowIndex,
        HashMap<String, Object> inputHashTable) throws IOException {
    HSSFCell reqCell = null;
    Object actualvalue = null;
    String req = "";
    DataFormatter fmt = new DataFormatter();

    if (inputHashTable.isEmpty() == true) {
        inputHashTable = getValueFromHashMap(reqSheet);
    }
    HSSFRow rowActual = reqSheet.getRow(rowIndex);
    if (inputHashTable.get(reqValue) == null) {

        TransactionMapping.report.setStrMessage("Column " + reqValue + " not Found. Please Check input Sheet");
        TransactionMapping.pauseFun("Column " + reqValue + " not Found. Please Check input Sheet");
    } else {
        actualvalue = inputHashTable.get(reqValue);//rowHeader.getCell(colIndex).toString();         
        if (actualvalue != null) {
            int colIndex = Integer.parseInt(actualvalue.toString());
            reqCell = rowActual.getCell(colIndex);
            //TM 27-04-2015: Updated the code for formula in cells
            if (reqCell == null) {
                System.out.println(reqValue + " is Null");
            } else {
                HSSFWorkbook wb = reqCell.getSheet().getWorkbook(); //TM-05/05/2015: Get workbook instance from the worksheet
                HSSFFormulaEvaluator.evaluateAllFormulaCells(wb); //TM-05/05/2015: To refresh all the formulas in the worksheet
                FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();

                CellValue cellValue = evaluator.evaluate(reqCell);
                int type = 0;
                if (cellValue != null) {
                    type = cellValue.getCellType();
                } else {
                    type = reqCell.getCellType();
                }

                switch (type) {
                case HSSFCell.CELL_TYPE_BLANK:
                    req = "";
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(reqCell)) {
                        SimpleDateFormat form = new SimpleDateFormat(
                                Automation.configHashMap.get("DATEFORMAT").toString());
                        req = form.format(reqCell.getDateCellValue());
                    } else
                        req = fmt.formatCellValue(reqCell, evaluator);
                    break;
                case HSSFCell.CELL_TYPE_STRING:
                    req = reqCell.getStringCellValue();
                    break;
                case HSSFCell.CELL_TYPE_BOOLEAN:
                    req = Boolean.toString(reqCell.getBooleanCellValue());
                    break;
                case HSSFCell.CELL_TYPE_ERROR:
                    req = "error";
                    break;
                }
            }
        }

        else {
            req = reqCell.getStringCellValue();
            System.out.println("null");
        }
    }
    return req;
}

From source file:UI.Helpers.ExcelHelper.java

private static boolean checkStrikeThrough(HSSFCell cell) {
    CellStyle style = cell.getCellStyle();
    HSSFFont cellFont = cell.getSheet().getWorkbook().getFontAt(style.getFontIndex());
    boolean isStrikeout = cellFont.getStrikeout();
    return isStrikeout;
}