Example usage for org.apache.poi.ss.usermodel Font getCharSet

List of usage examples for org.apache.poi.ss.usermodel Font getCharSet

Introduction

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

Prototype

int getCharSet();

Source Link

Document

get character-set to use.

Usage

From source file:jdbreport.model.io.xls.poi.Excel2003Writer.java

License:Apache License

private Font getFont(short fontIndex, AttributeSet attributeSet, Workbook wb) {
    Font font = null;//from w  w  w.ja  va  2  s  .  co  m
    String family = null;
    String sizeStr = null;
    short color = 0;
    boolean bold = false;
    boolean italic = false;
    boolean underline = false;
    boolean line_through = false;
    boolean sub = false;
    boolean sup = false;
    Enumeration<?> en = attributeSet.getAttributeNames();
    while (en.hasMoreElements()) {
        Object key = en.nextElement();
        String name = key.toString();
        String attribute = attributeSet.getAttribute(key).toString();

        switch (name) {
        case "font-weight":
            bold = attribute.equals("bold");
            break;
        case "font-style":
            italic = attribute.equals("italic");
            break;
        case "text-decoration":
            if (attribute.equals("underline")) {
                underline = true;
            } else if (attribute.equals("line-through")) {
                line_through = true;
            }
            break;
        case "font-family":
            family = attribute;
            break;
        case "font-size":
            sizeStr = attribute;

            break;
        case "color":
            Color fontColor = Utils.colorByName(attribute);
            if (fontColor == null) {
                try {
                    fontColor = Utils.stringToColor(attribute);
                } catch (Exception ignored) {

                }
            }
            if (fontColor != null) {
                color = colorToIndex(wb, fontColor);
            }
            break;
        case "vertical-align":
            if (attribute.equals("sub")) {
                sub = true;
            } else if (attribute.equals("sup")) {
                sup = true;
            }
            break;
        }
    }
    if (family != null || bold || italic || underline || line_through || color > 0 || sizeStr != null || sub
            || sup) {

        font = wb.createFont();
        if (fontIndex > 0) {
            Font parentFont = wb.getFontAt(fontIndex);
            if (parentFont != null) {
                font.setBold(parentFont.getBold());
                font.setColor(parentFont.getColor());
                try {
                    font.setCharSet(parentFont.getCharSet());
                } catch (Throwable ignored) {
                }
                font.setFontHeight(parentFont.getFontHeight());
                font.setFontName(parentFont.getFontName());
                font.setItalic(parentFont.getItalic());
                font.setStrikeout(parentFont.getStrikeout());
                font.setUnderline(parentFont.getUnderline());
                font.setTypeOffset(parentFont.getTypeOffset());
            }
        }
        if (family != null) {
            font.setFontName(family);
        }
        if (bold) {
            font.setBold(true);
        }
        if (italic) {
            font.setItalic(italic);
        }
        if (underline) {
            font.setUnderline(Font.U_SINGLE);
        }
        if (line_through) {
            font.setStrikeout(line_through);
        }
        if (color > 0) {
            font.setColor(color);
        }
        if (sizeStr != null) {
            short size = (short) Float.parseFloat(sizeStr);
            if (sizeStr.charAt(0) == '+' || sizeStr.charAt(0) == '-') {
                size = (short) (Content.pointToSize(font.getFontHeightInPoints()) + size);
            }
            font.setFontHeightInPoints(Content.sizeToPoints(size));
        }
        if (sup) {
            font.setTypeOffset(Font.SS_SUPER);
        } else if (sub) {
            font.setTypeOffset(Font.SS_SUB);
        }
    }
    return font;
}

From source file:org.pentaho.di.trans.steps.excelwriter.ExcelWriterStep.java

License:Apache License

