Example usage for org.apache.poi.ss.usermodel Workbook getFontAt

List of usage examples for org.apache.poi.ss.usermodel Workbook getFontAt

Introduction

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

Prototype

Font getFontAt(int idx);

Source Link

Document

Get the font at the given index number

Usage

From source file:itpreneurs.itp.report.archive.CellStyleDetails.java

License:Apache License

public static void main(String[] args) throws Exception {

    //     if(args.length == 0) {
    //        throw new IllegalArgumentException("Filename must be given");
    //     }/*from   w w w.j  a va 2 s .  c o  m*/

    String filename = "/Users/vincentgong/Documents/workspaces/Resource/itpreneurs/report/Workbook1.xlsx";

    Workbook wb = WorkbookFactory.create(new File(filename));
    DataFormatter formatter = new DataFormatter();

    for (int sn = 0; sn < wb.getNumberOfSheets(); sn++) {
        Sheet sheet = wb.getSheetAt(sn);
        System.out.println("Sheet #" + sn + " : " + sheet.getSheetName());

        for (Row row : sheet) {
            System.out.println("  Row " + row.getRowNum());

            for (Cell cell : row) {
                CellReference ref = new CellReference(cell);
                System.out.print("    " + ref.formatAsString());
                System.out.print(" (" + cell.getColumnIndex() + ") ");

                CellStyle style = cell.getCellStyle();
                System.out.print("Format=" + style.getDataFormatString() + " ");
                System.out.print("FG=" + renderColor(style.getFillForegroundColorColor()) + " ");
                System.out.print("BG=" + renderColor(style.getFillBackgroundColorColor()) + " ");

                Font font = wb.getFontAt(style.getFontIndex());
                System.out.print("Font=" + font.getFontName() + " ");
                System.out.print("FontColor=");
                if (font instanceof HSSFFont) {
                    System.out.print(renderColor(((HSSFFont) font).getHSSFColor((HSSFWorkbook) wb)));
                }
                if (font instanceof XSSFFont) {
                    System.out.print(renderColor(((XSSFFont) font).getXSSFColor()));
                }

                System.out.println();
                System.out.println("        " + formatter.formatCellValue(cell));
            }
        }

        System.out.println();
    }
}

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;//  www  .ja  v  a 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:net.ceos.project.poi.annotated.core.CellStyleHandler.java

License:Apache License

/**
 * Clone a cell style passed as parameter.
 * //from  w w w .  ja  v a  2s .c o m
 * @param wb
 *            the {@link Workbook} in use
 * @param csBase
 *            the {@link CellStyle} base
 * @return the new cell style
 */
private static CellStyle cloneCellStyle(final Workbook wb, final CellStyle csBase) {
    CellStyle cs = cellStyleFactory.newInstance(wb);
    cs.setAlignment(csBase.getAlignment());
    cs.setVerticalAlignment(csBase.getVerticalAlignment());
    cs.setBorderTop(csBase.getBorderTop());
    cs.setBorderBottom(csBase.getBorderBottom());
    cs.setBorderLeft(csBase.getBorderLeft());
    cs.setBorderRight(csBase.getBorderRight());
    cs.setFillForegroundColor(csBase.getFillForegroundColor());
    cs.setFillPattern(csBase.getFillPattern());
    cs.setWrapText(csBase.getWrapText());

    cs.setFont(wb.getFontAt(csBase.getFontIndex()));
    return cs;
}

From source file:org.apache.metamodel.excel.ExcelUtils.java

License:Apache License

