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.joeffice.spreadsheet.actions.FormatAction.java

License:Apache License

@Override
public void actionPerformed(ActionEvent ae) {
    SpreadsheetTopComponent currentTopComponent = OfficeTopComponent
            .getSelectedComponent(SpreadsheetTopComponent.class);
    if (currentTopComponent != null) {
        JTable currentTable = currentTopComponent.getSelectedTable();
        SheetTableModel tableModel = (SheetTableModel) currentTable.getModel();
        List<Cell> selectedCells = CellUtils.getSelectedCells(currentTable);
        if (selectedCells.isEmpty()) {
            return;
        }/*from   ww  w  .j  a  va2  s .c o  m*/
        if (choosePattern) {
            pattern = askFromList();
        } else if (definePattern) {
            pattern = askFromInputField();
        }
        if (pattern == null) {
            return;
        }
        Workbook workbook = selectedCells.get(0).getSheet().getWorkbook();
        DataFormat format = workbook.createDataFormat();
        short formatIndex = format.getFormat(pattern);
        for (Cell cell : selectedCells) {
            cell.getCellStyle().setDataFormat(formatIndex);
            tableModel.fireTableCellUpdated(cell.getRowIndex(), cell.getColumnIndex());
        }
    }
}

From source file:org.joeffice.spreadsheet.cell.CellBorder.java

License:Apache License

public CellBorder(Cell cell) {
    style = cell.getCellStyle();
}

From source file:org.joeffice.spreadsheet.cell.CellRenderer.java

License:Apache License

public void decorateLabel(Cell cell, JLabel defaultRenderer) {

    // Text/*w  ww.ja  va  2s . c o  m*/
    // String text = getFormattedText(cell);
    // XXX small bug with decimal not using the correct comma's
    if (cell.getCellType() == Cell.CELL_TYPE_FORMULA && formulaEvaluator == null) {
        formulaEvaluator = cell.getRow().getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
    }
    String text = DATA_FORMATTER.formatCellValue(cell, formulaEvaluator);
    setText(text);

    decorateComponent(cell, this, defaultRenderer);

    // Alignment
    CellStyle style = cell.getCellStyle();
    short alignment = style.getAlignment();
    if (alignment == CellStyle.ALIGN_CENTER || cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
        setHorizontalAlignment(SwingConstants.CENTER);
    } else if (alignment == CellStyle.ALIGN_RIGHT || cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
        setHorizontalAlignment(SwingConstants.RIGHT);
    } else {
        setHorizontalAlignment(defaultRenderer.getHorizontalAlignment());
    }
    short verticalAlignment = style.getAlignment(); // Either LibreOffice 4 or POI has problem with vertical alignment
    if (verticalAlignment == CellStyle.VERTICAL_TOP) {
        setVerticalAlignment(SwingConstants.TOP);
    } else if (verticalAlignment == CellStyle.VERTICAL_CENTER) {
        setVerticalAlignment(SwingConstants.CENTER);
    } else if (verticalAlignment == CellStyle.VERTICAL_BOTTOM) {
        setVerticalAlignment(SwingConstants.BOTTOM);
    } else {
        setVerticalAlignment(defaultRenderer.getVerticalAlignment());
    }
}

From source file:org.joeffice.spreadsheet.cell.CellRenderer.java

License:Apache License

