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

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

Introduction

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

Prototype

short getFontHeightInPoints();

Source Link

Document

Get the font height in points.

Usage

From source file:cn.edu.zucc.chenxg.preview.ToHtml.java

License:Apache License

private void fontStyle(CellStyle style) {
    Font font = wb.getFontAt(style.getFontIndex());

    if (font.getBoldweight() >= HSSFFont.BOLDWEIGHT_BOLD)
        out.format("  font-weight: bold;%n");
    if (font.getItalic())
        out.format("  font-style: italic;%n");

    int fontheight = font.getFontHeightInPoints();
    if (fontheight == 9) {
        //fix for stupid ol Windows
        fontheight = 10;//from   ww w.  j  av  a  2 s .c o  m
    }
    out.format("  font-size: %dpt;%n", fontheight);

    // Font color is handled with the other colors
}

From source file:com.actelion.research.spiritapp.report.AbstractReport.java

License:Open Source License

public void populateReport(Study study) throws Exception {
    assert study != null;

    this.study = study;

    initWorkbook();/*from   ww  w .j a  v  a 2 s .  com*/

    //Create the workbook
    populateWorkBook();

    //Post processing
    //Add Table borders (between different styles of cells)
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);

        for (int r = 4; r <= sheet.getLastRowNum(); r++) {
            Row row = sheet.getRow(r);
            if (row == null)
                continue;

            Row rowUp = sheet.getRow(r - 1);
            Row rowDown = sheet.getRow(r + 1);
            for (int c = 0; c <= row.getLastCellNum(); c++) {
                Cell cell = row.getCell(c);
                Cell cellLeft = c == 0 ? null : row.getCell(c - 1);
                boolean borderLeftAbove = cellLeft != null && cellLeft.getCellStyle().getBorderTop() == 1;
                boolean borderLeftUnder = cellLeft != null && cellLeft.getCellStyle().getBorderBottom() == 1;

                if ((cell != null
                        && cell.getCellStyle().getBorderLeft() + cell.getCellStyle().getBorderRight() > 0)
                        || (cell == null && c + 1 <= row.getLastCellNum() && row.getCell(c + 1) != null)) {
                    if (borderLeftAbove)
                        drawLineAbove(sheet, r, c, c, (short) 1);
                    if (borderLeftUnder)
                        drawLineUnder(sheet, r, c, c, (short) 1);
                }

                if (cell != null) {
                    Font font = wb.getFontAt(cell.getCellStyle().getFontIndex());
                    if (font.getFontHeightInPoints() >= 12)
                        continue;

                    Cell cellUp = rowUp != null && c < rowUp.getLastCellNum() ? rowUp.getCell(c) : null;
                    Cell cellDown = rowDown != null && c < rowDown.getLastCellNum() ? rowDown.getCell(c) : null;

                    if (cellUp == null /*|| (cell.getCellType()!=0 && cellUp.getCellType()!=0 && cellUp.getCellType()!=cell.getCellType())*/ ) {
                        //Border above
                        drawLineAbove(sheet, r, c, c, (short) 1);
                    }
                    if (cellDown == null /*|| (cell.getCellType()!=0 && cellDown.getCellType()!=0 && cellDown.getCellType()!=cell.getCellType())*/) {
                        //Border under
                        drawLineUnder(sheet, r, c, c, (short) 1);
                    }
                }
            }
        }
    }

}

From source file:com.actelion.research.spiritapp.ui.util.PDFUtils.java

License:Open Source License

private static Chunk getChunk(Workbook wb, Cell cell) {
    Chunk phrase = null;/*from  w w  w . j a va 2  s  . c o  m*/

    switch (cell.getCellType() == Cell.CELL_TYPE_FORMULA ? cell.getCachedFormulaResultType()
            : cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        phrase = new Chunk("" + cell.getStringCellValue());
        break;
    case Cell.CELL_TYPE_NUMERIC:
        String format = cell.getCellStyle().getDataFormatString();
        if (cell.getCellStyle().getDataFormat() > 0) {
            try {
                if (format.contains("0")) {
                    //Decimal
                    DecimalFormat df = new DecimalFormat(format);
                    phrase = new Chunk(df.format(cell.getNumericCellValue()));
                } else if (format.contains("h:")) {
                    phrase = new Chunk(FormatterUtils.formatDateTimeShort(cell.getDateCellValue()));
                } else if (format.contains("yy")) {
                    phrase = new Chunk(FormatterUtils.formatDate(cell.getDateCellValue()));
                }
            } catch (Exception e) {
                System.err.println(e);
            }
        }
        if (phrase == null) {
            phrase = new Chunk("" + (int) cell.getNumericCellValue());
        }
        break;
    case Cell.CELL_TYPE_BLANK:
        phrase = new Chunk("");
        break;
    default:
        phrase = new Chunk("" + cell.getCellType());
    }
    Font font = wb.getFontAt(cell.getCellStyle().getFontIndex());
    short[] rgb = HSSFColor.getIndexHash().get((int) font.getColor()).getTriplet();

    phrase.setFont(new com.lowagie.text.Font(phrase.getFont().getBaseFont(), font.getFontHeightInPoints() - 3,
            (font.getBold() ? com.lowagie.text.Font.BOLD : com.lowagie.text.Font.NORMAL),
            new Color(rgb[0], rgb[1], rgb[2])));
    return phrase;
}

