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.tiefaces.components.websheet.utility.CellStyleUtility.java

License:MIT License

/**
 * Setup cell style.//from  www. j  a v  a2 s  . co m
 *
 * @param wb
 *            the wb
 * @param fcell
 *            the fcell
 * @param poiCell
 *            the poi cell
 * @param rowHeight
 *            the row height
 */
public static void setupCellStyle(final Workbook wb, final FacesCell fcell, final Cell poiCell,
        final float rowHeight) {

    CellStyle cellStyle = poiCell.getCellStyle();
    if ((cellStyle != null) && (!cellStyle.getLocked())) {
        // not locked
        if (fcell.getInputType().isEmpty()) {
            fcell.setInputType(CellStyleUtility.getInputTypeFromCellType(poiCell));
        }
        if (fcell.getControl().isEmpty() && (!fcell.getInputType().isEmpty())) {
            fcell.setControl("text");
        }
        setInputStyleBaseOnInputType(fcell, poiCell);

    }
    String webStyle = getCellStyle(wb, poiCell, fcell.getInputType()) + getCellFontStyle(wb, poiCell)
            + getRowStyle(wb, poiCell, fcell.getInputType(), rowHeight, fcell.getRowspan());
    fcell.setStyle(webStyle);
    fcell.setColumnStyle(getColumnStyle(wb, fcell, poiCell, rowHeight));
}

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

License:MIT License

/**
 * Gets the input type from cell type.//  ww  w.j  a  v  a2  s.c  om
 *
 * @param cell
 *            the cell
 * @return the input type from cell type
 */
@SuppressWarnings("deprecation")
private static String getInputTypeFromCellType(final Cell cell) {

    String inputType = TieConstants.CELL_INPUT_TYPE_TEXT;
    if (cell.getCellTypeEnum() == CellType.NUMERIC) {
        inputType = TieConstants.CELL_INPUT_TYPE_DOUBLE;
    }
    CellStyle style = cell.getCellStyle();
    if (style != null) {
        int formatIndex = style.getDataFormat();
        String formatString = style.getDataFormatString();
        if (DateUtil.isADateFormat(formatIndex, formatString)) {
            inputType = TieConstants.CELL_INPUT_TYPE_DATE;
        } else {
            if (isAPercentageCell(formatString)) {
                inputType = TieConstants.CELL_INPUT_TYPE_PERCENTAGE;
            }
        }
    }
    return inputType;
}

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

License:MIT License

/**
 * get decimal places from format string e.g. 0.00 will return 2
 *
 * @param cell/* w  w  w  .  j  a  va  2s . c  o  m*/
 *            the cell
 * @return decimal places of the formatted string
 */
private static short getDecimalPlacesFromFormat(final Cell cell) {
    CellStyle style = cell.getCellStyle();
    if (style == null) {
        return 0;
    }
    String formatString = style.getDataFormatString();
    if (formatString == null) {
        return 0;
    }
    int ipos = formatString.indexOf('.');
    if (ipos < 0) {
        return 0;
    }
    short counter = 0;
    for (int i = ipos + 1; i < formatString.length(); i++) {
        if (formatString.charAt(i) == '0') {
            counter++;
        } else {
            break;
        }
    }
    return counter;
}

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

License:MIT License

/**
 * get symbol from format string e.g. [$CAD] #,##0.00 will return CAD. While
 * $#,##0.00 will return $/*from w w  w.j a  va 2  s. c  om*/
 *
 * @param cell
 *            the cell
 * @return symbol of the formatted string
 */
private static String getSymbolFromFormat(final Cell cell) {
    CellStyle style = cell.getCellStyle();
    if (style == null) {
        return null;
    }
    String formatString = style.getDataFormatString();
    if (formatString == null) {
        return null;
    }
    if (formatString.indexOf(TieConstants.CELL_ADDR_PRE_FIX) < 0) {
        return null;
    }
    int ipos = formatString.indexOf("[$");
    if (ipos < 0) {
        // only $ found, then return default dollar symbol
        return "$";
    }
    // return specified dollar symbol
    return formatString.substring(ipos + 2, formatString.indexOf(']', ipos));
}

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

License:MIT License

/**
 * get symbol position from format string e.g. [$CAD] #,##0.00 will return
 * p. While #,##0.00 $ will return s/*from w  w w. j  a v a  2 s  .co m*/
 *
 * @param cell
 *            the cell
 * @return symbol position of the formatted string
 */