public static void decorateComponent(Cell cell, JComponent renderingComponent, JComponent defaultRenderer) {
    CellStyle style = cell.getCellStyle();

    // Background neither the index or the color works for XSSF cells
    Color backgroundColor = CellUtils.poiToAwtColor(style.getFillBackgroundColorColor());
    if (backgroundColor != null) {
        renderingComponent.setBackground(backgroundColor);
    } else {//w w  w  . j a  va  2  s .  c om
        renderingComponent.setBackground(defaultRenderer.getBackground());
    }

    // Font and forground
    short fontIndex = style.getFontIndex();
    if (fontIndex > 0) {
        Font xlsFont = cell.getSheet().getWorkbook().getFontAt(fontIndex);
        java.awt.Font font = java.awt.Font.decode(xlsFont.getFontName());
        font = font.deriveFont((float) xlsFont.getFontHeightInPoints());
        font = font.deriveFont(java.awt.Font.PLAIN);
        if (xlsFont.getItalic()) {
            font = font.deriveFont(java.awt.Font.ITALIC);
        }
        if (xlsFont.getBoldweight() == Font.BOLDWEIGHT_BOLD) {
            font = font.deriveFont(java.awt.Font.BOLD);
        }
        if (xlsFont.getUnderline() > Font.U_NONE) {
            // no underline in fonts
        }
        short fontColorIndex = xlsFont.getColor();
        Color fontColor = CellUtils.shortToColor(fontColorIndex);
        if (fontColor != null) {
            renderingComponent.setForeground(fontColor);
        } else {
            renderingComponent.setForeground(defaultRenderer.getForeground());
        }
        renderingComponent.setFont(font);
    } else {
        renderingComponent.setForeground(defaultRenderer.getForeground());
        renderingComponent.setFont(defaultRenderer.getFont());
    }

    // Borders
    // At the moment done in renderer but should be done with a JLayer to paint over the grid
    renderingComponent.setBorder(new CellBorder(cell));

    if (cell.getCellComment() != null) {
        renderingComponent.setToolTipText(cell.getCellComment().getString().getString());
    }
}

From source file:org.joeffice.spreadsheet.cell.CellUtils.java

License:Apache License