From source file:com.actelion.research.spiritapp.ui.util.POIUtils.java

License:Open Source License

public static void autoSizeColumns(Sheet sheet, int maxColWidth, boolean resizeHeight) {
    ListHashMap<Integer, Integer> col2lens = new ListHashMap<>();
    for (int row = sheet.getFirstRowNum(); row <= sheet.getLastRowNum(); row++) {
        Row r = sheet.getRow(row);/*from   w w  w  .j a v a2 s.c  o m*/
        if (r == null || r.getFirstCellNum() < 0)
            continue;
        short maxH = 0;

        for (int col = r.getFirstCellNum(); col <= r.getLastCellNum(); col++) {
            Cell c = r.getCell(col);
            if (c == null
                    || (c.getCellType() != Cell.CELL_TYPE_STRING && c.getCellType() != Cell.CELL_TYPE_NUMERIC))
                continue;

            Font font = sheet.getWorkbook().getFontAt(c.getCellStyle().getFontIndex());
            String s = c.getCellType() == Cell.CELL_TYPE_STRING ? c.getStringCellValue()
                    : "" + c.getNumericCellValue();
            String[] lines = MiscUtils.split(s, "\n");
            int maxLen = 1;
            for (int i = 0; i < lines.length; i++) {
                maxLen = Math.max(lines[i].length(), maxLen);
            }
            if (font.getFontHeightInPoints() < 12) {
                col2lens.add(col, 700
                        + maxLen * (font.getFontHeightInPoints() + (font.getBoldweight() > 500 ? 1 : 0)) * 20);
            }
            maxH = (short) Math.max(maxH, 50 + lines.length * (font.getFontHeight() * 1.2));
        }
        if (resizeHeight)
            r.setHeight(maxH);
    }

    for (int col : col2lens.keySet()) {
        List<Integer> lens = col2lens.get(col);
        Collections.sort(lens);
        int len = lens.get(lens.size() - 1);
        if (lens.size() > 10 && lens.get(lens.size() - 1) > 2 * lens.get(lens.size() - 2)) {
            len = lens.get(lens.size() - 2);
        }
        sheet.setColumnWidth(col,
                Math.max(Math.min((int) (len * 1.25), maxColWidth > 0 ? maxColWidth : 300000), 1500));
    }
}

From source file:com.common.report.util.html.ToHtml.java

License:Apache License

private void fontStyle(CellStyle style) {
    Font font = wb.getFontAt(style.getFontIndex());

    if (font.getBoldweight() >= HSSFFont.BOLDWEIGHT_NORMAL)
        out.format("  font-weight: bold;%n");
    if (font.getItalic())
        out.format("  font-style: italic;%n");

    int fontheight = font.getFontHeightInPoints();
    if (fontheight == 9) {
        //fix for stupid ol Windows
        fontheight = 10;//from  w w  w. j  av a  2  s.c  o m
    }
    out.format("  font-size: %dpt;%n", fontheight);

    // Font color is handled with the other colors
}

From source file:com.dua3.meja.model.poi.PoiWorkbook.java

License:Apache License

