List of usage examples for org.apache.poi.ss.usermodel Font getUnderline
byte getUnderline();
From source file:com.canoo.webtest.plugins.exceltest.ExcelVerifyCellStyle.java
License:Open Source License
private static String getFontStyle(final Font font) { final StringBuffer sb = new StringBuffer(); if (font.getBoldweight() == Font.BOLDWEIGHT_BOLD) { sb.append("bold "); }// ww w. j a va 2s. c o m if (font.getItalic()) { sb.append("italic "); } if (font.getStrikeout()) { sb.append("strikethrough "); } if (font.getTypeOffset() == Font.SS_SUB) { sb.append("subscript "); } else if (font.getTypeOffset() == Font.SS_SUPER) { sb.append("superscript "); } switch (font.getUnderline()) { case Font.U_NONE: break; case Font.U_SINGLE: sb.append("underline "); break; case Font.U_SINGLE_ACCOUNTING: sb.append("underline-accounting "); break; case Font.U_DOUBLE: sb.append("underline-double "); break; case Font.U_DOUBLE_ACCOUNTING: sb.append("underline-double-accounting "); break; default: sb.append("underline-unknown "); break; } if (sb.length() == 0) { return "normal"; } return sb.substring(0, sb.length() - 1); }
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 w w . jav a 2 s .c om*/ // 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.vaadin.addon.spreadsheet.SpreadsheetStyleFactory.java
License:Open Source License
private void fontStyle(StringBuilder sb, CellStyle cellStyle) { try {/*from w w w.j a v 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:guru.qas.martini.report.DefaultState.java
License:Apache License
protected void colorCompromisedThemes() { Collection<Cell> failed = statii.get("FAILED"); if (!failed.isEmpty()) { List<Row> rows = Lists.newArrayListWithExpectedSize(failed.size()); for (Cell cell : failed) { Row row = cell.getRow();/*from ww w.j av a 2 s. c o m*/ rows.add(row); } Set<Cell> compromisedThemeCells = Sets.newHashSet(); Map<String, Collection<Cell>> themeMap = themes.asMap(); for (Map.Entry<String, Collection<Cell>> mapEntry : themeMap.entrySet()) { Collection<Cell> themeCells = mapEntry.getValue(); boolean compromised = false; for (Iterator<Cell> iterator = themeCells.iterator(); !compromised && iterator.hasNext();) { Cell themeCell = iterator.next(); Row row = themeCell.getRow(); compromised = rows.contains(row); } if (compromised) { compromisedThemeCells.addAll(themeCells); } } Set<String> compromisedThemes = Sets.newHashSet(); for (Cell themeCell : compromisedThemeCells) { String contents = themeCell.getStringCellValue(); if (null != contents) { Iterable<String> themes = Splitter.onPattern("\\s+").omitEmptyStrings().split(contents); Iterables.addAll(compromisedThemes, themes); } } for (String theme : compromisedThemes) { Collection<Cell> cells = themes.get(theme); for (Cell cell : cells) { CellStyle cellStyle = cell.getCellStyle(); Sheet sheet = cell.getSheet(); Workbook workbook = sheet.getWorkbook(); int originalFontIndex = cellStyle.getFontIndexAsInt(); Font originalFont = workbook.getFontAt(originalFontIndex); CellStyle clone = workbook.createCellStyle(); clone.cloneStyleFrom(cellStyle); Font font = workbook.findFont(true, IndexedColors.DARK_RED.getIndex(), originalFont.getFontHeight(), originalFont.getFontName(), originalFont.getItalic(), originalFont.getStrikeout(), originalFont.getTypeOffset(), originalFont.getUnderline()); if (null == font) { font = workbook.createFont(); font.setBold(true); font.setColor(IndexedColors.DARK_RED.getIndex()); font.setFontHeight(originalFont.getFontHeight()); font.setFontName(originalFont.getFontName()); font.setItalic(originalFont.getItalic()); font.setStrikeout(originalFont.getStrikeout()); font.setTypeOffset(originalFont.getTypeOffset()); font.setUnderline(originalFont.getUnderline()); } clone.setFont(font); cell.setCellStyle(clone); } } } }
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 v a2s. 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.ConditionalFormattingHandler.java
License:Apache License
/** * Apply the conditional formatting according the values defined at the * respective annotation. Is only available at the declared object, in * another words, at the linked {@link Sheet} with the object. * //from w ww .j av a2s . c o m * @param configCriteria * the {@link XConfigCriteria} * @param conditionalFomat * the {@link XlsConditionalFormat} * @throws ConfigurationException */ protected static void applyCondition(XConfigCriteria configCriteria, XlsConditionalFormat conditionalFomat) throws ConfigurationException { // Define a Conditional Formatting rule, which triggers formatting // according the developer definition and applies patternFormatting SheetConditionalFormatting sheet = configCriteria.getSheet().getSheetConditionalFormatting(); /* apply all rules defined */ int i = 0; ConditionalFormattingRule[] rules = new ConditionalFormattingRule[conditionalFomat.rules().length]; XlsConditionalFormatRules[] rulesAnnotated = conditionalFomat.rules(); for (XlsConditionalFormatRules rule : rulesAnnotated) { ConditionalFormattingRule setRule = sheet.createConditionalFormattingRule(rule.operator(), rule.formula1(), StringUtils.isNotBlank(rule.formula2()) ? rule.formula2() : null); CellStyle decorator = null; try { decorator = configCriteria.getCellStyle(conditionalFomat.decorator()); } catch (ElementException e) { throw new ConfigurationException(ExceptionMessage.CONFIGURATION_DECORATOR_MISSING.getMessage(), e); } /* add FontFormatting */ FontFormatting fontFormat = setRule.createFontFormatting(); Font f = configCriteria.getWorkbook().getFontAt(decorator.getFontIndex()); fontFormat.setFontStyle(f.getItalic(), f.getBold()); fontFormat.setFontColorIndex(f.getColor()); fontFormat.setUnderlineType(f.getUnderline()); /* add BorderFormatting */ BorderFormatting borderFormat = setRule.createBorderFormatting(); borderFormat.setBorderBottom(decorator.getBorderBottom()); borderFormat.setBorderTop(decorator.getBorderTop()); borderFormat.setBorderLeft(decorator.getBorderLeft()); borderFormat.setBorderRight(decorator.getBorderRight()); borderFormat.setBottomBorderColor(decorator.getBottomBorderColor()); borderFormat.setTopBorderColor(decorator.getTopBorderColor()); borderFormat.setLeftBorderColor(decorator.getLeftBorderColor()); borderFormat.setRightBorderColor(decorator.getRightBorderColor()); /* add PatternFormatting */ PatternFormatting patternFormat = setRule.createPatternFormatting(); patternFormat.setFillBackgroundColor(decorator.getFillForegroundColor()); /* join rule */ rules[i++] = setRule; } /* Define a region */ CellRangeAddress[] regions = { CellRangeAddress.valueOf(CellFormulaConverter .calculateRangeAddressFromTemplate(configCriteria, conditionalFomat.rangeAddress())) }; /* Apply Conditional Formatting rule defined above to the regions */ sheet.addConditionalFormatting(regions, rules); }
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; }//from w w w . j a v a2 s.com 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.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 ww. ja v a2 s. c o m*/ 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.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 ava2 s . c o m */ 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; }