public static void copyCell(Cell oldCell, Cell newCell) {
    newCell.setCellStyle(oldCell.getCellStyle());

    if (newCell.getCellComment() != null) {
        newCell.setCellComment(oldCell.getCellComment());
    }/* w  ww .j  a va2s  . c om*/

    if (oldCell.getHyperlink() != null) {
        newCell.setHyperlink(oldCell.getHyperlink());
    }

    newCell.setCellType(oldCell.getCellType());

    // Set the cell data value
    switch (oldCell.getCellType()) {
    case Cell.CELL_TYPE_BLANK:
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        newCell.setCellValue(oldCell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_ERROR:
        newCell.setCellErrorValue(oldCell.getErrorCellValue());
        break;
    case Cell.CELL_TYPE_FORMULA:
        newCell.setCellFormula(oldCell.getCellFormula());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        newCell.setCellValue(oldCell.getNumericCellValue());
        break;
    case Cell.CELL_TYPE_STRING:
        newCell.setCellValue(oldCell.getRichStringCellValue());
        break;
    }
}

From source file:org.joeffice.spreadsheet.TableStyleable.java

License:Apache License

/**
 * Add the attribute as defined in {@link AttributedString} to the {@link MutableAttributeSet} for the JTextPane.
 *
 * @see java.awt.font.TextAttribute//w w w.  j a  va 2 s. c  om
 */
protected void addAttribute(AttributedCharacterIterator.Attribute attribute, Object attributeValue, Cell cell) {
    CellStyle oldStyle = cell.getCellStyle();
    Workbook workbook = cell.getSheet().getWorkbook();
    CellStyle style = cell.getSheet().getWorkbook().createCellStyle();
    style.cloneStyleFrom(oldStyle);
    Font newFont = copyFont(cell);
    if (attribute == FAMILY) {
        newFont.setFontName((String) attributeValue);
        CellUtil.setFont(cell, workbook, newFont);
    } else if (attribute == FOREGROUND) {
        Color color = (Color) attributeValue;
        if (cell instanceof XSSFCell) {
            ((XSSFCellStyle) style).setFillForegroundColor(new XSSFColor(color));
        } else {
            HSSFWorkbook xlsWorkbook = (HSSFWorkbook) workbook;
            HSSFColor xlsColor = xlsWorkbook.getCustomPalette().findColor((byte) color.getRed(),
                    (byte) color.getGreen(), (byte) color.getBlue());
            if (xlsColor == null) {
                xlsColor = xlsWorkbook.getCustomPalette().addColor((byte) color.getRed(),
                        (byte) color.getGreen(), (byte) color.getBlue());
            }
            style.setFillForegroundColor(xlsColor.getIndex());
        }
        style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    } else if (attribute == BACKGROUND) {
        Color color = (Color) attributeValue;
        if (cell instanceof XSSFCell) {
            ((XSSFCellStyle) style).setFillBackgroundColor(new XSSFColor(color));
        } else {
            HSSFWorkbook xlsWorkbook = (HSSFWorkbook) workbook;
            HSSFColor xlsColor = xlsWorkbook.getCustomPalette().findColor((byte) color.getRed(),
                    (byte) color.getGreen(), (byte) color.getBlue());
            if (xlsColor == null) {
                xlsColor = xlsWorkbook.getCustomPalette().addColor((byte) color.getRed(),
                        (byte) color.getGreen(), (byte) color.getBlue());
            }
            style.setFillBackgroundColor(xlsColor.getIndex());
        }
    } else if (attribute == WEIGHT) {
        short boldValue = Font.BOLDWEIGHT_BOLD;
        if (newFont.getBoldweight() == Font.BOLDWEIGHT_BOLD) {
            boldValue = Font.BOLDWEIGHT_NORMAL;
        }
        newFont.setBoldweight(boldValue);
        CellUtil.setFont(cell, workbook, newFont);
    } else if (attribute == UNDERLINE) {
        byte underlineValue = Font.U_SINGLE;
        if (newFont.getUnderline() == Font.U_SINGLE) {
            underlineValue = Font.U_NONE;
        }
        newFont.setUnderline(underlineValue);
        CellUtil.setFont(cell, workbook, newFont);
    } else if (attribute == SUPERSCRIPT) {
        short superscriptValue = Font.SS_NONE;
        if (SUPERSCRIPT_SUB.equals(attributeValue)) {
            superscriptValue = Font.SS_SUB;
        } else if (SUPERSCRIPT_SUPER.equals(attributeValue)) {
            superscriptValue = Font.SS_SUPER;
        }
        newFont.setTypeOffset(superscriptValue);
        CellUtil.setFont(cell, workbook, newFont);
    } else if (attribute == STRIKETHROUGH) {
        boolean strikeThrough = true;
        if (newFont.getStrikeout()) {
            strikeThrough = false;
        }
        newFont.setStrikeout(strikeThrough);
        CellUtil.setFont(cell, workbook, newFont);
    } else if (attribute == POSTURE) {
        boolean italic = true;
        if (newFont.getItalic()) {
            italic = false;
        }
        newFont.setItalic(italic);
        CellUtil.setFont(cell, workbook, newFont);
    } else if (attribute == SIZE) {
        newFont.setFontHeightInPoints(((Number) attributeValue).shortValue());
        CellUtil.setFont(cell, workbook, newFont);
    } else if (attribute == JUSTIFICATION) {
        CellUtil.setAlignment(cell, workbook, CellStyle.ALIGN_JUSTIFY);
    } else if (attribute == ALIGNMENT) {
        if (attributeValue.equals(StyleConstants.ALIGN_LEFT)) {
            CellUtil.setAlignment(cell, workbook, CellStyle.ALIGN_LEFT);
        } else if (attributeValue.equals(StyleConstants.ALIGN_RIGHT)) {
            CellUtil.setAlignment(cell, workbook, CellStyle.ALIGN_RIGHT);
        } else if (attributeValue.equals(StyleConstants.ALIGN_CENTER)) {
            CellUtil.setAlignment(cell, workbook, CellStyle.ALIGN_CENTER);
        }
    } else if (attribute == INDENTATION) {
        style.setIndention(((Number) attributeValue).shortValue());
    } else if (attribute == TEXT_TRANSFORM) {
        String text = CellUtils.getFormattedText(cell);
        String transformedText = ((TextTransformer) attributeValue).transformText(text);
        cell.setCellValue(transformedText);
    }
}

From source file:org.joeffice.spreadsheet.TableStyleable.java

License:Apache License

private Font copyFont(Cell cell) {
    CellStyle style = cell.getCellStyle();
    Workbook workbook = cell.getSheet().getWorkbook();
    short fontIndex = style.getFontIndex();
    Font xlsFont = cell.getSheet().getWorkbook().getFontAt(fontIndex);
    Font newFont = workbook.createFont();
    newFont.setFontName(xlsFont.getFontName());
    newFont.setFontHeight((short) xlsFont.getFontHeight());
    newFont.setBoldweight(xlsFont.getBoldweight());
    newFont.setItalic(xlsFont.getItalic());
    newFont.setUnderline(xlsFont.getUnderline());
    newFont.setColor(xlsFont.getColor());
    return newFont;
}

From source file:org.lisapark.octopus.util.json.ExcelSardineUtils.java

License:Open Source License

public static void main(String[] args) {

    Map<String, Integer> prodCellIndexMap = Maps.newHashMap();
    prodCellIndexMap.put(SHOP, 0);//from   w ww .  j a va2  s .  c  o  m
    prodCellIndexMap.put(SHIFT, 0);
    prodCellIndexMap.put(MACHINE, 0);
    prodCellIndexMap.put(PRODUCT, 0);
    prodCellIndexMap.put(PRODUCT_TYPE, 0);
    prodCellIndexMap.put(MATERIAL_TYPE, 0);
    prodCellIndexMap.put(RAW_MATERIAL, 4);
    prodCellIndexMap.put(TOTAL_MATERIALS, 5);
    prodCellIndexMap.put(TOTAL_PRODUCTS, 6);

    Map<String, Integer> wrhCellIndexMap = Maps.newHashMap();
    wrhCellIndexMap.put(WAREHOUSE, 1);
    wrhCellIndexMap.put(ITEM, 1);
    wrhCellIndexMap.put(ITEM_TYPE, 1);
    wrhCellIndexMap.put(BEGINING, 0);
    wrhCellIndexMap.put(INCOMING, 1);
    wrhCellIndexMap.put(OUTGOING, 2);
    wrhCellIndexMap.put(ENDING, 3);

    try {
        String excelFile = "http://173.72.110.131:8080/WebDavServer/iPlast/Warehouse/";

        // Get all xml files
        Sardine sardine = SardineFactory.begin("", "");
        List<DavResource> resources = sardine.getResources(excelFile);

        for (DavResource res : resources) {
            String url = res.getPath();
            //getAbsoluteUrl();
            if (res.isDirectory()) {
                continue;
            } else {
                Map<String, String> props = res.getCustomProps();
                if (props.get(PROCESSED) == null) {
                    InputStream isData = sardine.get(url);
                    HSSFWorkbook book = new HSSFWorkbook(isData);

                    int index = 0;
                    int increament = 1;
                    if (book.getNumberOfSheets() > index) {
                        if (increament == 0) {
                            //                                increament = PROD_OUTLINE_INCREAMENT;
                            increament = WRH_OUTLINE_INCREAMENT;
                        }
                        Sheet sheet = book.getSheetAt(index);
                        if (sheet == null) {
                            continue;
                        }

                        // Iterate through the rows.
                        int splitRowNumber = 0;

                        if (sheet.getPaneInformation() != null && sheet.getPaneInformation().isFreezePane()) {
                            splitRowNumber = sheet.getPaneInformation().getHorizontalSplitPosition();
                        }

                        Map<String, Object> rowMap = Maps.newHashMap();

                        int start = 2;
                        Row dateRow = sheet.getRow(8);
                        int end = dateRow.getLastCellNum();

                        for (int dateShift = start; dateShift < end - 4; dateShift = dateShift + 4) {

                            rowMap.put(DATE, formatDate(dateRow.getCell(dateShift).getStringCellValue()));
                            System.out.println(dateRow.getCell(dateShift).getStringCellValue());

                            Sheet _sheet = book.getSheetAt(index);

                            for (Iterator<Row> rowsIt = _sheet.rowIterator(); rowsIt.hasNext();) {
                                Row row = rowsIt.next();
                                if (row.getPhysicalNumberOfCells() <= 0 || row.getRowNum() < splitRowNumber) {
                                    continue;
                                }

                                Cell cell = row.getCell(1);
                                int indent = cell.getCellStyle().getIndention();
                                int absIndent = indent / increament;
                                //                                
                                if (processRowWrhSs(rowMap, row, wrhCellIndexMap, absIndent, dateShift)) {
                                    System.out.println(rowMap);
                                }
                            }
                        }
                    }
                    props.put(PROCESSED, TRUE);
                    sardine.setCustomProps(url, props, null);
                } else {
                    System.out.println("Property PROCESSED: " + props.get(PROCESSED));
                    List<String> removeProps = new ArrayList<String>(1);
                    removeProps.add(PROCESSED);

                    sardine.setCustomProps(url, null, removeProps);
                }
                break;
            }
        }
    } catch (FileNotFoundException ex) {
        Exceptions.printStackTrace(ex);
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}

From source file:org.lisapark.octopus.util.json.JsonUtils.java

License:Open Source License

/**
 * //  ww w.  j a  v  a  2 s  . com
 * @param jsonCells
 * @param row
 * @param stack
 * @param increment
 * @return
 * @throws JSONException 
 */
private JSONObject applyIndentation(JSONArray jsonCells, Row row, List<String> stack, int increment)
        throws JSONException {
    JSONObject jsonRow = new JSONObject();

    Cell cell = row.getCell(row.getFirstCellNum());
    String nodeName = cell.getStringCellValue() + "";
    int indent = cell.getCellStyle().getIndention();
    int absIndent = indent / increment;

    if (indent == 0) {
        jsonRow = jsonRow.put(nodeName + "", jsonCells);
    } else if (absIndent > (stack.size() - 1)) {
        jsonRow = buildNestedJsonObject(jsonRow, nodeName, jsonCells, stack);
    } else {
        // remove items from the top of the stack
        for (int i = stack.size(); i > absIndent; i--) {
            stack.remove(i - 1);
        }
        jsonRow = buildNestedJsonObject(jsonRow, nodeName, jsonCells, stack);
    }

    return jsonRow;
}

From source file:org.lisapark.octopus.util.json.JsonUtils.java

License:Open Source License

/**
 * //from  w w w . ja  v a2s.  co m
 * @param jsonCells
 * @param row
 * @param stack
 * @param increment
 * @return 
 */
private String jsonApplyIndentationAsString(String stringCells, Row row, List<String> stack, int increment) {
    StringBuilder stringBuilder = new StringBuilder();

    Cell cell = row.getCell(row.getFirstCellNum());
    String nodeName = cell.getStringCellValue();

    if (nodeName.isEmpty()) {
        nodeName = DEFAULT_NODE_NAME;
    }

    int indent = cell.getCellStyle().getIndention();
    int absIndent = indent / increment;

    if (absIndent > 0 && absIndent <= (stack.size() - 1)) {
        // remove items from the top of the stack
        for (int i = stack.size() - 1; i >= absIndent; --i) {
            stack.remove(i);
        }
        stack.add(nodeName);
        stringBuilder = buildNestedJsonString(stringCells, stack);
    } else if (absIndent > (stack.size() - 1)) {
        stack.add(nodeName);
        stringBuilder = buildNestedJsonString(stringCells, stack);
    } else if (absIndent == 0) {
        stringBuilder = stringBuilder.append("{" + "\"").append(nodeName.replace('.', '_')).append("\"" + " : ")
                .append(stringCells).append("}");
        stack.removeAll(stack);
        stack.add(nodeName);
    }

    return stringBuilder.toString();
}