public static Style getCellStyle(Workbook workbook, Cell cell) {
    if (cell == null) {
        return Style.NO_STYLE;
    }/*w  w  w  . j  av a2s .  c  o m*/
    final CellStyle cellStyle = cell.getCellStyle();

    final short fontIndex = cellStyle.getFontIndex();
    final Font font = workbook.getFontAt(fontIndex);
    final StyleBuilder styleBuilder = new StyleBuilder();

    // Font bold, italic, underline
    if (font.getBoldweight() >= Font.BOLDWEIGHT_BOLD) {
        styleBuilder.bold();
    }
    if (font.getItalic()) {
        styleBuilder.italic();
    }
    if (font.getUnderline() != FontUnderline.NONE.getByteValue()) {
        styleBuilder.underline();
    }

    // Font size
    final Font stdFont = workbook.getFontAt((short) 0);
    final short fontSize = font.getFontHeightInPoints();
    if (stdFont.getFontHeightInPoints() != fontSize) {
        styleBuilder.fontSize(fontSize, SizeUnit.PT);
    }

    // Font color
    final short colorIndex = font.getColor();
    if (font instanceof HSSFFont) {
        if (colorIndex != HSSFFont.COLOR_NORMAL) {
            final HSSFWorkbook wb = (HSSFWorkbook) workbook;
            HSSFColor color = wb.getCustomPalette().getColor(colorIndex);
            if (color != null) {
                short[] triplet = color.getTriplet();
                styleBuilder.foreground(triplet);
            }
        }
    } else if (font instanceof XSSFFont) {
        XSSFFont xssfFont = (XSSFFont) font;

        XSSFColor color = xssfFont.getXSSFColor();
        if (color != null) {
            String argbHex = color.getARGBHex();
            if (argbHex != null) {
                styleBuilder.foreground(argbHex.substring(2));
            }
        }
    } else {
        throw new IllegalStateException(
                "Unexpected font type: " + (font == null ? "null" : font.getClass()) + ")");
    }

    // Background color
    if (cellStyle.getFillPattern() == 1) {
        Color color = cellStyle.getFillForegroundColorColor();
        if (color instanceof HSSFColor) {
            short[] triplet = ((HSSFColor) color).getTriplet();
            if (triplet != null) {
                styleBuilder.background(triplet);
            }
        } else if (color instanceof XSSFColor) {
            String argb = ((XSSFColor) color).getARGBHex();
            if (argb != null) {
                styleBuilder.background(argb.substring(2));
            }
        } else {
            throw new IllegalStateException(
                    "Unexpected color type: " + (color == null ? "null" : color.getClass()) + ")");
        }
    }

    // alignment
    switch (cellStyle.getAlignment()) {
    case CellStyle.ALIGN_LEFT:
        styleBuilder.leftAligned();
        break;
    case CellStyle.ALIGN_RIGHT:
        styleBuilder.rightAligned();
        break;
    case CellStyle.ALIGN_CENTER:
        styleBuilder.centerAligned();
        break;
    case CellStyle.ALIGN_JUSTIFY:
        styleBuilder.justifyAligned();
        break;
    }

    return styleBuilder.create();
}

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  a  va 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.netxilia.impexp.impl.PoiUtils.java

License:Open Source License

