Example usage for org.apache.poi.ss.usermodel Cell getCellStyle

List of usage examples for org.apache.poi.ss.usermodel Cell getCellStyle

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Cell getCellStyle.

Prototype

CellStyle getCellStyle();

Source Link

Document

Return the cell's style.

Usage

From source file:org.cgiar.ccafs.ap.summaries.projects.xlsx.BaseXLS.java

License:Open Source License

/**
 * This method writes double value with format budget into a specific cell.
 * /*from   w  ww . j  a  v a 2 s. co m*/
 * @param sheet is the sheet where you want to add information into.
 * @param value is the specific information to be written.
 */
public void writeDescription(Sheet sheet, String description) {
    // Set description
    Row row = sheet.getRow(REPORT_DESCRIPTION_ROW);
    Cell cell = row.getCell(REPORT_DESCRIPTION_COLUMN);
    cell.getCellStyle().setAlignment(CellStyle.ALIGN_LEFT);
    cell.getCellStyle().setWrapText(true);
    cell.setCellValue(description);

}

From source file:org.dbflute.helper.io.xls.DfTableXlsReader.java

License:Apache License

protected boolean isCellBase64Formatted(Cell cell) {
    final CellStyle cs = cell.getCellStyle();
    final short dfNum = cs.getDataFormat();
    return DfDataSetConstants.BASE64_FORMAT.equals(_dataFormat.getFormat(dfNum));
}

From source file:org.dbflute.helper.io.xls.DfTableXlsReader.java

License:Apache License

protected boolean isCellDateFormatted(Cell cell) {
    final CellStyle cs = cell.getCellStyle();
    final short dfNum = cs.getDataFormat();
    final String format = _dataFormat.getFormat(dfNum);
    if (format == null || format.length() == 0) {
        return false;
    }/* ww w.  ja  va 2  s .c  om*/
    if (format.indexOf('/') > 0 || format.indexOf('y') > 0 || format.indexOf('m') > 0
            || format.indexOf('d') > 0) {
        return true;
    }
    return false;
}

From source file:org.dbunit.dataset.excel.MyXlsTable.java

License:Open Source License

public Object getValue(int row, String column) throws DataSetException {
    if (logger.isDebugEnabled())
        logger.debug("getValue(row={}, columnName={}) - start", Integer.toString(row), column);

    assertValidRowIndex(row);//w ww. j  a v  a  2s.  c  om

    int columnIndex = getColumnIndex(column);
    Row rowObject = _sheet.getRow(row + 1);
    if (rowObject == null) {
        return null;
    }
    Cell cell = rowObject.getCell(columnIndex);
    if (cell == null) {
        return null;
    }

    int type = cell.getCellType();
    switch (type) {
    case Cell.CELL_TYPE_NUMERIC:
        CellStyle style = cell.getCellStyle();
        if (DateUtil.isCellDateFormatted(cell)) {
            return getDateValue(cell);
        } else if (XlsDataSetWriter.DATE_FORMAT_AS_NUMBER_DBUNIT.equals(style.getDataFormatString())) {
            // The special dbunit date format
            return getDateValueFromJavaNumber(cell);
        } else {
            return getNumericValue(cell);
        }

    case Cell.CELL_TYPE_STRING:
        return cell.getRichStringCellValue().getString();

    case Cell.CELL_TYPE_FORMULA:
        throw new DataTypeException("Formula not supported at row=" + row + ", column=" + column);

    case Cell.CELL_TYPE_BLANK:
        return null;

    case Cell.CELL_TYPE_BOOLEAN:
        return cell.getBooleanCellValue() ? Boolean.TRUE : Boolean.FALSE;

    case Cell.CELL_TYPE_ERROR:
        throw new DataTypeException("Error at row=" + row + ", column=" + column);

    default:
        throw new DataTypeException("Unsupported type at row=" + row + ", column=" + column);
    }
}

From source file:org.dbunit.dataset.excel.MyXlsTable.java

License:Open Source License

protected BigDecimal getNumericValue(Cell cell) {
    logger.debug("getNumericValue(cell={}) - start", cell);

    String formatString = cell.getCellStyle().getDataFormatString();
    String resultString = null;/*ww  w.j a  v  a2 s  .  c o m*/
    double cellValue = cell.getNumericCellValue();

    if ((formatString != null)) {
        if (!formatString.equals("General") && !formatString.equals("@")) {
            logger.debug("formatString={}", formatString);
            DecimalFormat nf = new DecimalFormat(formatString, symbols);
            resultString = nf.format(cellValue);
        }
    }

    BigDecimal result;
    if (resultString != null) {
        try {
            result = new BigDecimal(resultString);
        } catch (NumberFormatException e) {
            logger.debug("Exception occurred while trying create a BigDecimal. value={}", resultString);
            // Probably was not a BigDecimal format retrieved from the excel. Some
            // date formats are not yet recognized by HSSF as DateFormats so that
            // we could get here.
            result = toBigDecimal(cellValue);
        }
    } else {
        result = toBigDecimal(cellValue);
    }
    return result;
}