PoiFont getPoiFont(com.dua3.meja.model.Font font, Style style) {
    Map<String, String> properties = style.properties();

    if (properties.isEmpty() && font instanceof PoiFont && ((PoiFont) font).workbook == this) {
        return (PoiFont) font;
    }/*from w ww.j  ava 2  s.  co m*/

    // FIXME JDK 8 
    // String name = properties.getOrDefault(Style.FONT_FAMILY, font.getFamily());
    String name = properties.get(Style.FONT_FAMILY);
    if (name == null) {
        name = font.getFamily();
    }

    String sSize = properties.get(Style.FONT_SIZE);
    short height = (short) Math
            .round(sSize == null ? font.getSizeInPoints() : MejaHelper.decodeFontSize(sSize));

    final String sStyle = properties.get(Style.FONT_STYLE);
    boolean italic = sStyle == null ? font.isItalic() : "italic".equals(sStyle);

    final String sWeight = properties.get(Style.FONT_WEIGHT);
    boolean bold = sWeight == null ? font.isBold() : "bold".equals(sWeight);

    String sDecoration = properties.get(Style.TEXT_DECORATION);
    boolean underline = sDecoration == null ? font.isUnderlined() : "underline".equals(sDecoration);
    boolean strikethrough = sDecoration == null ? font.isStrikeThrough() : "line-through".equals(sDecoration);

    String sColor = properties.get(Style.COLOR);
    Color color = sColor == null ? font.getColor() : Color.valueOf(sColor);

    // try to find existing font
    for (short i = 0; i < poiWorkbook.getNumberOfFonts(); i++) {
        Font poiFont = poiWorkbook.getFontAt(i);

        if (poiFont.getFontName().equalsIgnoreCase(name) && poiFont.getFontHeightInPoints() == height
                && poiFont.getBold() == bold && poiFont.getItalic() == italic
                && (poiFont.getUnderline() != Font.U_NONE) == underline
                && poiFont.getStrikeout() == strikethrough && getColor(poiFont, Color.BLACK).equals(color)
                && poiFont.getTypeOffset() == Font.SS_NONE) {
            return new PoiFont(this, poiFont);
        }
    }

    // if not found, create it
    return createFont(name, height, font.getColor(), bold, italic, underline, strikethrough);
}

From source file:com.googlecode.testcase.annotation.handle.toexcel.strategy.ToHtmlWithExcel.java

License:Apache License

private void fontStyle(CellStyle style) {
    Font font = wb.getFontAt(style.getFontIndex());

    if (font.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL)
        out.format("  font-weight: bold;%n");
    if (font.getItalic())
        out.format("  font-style: italic;%n");

    int fontheight = font.getFontHeightInPoints();
    if (fontheight == 9) {
        //fix for stupid ol Windows
        fontheight = 10;//from  www.j a v a  2s . c  o  m
    }
    out.format("  font-size: %dpt;%n", fontheight);

    // Font color is handled with the other colors
}

From source file:com.hurry.excel.html.Excel2Html.java

License:Apache License

private void fontStyle(CellStyle style) {
    Font font = wb.getFontAt(style.getFontIndex());

    if (font.getBoldweight() >= HSSFFont.BOLDWEIGHT_NORMAL)
        out.format("  font-weight: bold;%n");
    if (font.getItalic())
        out.format("  font-style: italic;%n");

    int fontheight = font.getFontHeightInPoints();
    if (fontheight == 9) {
        // fix for stupid ol Windows
        fontheight = 10;//w  w w  . j  av a  2  s  . c  o m
    }
    out.format("  font-size: %dpt;%n", fontheight);

    // Font color is handled with the other colors
}

From source file:com.vaadin.addon.spreadsheet.SpreadsheetStyleFactory.java

License:Open Source License

private void fontStyle(StringBuilder sb, CellStyle cellStyle) {
    try {/*from   www  . jav  a 2s .c  o  m*/
        Font font = spreadsheet.getWorkbook().getFontAt(cellStyle.getFontIndex());
        if (font.getIndex() == defaultFont.getIndex()) {
            // uses default font, no need to add styles
            return;
        }
        String fontFamily = styleFontFamily(font);
        if (!fontFamily.equals(defaultFontFamily)) {
            sb.append(fontFamily);
        }
        if (font.getBoldweight() != Font.BOLDWEIGHT_NORMAL) {
            sb.append("font-weight:");
            sb.append(font.getBoldweight());
            sb.append(";");
        }
        if (font.getItalic()) {
            sb.append("font-style:italic;");
        }
        final int fontheight = font.getFontHeightInPoints();
        if (fontheight != defaultFontHeightInPoints) {
            sb.append("font-size:");
            sb.append(fontheight);
            sb.append("pt;");
        }
        if (font.getUnderline() != Font.U_NONE) {
            sb.append("text-decoration:underline;");
        } else if (font.getStrikeout()) {
            sb.append("text-decoration:overline;");
        }
    } catch (IndexOutOfBoundsException ioobe) {
        // somehow workbook doesn't have all the fonts the cells have???
        LOGGER.log(Level.WARNING, "Font missing, " + cellStyle.getFontIndex() + " / " + cellStyle.getClass()
                + ", " + ioobe.getMessage(), ioobe);
    }
}

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;
    String family = null;/*from  ww  w.  jav a2 s  . com*/
    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;
}