private static String getSymbolPositionFromFormat(final Cell cell) {
    CellStyle style = cell.getCellStyle();
    if (style == null) {
        return "p";
    }
    String formatString = style.getDataFormatString();
    if (formatString == null) {
        return "p";
    }
    int symbolpos = formatString.indexOf('$');
    int numberpos = formatString.indexOf('#');
    if (numberpos < 0) {
        numberpos = formatString.indexOf('0');
    }
    if (symbolpos < numberpos) {
        return "p";
    } else {
        return "s";
    }
}

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

License:MIT License

/**
 * set cell value.//  w  w  w  .j ava2  s.  c om
 * 
 * @param sourceCell
 *            source cell.
 * @param newCell
 *            new cell.
 * @param checkLock
 *            check lock flag.
 */
@SuppressWarnings("deprecation")
private static void copyCellSetValue(final Cell sourceCell, final Cell newCell, final boolean checkLock) {

    CellStyle newCellStyle = newCell.getCellStyle();
    String name = sourceCell.getCellTypeEnum().toString();
    CellValueType e = Enum.valueOf(CellValueType.class, name);
    e.setCellValue(newCell, sourceCell, checkLock, newCellStyle);
}

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

License:MIT License

/**
 * create cell style from source cell.//from  w ww.  j a  va  2 s.c om
 * 
 * @param destSheet
 *            dest sheet.
 * @param sourceCell
 *            source cell.
 * @return cell style.
 */
private static CellStyle getCellStyleFromSourceCell(final Sheet destSheet, final Cell sourceCell) {
    Workbook wb = destSheet.getWorkbook();
    // Copy style from old cell and apply to new cell
    CellStyle newCellStyle = wb.createCellStyle();
    newCellStyle.cloneStyleFrom(sourceCell.getCellStyle());
    return newCellStyle;
}

From source file:org.wandora.application.tools.extractors.excel.AbstractExcelExtractor.java

License:Open Source License

protected String getCellValueAsString(Cell cell, int type) {
    if (cell != null) {
        switch (type) {
        case Cell.CELL_TYPE_ERROR: {
            return "ERROR" + cell.getErrorCellValue();
        }//from ww  w . java  2  s  . c o  m
        case Cell.CELL_TYPE_BOOLEAN: {
            return "" + cell.getBooleanCellValue();
        }
        case Cell.CELL_TYPE_NUMERIC: {
            if (DateUtil.isCellDateFormatted(cell)) {
                return dateFormat.format(cell.getDateCellValue());
            } else {
                double value = cell.getNumericCellValue();
                String formatString = cell.getCellStyle().getDataFormatString();
                int formatIndex = cell.getCellStyle().getDataFormat();
                return formatter.formatRawCellContents(value, formatIndex, formatString);
            }
        }
        case Cell.CELL_TYPE_STRING: {
            return cell.getRichStringCellValue().getString();
        }
        }
    }
    return null;
}

From source file:org.wandora.application.tools.extractors.excel.AbstractExcelExtractor.java

License:Open Source License

public void associateToColors(Cell cell, TopicMap tm) throws TopicMapException {
    if (cell.getCellStyle() != null) {
        Topic colorTypeTopic = getBackgroundColorTypeTopic(tm);
        Topic colorTopic = getColorTopic(cell, tm);
        Topic cellTypeTopic = getCellTypeTopic(tm);
        Topic cellTopic = getCellTopic(cell, tm);

        if (colorTypeTopic != null && colorTopic != null && cellTypeTopic != null && cellTopic != null) {
            Association a = tm.createAssociation(colorTypeTopic);
            a.addPlayer(cellTopic, cellTypeTopic);
            a.addPlayer(colorTopic, colorTypeTopic);
        }/*  w  w w. j a v  a 2s .  c  o m*/

        colorTypeTopic = getForegroundColorTypeTopic(tm);
        colorTopic = getColorTopic(cell, tm);

        if (colorTypeTopic != null && colorTopic != null && cellTypeTopic != null && cellTopic != null) {
            Association a = tm.createAssociation(colorTypeTopic);
            a.addPlayer(cellTopic, cellTypeTopic);
            a.addPlayer(colorTopic, colorTypeTopic);
        }
    }
}

From source file:org.wandora.application.tools.extractors.excel.AbstractExcelExtractor.java

License:Open Source License

public Topic getColorTopic(Cell cell, TopicMap tm) throws TopicMapException {
    CellStyle style = cell.getCellStyle();
    int color = style.getFillBackgroundColor();
    String si = EXCEL_COLOR_SI_PREFIX + "/" + urlEncode(Integer.toString(color));
    Topic topic = getOrCreateTopic(tm, si, "Color " + color);
    topic.addType(getColorTypeTopic(tm));
    return topic;
}