void writeField(Object v, ValueMetaInterface vMeta, ExcelWriterStepField excelField, Row xlsRow, int posX,
        Object[] row, int fieldNr, boolean isTitle) throws KettleException {
    try {/*from ww w . j av  a2  s.c o  m*/
        boolean cellExisted = true;
        // get the cell
        Cell cell = xlsRow.getCell(posX);
        if (cell == null) {
            cellExisted = false;
            cell = xlsRow.createCell(posX);
        }

        // if cell existed and existing cell's styles should not be changed, don't
        if (!(cellExisted && meta.isLeaveExistingStylesUnchanged())) {

            // if the style of this field is cached, reuse it
            if (!isTitle && data.getCachedStyle(fieldNr) != null) {
                cell.setCellStyle(data.getCachedStyle(fieldNr));
            } else {
                // apply style if requested
                if (excelField != null) {

                    // determine correct cell for title or data rows
                    String styleRef = null;
                    if (!isTitle && !Utils.isEmpty(excelField.getStyleCell())) {
                        styleRef = excelField.getStyleCell();
                    } else if (isTitle && !Utils.isEmpty(excelField.getTitleStyleCell())) {
                        styleRef = excelField.getTitleStyleCell();
                    }

                    if (styleRef != null) {
                        Cell styleCell = getCellFromReference(styleRef);
                        if (styleCell != null && cell != styleCell) {
                            cell.setCellStyle(styleCell.getCellStyle());
                        }
                    }
                }

                // set cell format as specified, specific format overrides cell specification
                if (!isTitle && excelField != null && !Utils.isEmpty(excelField.getFormat())
                        && !excelField.getFormat().startsWith("Image")) {
                    setDataFormat(excelField.getFormat(), cell);
                }
                // cache it for later runs
                if (!isTitle) {
                    data.cacheStyle(fieldNr, cell.getCellStyle());
                }
            }
        }

        // create link on cell if requested
        if (!isTitle && excelField != null && data.linkfieldnrs[fieldNr] >= 0) {
            String link = data.inputRowMeta.getValueMeta(data.linkfieldnrs[fieldNr])
                    .getString(row[data.linkfieldnrs[fieldNr]]);
            if (!Utils.isEmpty(link)) {
                CreationHelper ch = data.wb.getCreationHelper();
                // set the link on the cell depending on link type
                Hyperlink hyperLink = null;
                if (link.startsWith("http:") || link.startsWith("https:") || link.startsWith("ftp:")) {
                    hyperLink = ch.createHyperlink(HyperlinkType.URL);
                    hyperLink.setLabel("URL Link");
                } else if (link.startsWith("mailto:")) {
                    hyperLink = ch.createHyperlink(HyperlinkType.EMAIL);
                    hyperLink.setLabel("Email Link");
                } else if (link.startsWith("'")) {
                    hyperLink = ch.createHyperlink(HyperlinkType.DOCUMENT);
                    hyperLink.setLabel("Link within this document");
                } else {
                    hyperLink = ch.createHyperlink(HyperlinkType.FILE);
                    hyperLink.setLabel("Link to a file");
                }

                hyperLink.setAddress(link);
                cell.setHyperlink(hyperLink);

                // if cell existed and existing cell's styles should not be changed, don't
                if (!(cellExisted && meta.isLeaveExistingStylesUnchanged())) {

                    if (data.getCachedLinkStyle(fieldNr) != null) {
                        cell.setCellStyle(data.getCachedLinkStyle(fieldNr));
                    } else {
                        // CellStyle style = cell.getCellStyle();
                        Font origFont = data.wb.getFontAt(cell.getCellStyle().getFontIndex());
                        Font hlink_font = data.wb.createFont();
                        // reporduce original font characteristics

                        hlink_font.setBold(origFont.getBold());
                        hlink_font.setCharSet(origFont.getCharSet());
                        hlink_font.setFontHeight(origFont.getFontHeight());
                        hlink_font.setFontName(origFont.getFontName());
                        hlink_font.setItalic(origFont.getItalic());
                        hlink_font.setStrikeout(origFont.getStrikeout());
                        hlink_font.setTypeOffset(origFont.getTypeOffset());
                        // make it blue and underlined
                        hlink_font.setUnderline(Font.U_SINGLE);
                        hlink_font.setColor(IndexedColors.BLUE.getIndex());
                        CellStyle style = cell.getCellStyle();
                        style.setFont(hlink_font);
                        cell.setCellStyle(style);
                        data.cacheLinkStyle(fieldNr, cell.getCellStyle());
                    }
                }
            }
        }

        // create comment on cell if requrested
        if (!isTitle && excelField != null && data.commentfieldnrs[fieldNr] >= 0
                && data.wb instanceof XSSFWorkbook) {
            String comment = data.inputRowMeta.getValueMeta(data.commentfieldnrs[fieldNr])
                    .getString(row[data.commentfieldnrs[fieldNr]]);
            if (!Utils.isEmpty(comment)) {
                String author = data.commentauthorfieldnrs[fieldNr] >= 0
                        ? data.inputRowMeta.getValueMeta(data.commentauthorfieldnrs[fieldNr]).getString(
                                row[data.commentauthorfieldnrs[fieldNr]])
                        : "Kettle PDI";
                cell.setCellComment(createCellComment(author, comment));
            }
        }
        // cell is getting a formula value or static content
        if (!isTitle && excelField != null && excelField.isFormula()) {
            // formula case
            cell.setCellFormula(vMeta.getString(v));
        } else {
            // static content case
            switch (vMeta.getType()) {
            case ValueMetaInterface.TYPE_DATE:
                if (v != null && vMeta.getDate(v) != null) {
                    cell.setCellValue(vMeta.getDate(v));
                }
                break;
            case ValueMetaInterface.TYPE_BOOLEAN:
                if (v != null) {
                    cell.setCellValue(vMeta.getBoolean(v));
                }
                break;
            case ValueMetaInterface.TYPE_STRING:
            case ValueMetaInterface.TYPE_BINARY:
                if (v != null) {
                    cell.setCellValue(vMeta.getString(v));
                }
                break;
            case ValueMetaInterface.TYPE_BIGNUMBER:
            case ValueMetaInterface.TYPE_NUMBER:
            case ValueMetaInterface.TYPE_INTEGER:
                if (v != null) {
                    cell.setCellValue(vMeta.getNumber(v));
                }
                break;
            default:
                break;
            }
        }
    } catch (Exception e) {
        logError("Error writing field (" + data.posX + "," + data.posY + ") : " + e.toString());
        logError(Const.getStackTracker(e));
        throw new KettleException(e);
    }
}