public static CellStyle netxiliaStyle2Poi(Styles nxStyle, Workbook workbook, CellStyle poiStyle) {
    if (nxStyle == null) {
        return poiStyle;
    }//w ww .ja va 2 s.  com
    poiStyle.setWrapText(nxStyle.contains(DefaultStyle.nowrap.getStyle()));

    // font
    short bold = nxStyle.contains(DefaultStyle.bold.getStyle()) ? Font.BOLDWEIGHT_BOLD : Font.BOLDWEIGHT_NORMAL;
    byte underline = nxStyle.contains(DefaultStyle.underline.getStyle()) ? Font.U_SINGLE : Font.U_NONE;
    boolean italic = nxStyle.contains(DefaultStyle.italic.getStyle());
    boolean strikeout = nxStyle.contains(DefaultStyle.strikeout.getStyle());
    Font defaultFont = workbook.getFontAt(poiStyle.getFontIndex());
    Font font = workbook.findFont(bold, defaultFont.getColor(), defaultFont.getFontHeight(),
            defaultFont.getFontName(), italic, strikeout, defaultFont.getTypeOffset(), underline);
    if (font == null) {
        font = workbook.createFont();
        font.setBoldweight(bold);
        font.setItalic(italic);
        font.setUnderline(underline);
        font.setStrikeout(strikeout);
    }
    poiStyle.setFont(font);

    // borders
    if (nxStyle.contains(DefaultStyle.borderLeft.getStyle())) {
        poiStyle.setBorderLeft(CellStyle.BORDER_THIN);
    }
    if (nxStyle.contains(DefaultStyle.borderRight.getStyle())) {
        poiStyle.setBorderRight(CellStyle.BORDER_THIN);
    }
    if (nxStyle.contains(DefaultStyle.borderTop.getStyle())) {
        poiStyle.setBorderTop(CellStyle.BORDER_THIN);
    }
    if (nxStyle.contains(DefaultStyle.borderBottom.getStyle())) {
        poiStyle.setBorderBottom(CellStyle.BORDER_THIN);
    }

    // align
    if (nxStyle.contains(DefaultStyle.alignLeft.getStyle())) {
        poiStyle.setAlignment(CellStyle.ALIGN_LEFT);
    } else if (nxStyle.contains(DefaultStyle.alignRight.getStyle())) {
        poiStyle.setAlignment(CellStyle.ALIGN_RIGHT);
    } else if (nxStyle.contains(DefaultStyle.alignCenter.getStyle())) {
        poiStyle.setAlignment(CellStyle.ALIGN_CENTER);
    } else if (nxStyle.contains(DefaultStyle.alignJustify.getStyle())) {
        poiStyle.setAlignment(CellStyle.ALIGN_JUSTIFY);
    }

    return poiStyle;
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.table.xls.helper.ExcelFontFactory.java

License:Open Source License

/**
 * Constructor for ExcelFontFactory./*  w w w. j  a  v a 2s  .c om*/
 *
 * @param workbook
 *          the workbook.
 */
public ExcelFontFactory(final Workbook workbook, final ExcelColorProducer colorProducer) {
    if (workbook == null) {
        throw new NullPointerException();
    }
    if (colorProducer == null) {
        throw new NullPointerException();
    }

    this.fonts = new HashMap<HSSFFontWrapper, Font>();
    this.workbook = workbook;

    // read the fonts from the workbook ...
    // Funny one: Please note that the layout will be broken if the first
    // font is not 'Arial 10'.
    final short numberOfFonts = this.workbook.getNumberOfFonts();
    for (int i = 0; i < numberOfFonts; i++) {
        final Font font = workbook.getFontAt((short) i);
        this.fonts.put(new HSSFFontWrapper(font), font);
    }

    // add the default font
    // this MUST be the first one, that is created.
    // oh, I hate Excel ...
    final HSSFFontWrapper wrapper = new HSSFFontWrapper("Arial", (short) 10, false, false, false, false,
            colorProducer.getNearestColor(Color.black));
    getExcelFont(wrapper);
}

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

License:MIT License

/**
 * Gets the row style./*from   www.  jav  a 2 s  .  c  o  m*/
 *
 * @param wb
 *            the wb
 * @param poiCell
 *            the poi cell
 * @param inputType
 *            the input type
 * @param rowHeight
 *            the row height
 * @param rowspan
 *            the rowspan
 * @return the row style
 */
public static String getRowStyle(final Workbook wb, final Cell poiCell, final String inputType,
        final float rowHeight, final int rowspan) {

    CellStyle cellStyle = poiCell.getCellStyle();
    if ((cellStyle != null) && (rowspan == 1)) {
        short fontIdx = cellStyle.getFontIndex();
        Font font = wb.getFontAt(fontIdx);
        float maxHeight = rowHeight;
        if (!inputType.isEmpty()) {
            maxHeight = Math.min(font.getFontHeightInPoints() + 8f, rowHeight);
        }
        return "height:" + WebSheetUtility.pointsToPixels(maxHeight) + "px;";
    }
    return "";
}

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

License:MIT License

/**
 * Gets the cell font style./*from ww  w  . jav a  2  s .  c  om*/
 *
 * @param wb
 *            the wb
 * @param poiCell
 *            the poi cell
 * @return the cell font style
 */
public static String getCellFontStyle(final Workbook wb, final Cell poiCell) {

    CellStyle cellStyle = poiCell.getCellStyle();
    StringBuilder webStyle = new StringBuilder();
    if (cellStyle != null) {
        short fontIdx = cellStyle.getFontIndex();
        Font font = wb.getFontAt(fontIdx);
        if (font.getItalic()) {
            webStyle.append("font-style: italic;");
        }
        if (font.getBold()) {
            webStyle.append("font-weight: bold;");
        }
        webStyle.append("font-size: " + font.getFontHeightInPoints() + "pt;");
        String decoration = getCellFontDecoration(font);
        if (decoration.length() > 0) {
            webStyle.append("text-decoration:" + decoration + ";");
        }
        webStyle.append(getCellFontColor(font));

    }
    return webStyle.toString();

}