From source file:org.diffkit.diff.sns.DKPoiSheet.java

License:Apache License

@SuppressWarnings("unchecked")
private List<Type> discoverColumnTypes(List<Row> rows_) {
    if (CollectionUtils.isEmpty(rows_))
        return null;
    List<Type> columnTypes = GrowthList
            .decorate(LazyList.decorate(new ArrayList<Type>(), FactoryUtils.nullFactory()));
    int start = this.hasHeader() ? 1 : 0;
    for (int i = start; i < rows_.size(); i++) {
        Row aRow = rows_.get(i);/* w  w w  .  j  a va2  s. c  o m*/
        int width = aRow.getLastCellNum();
        for (int j = 0; j < width; j++) {
            Cell cell = aRow.getCell(j);
            if (cell == null)
                continue;
            if (_isDebugEnabled) {
                _log.debug(String.format("cell->%s formatString->%s format->%s", cell.getColumnIndex(),
                        cell.getCellStyle().getDataFormatString(), cell.getCellStyle().getDataFormat()));
            }
            Type cellType = mapColumnType(cell);
            Type columnType = columnTypes.get(j);
            if (_isDebugEnabled)
                _log.debug("cellType->{} columnType->{}", cellType, columnType);
            if (columnType == null)
                columnTypes.set(j, cellType);
            else if (columnType != cellType)
                columnTypes.set(j, Type.MIXED);
        }
    }
    return columnTypes;
}

From source file:org.diffkit.diff.sns.DKPoiSheet.java

License:Apache License

/**
 * guaranteed to not return null-- defaults to DECIMAL as last resort
 *//*ww  w  .j a  va  2s. c  om*/
private static Type getTypeForNumericCell(Cell cell_) {
    if (IS_DEBUG_ENABLED)
        LOG.debug("cell_->{}", cell_);
    if (cell_ == null)
        return null;
    int formatNumber = cell_.getCellStyle().getDataFormat();
    // try to get it from the cache
    Type type = _numericCellTypes.get(formatNumber);
    if (type != null)
        return type;
    String dataFormatString = cell_.getCellStyle().getDataFormatString();
    if (IS_DEBUG_ENABLED)
        LOG.debug("dataFormatString->{}", dataFormatString);
    type = mapTypeForFormatString(dataFormatString);
    if (IS_DEBUG_ENABLED)
        LOG.debug("type->{}", type);
    if (type == null)
        type = Type.DECIMAL;
    // cache calculated value
    _numericCellTypes.put(formatNumber, type);
    return type;
}

From source file:org.drugepi.table.ExcelUtils.java

License:Mozilla Public License

private static boolean cellIsColor(Cell cell, ColorRepresentation color) {
    if (cell == null)
        return false;

    CellStyle style = cell.getCellStyle();

    Color bgColor = style.getFillForegroundColorColor();

    if (bgColor instanceof XSSFColor) {
        XSSFColor xssfBgColor = (XSSFColor) bgColor;
        //         System.out.printf("Cell color is %s index is %d %d\n", xssfBgColor.getARGBHex(), xssfBgColor.getRgb()[0], xssfBgColor.getIndexed());
        return xssfBgColor.getARGBHex().equalsIgnoreCase(color.xssfArgb);
    } else if (bgColor instanceof HSSFColor) {
        HSSFColor hssfBgColor = (HSSFColor) bgColor;
        //         System.out.printf("Cell color is %s\n", hssfBgColor.getHexString());
        return hssfBgColor.getHexString().equalsIgnoreCase(color.hssfHexString);
    } else/*  w ww.j  av  a2  s.  com*/
        return false;
}

From source file:org.drugepi.table.ExcelUtils.java

License:Mozilla Public License

public static boolean cellIsBold(Cell cell) {
    if (cell == null)
        return false;

    Row row = cell.getRow();//from w  w w. j  ava  2s.c o  m
    Sheet sheet = row.getSheet();
    Workbook workbook = sheet.getWorkbook();
    Font font = workbook.getFontAt(cell.getCellStyle().getFontIndex());

    if (font.getBoldweight() == Font.BOLDWEIGHT_BOLD)
        return true;

    return false;
}

From source file:org.drugepi.table.ExcelUtils.java

License:Mozilla Public License

public static void restyleCell(CellStyleLookup csl, Cell cell) {
    CellStyle origStyle = cell.getCellStyle();

    CellStyle newStyle = csl.getExistingStyle(origStyle);
    if (newStyle == null) {
        newStyle = cell.getRow().getSheet().getWorkbook().createCellStyle();
        newStyle.cloneStyleFrom(origStyle);
        newStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        csl.putNewStyle(origStyle, newStyle);
    }//  w ww  .  j av a2  s .  co  m

    cell.setCellStyle(newStyle);
}