From source file:uk.ac.liverpool.spreadsheet.ExcelFeatureAnalysis.java

License:Apache License

private static void analyseSpreadsheet(Element da, ExcelFeatureAnalysis efa) {

    Element s = new Element("spreadsheets", sn);
    da.addContent(s);/*from   w  w w .  j ava  2  s . co  m*/
    s.setAttribute("numberOfSheets", "" + efa.wb.getNumberOfSheets());
    // workbook wide features

    List<? extends PictureData> allPictures = efa.wb.getAllPictures();
    if (allPictures != null && allPictures.size() > 0) {
        Element oo = new Element("Pictures", sn);
        s.addContent(oo);
        for (PictureData pd : allPictures) {
            Element ob = new Element("Picture", sn);
            ob.setAttribute("mimeType", pd.getMimeType());
            oo.addContent(ob);
        }
    }

    int numfonts = efa.wb.getNumberOfFonts();
    if (numfonts > 0) {
        Element oo = new Element("Fonts", sn);
        s.addContent(oo);
        for (int i = 0; i < numfonts; i++) {
            Font cs = efa.wb.getFontAt((short) i);
            Element ob = new Element("Font", sn);
            ob.setAttribute("Name", cs.getFontName());

            ob.setAttribute("Charset", "" + cs.getCharSet());
            oo.addContent(ob);
        }
    }

    if (efa.hswb != null) {

        DocumentSummaryInformation dsi = efa.hswb.getDocumentSummaryInformation();
        if (dsi != null)
            s.setAttribute("OSVersion", "" + dsi.getOSVersion());
        // Property[] properties = dsi.getProperties();
        // CustomProperties customProperties = dsi.getCustomProperties();

        List<HSSFObjectData> eo = efa.hswb.getAllEmbeddedObjects();
        if (eo != null && eo.size() > 0) {
            Element oo = new Element("EmbeddedObjects", sn);
            s.addContent(oo);
            for (HSSFObjectData o : eo) {
                Element ob = new Element("EmbeddedObject", sn);
                ob.setAttribute("name", o.getOLE2ClassName());
                oo.addContent(ob);
            }

        }
    } else if (efa.xswb != null) {
        try {
            POIXMLProperties properties = efa.xswb.getProperties();
            List<PackagePart> allEmbedds = efa.xswb.getAllEmbedds();
            if (allEmbedds != null && allEmbedds.size() > 0) {
                Element oo = new Element("EmbeddedObjects", sn);
                s.addContent(oo);

                for (PackagePart p : allEmbedds) {
                    Element ob = new Element("EmbeddedObject", sn);
                    ob.setAttribute("mimeType", p.getContentType());
                    ob.setAttribute("name", p.getPartName().getName());

                    oo.addContent(ob);
                }
            }
        } catch (OpenXML4JException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    int nn = efa.wb.getNumberOfNames();
    if (nn > 0) {
        Element oo = new Element("NamedCells", sn);
        s.addContent(oo);
    }

    // sheet specific features
    int total = efa.wb.getNumberOfSheets();
    for (int c = 0; c < total; c++) {
        Sheet sheet = efa.wb.getSheetAt(c);
        Element single = new Element("sheet", sn);
        s.addContent(single);
        analyseSheet(sheet, single, sn, efa);
    }
}