List of usage examples for org.apache.poi.ss.usermodel Font getColor
short getColor();
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 ww w . java 2s.co 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.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. j a va 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.ocs.dynamo.ui.composite.table.export.TableExportActionHandlerTest.java
License:Apache License
@Test public void testExportWithCustomCellStyle() throws IOException { List<EntityModel<?>> models = new ArrayList<>(); models.add(entityModelFactory.getModel(Person.class)); handler = new TableExportActionHandler(ui, entityModelFactory, models, messageService, REPORT_TITLE, columnIds, true, new CustomCellStyleGenerator() { private CellStyle cellStyle; private CellStyle bdStyle; @Override//from www .j a v a 2 s .com public CellStyle getCustomCellStyle(Workbook workbook, Object propId, Object value, AttributeModel attributeModel) { if (cellStyle == null) { cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(CellStyle.ALIGN_RIGHT); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBorderTop(CellStyle.BORDER_THIN); cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setBorderRight(CellStyle.BORDER_THIN); Font font = workbook.createFont(); font.setColor(IndexedColors.BLUE.getIndex()); cellStyle.setFont(font); } if (bdStyle == null) { DataFormat format = workbook.createDataFormat(); bdStyle = workbook.createCellStyle(); bdStyle.setAlignment(CellStyle.ALIGN_RIGHT); bdStyle.setBorderBottom(CellStyle.BORDER_THIN); bdStyle.setBorderTop(CellStyle.BORDER_THIN); bdStyle.setBorderLeft(CellStyle.BORDER_THIN); bdStyle.setBorderRight(CellStyle.BORDER_THIN); bdStyle.setDataFormat(format.getFormat("#,##0.00")); Font font = workbook.createFont(); font.setColor(IndexedColors.BLUE.getIndex()); bdStyle.setFont(font); } if ("name".equals(propId)) { return cellStyle; } else if ("percentage".equals(propId)) { return bdStyle; } return null; } }); handler.handleAction(handler.getActions(null, null)[0], getTable(), null); byte[] bytes = captureSave(); Workbook wb = importer.createWorkbook(bytes); Assert.assertEquals("Bas, Bob", wb.getSheetAt(0).getRow(2).getCell(0).getStringCellValue()); Font font = wb.getFontAt(wb.getSheetAt(0).getRow(2).getCell(0).getCellStyle().getFontIndex()); Assert.assertEquals(IndexedColors.BLUE.getIndex(), font.getColor()); Assert.assertEquals("Patrick", wb.getSheetAt(0).getRow(3).getCell(0).getStringCellValue()); Assert.assertEquals(35, wb.getSheetAt(0).getRow(2).getCell(1).getNumericCellValue(), 0.001); Assert.assertEquals(44, wb.getSheetAt(0).getRow(3).getCell(1).getNumericCellValue(), 0.001); // totals must be summed up Assert.assertEquals(79, wb.getSheetAt(0).getRow(4).getCell(1).getNumericCellValue(), 0.001); // percentage Assert.assertEquals(0.12, wb.getSheetAt(0).getRow(2).getCell(3).getNumericCellValue(), 0.001); Assert.assertEquals(0.15, wb.getSheetAt(0).getRow(3).getCell(3).getNumericCellValue(), 0.001); }
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 ww w . j a v a2 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.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 ww w .ja va 2 s .co 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 www .j a v a 2 s . c om*/ 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 {/* ww w. ja v a2s .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.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.netxilia.impexp.impl.PoiUtils.java
License:Open Source License
public static Styles poiStyle2Netxilia(CellStyle poiStyle, Font font, HSSFPalette palette, NetxiliaStyleResolver styleResolver) { List<Style> entries = new ArrayList<Style>(); if (!poiStyle.getWrapText()) { entries.add(DefaultStyle.nowrap.getStyle()); }/*from w ww.ja v a2s . co m*/ // font if (font.getItalic()) { entries.add(DefaultStyle.italic.getStyle()); } if (font.getStrikeout()) { entries.add(DefaultStyle.strikeout.getStyle()); } if (font.getBoldweight() == Font.BOLDWEIGHT_BOLD) { entries.add(DefaultStyle.bold.getStyle()); } if (font.getUnderline() != Font.U_NONE) { entries.add(DefaultStyle.underline.getStyle()); } // borders if (poiStyle.getBorderBottom() != CellStyle.BORDER_NONE) { entries.add(DefaultStyle.borderBottom.getStyle()); } if (poiStyle.getBorderLeft() != CellStyle.BORDER_NONE) { entries.add(DefaultStyle.borderLeft.getStyle()); } if (poiStyle.getBorderTop() != CellStyle.BORDER_NONE) { entries.add(DefaultStyle.borderTop.getStyle()); } if (poiStyle.getBorderRight() != CellStyle.BORDER_NONE) { entries.add(DefaultStyle.borderRight.getStyle()); } // align switch (poiStyle.getAlignment()) { case CellStyle.ALIGN_LEFT: entries.add(DefaultStyle.alignLeft.getStyle()); break; case CellStyle.ALIGN_RIGHT: entries.add(DefaultStyle.alignRight.getStyle()); break; case CellStyle.ALIGN_CENTER: entries.add(DefaultStyle.alignCenter.getStyle()); break; case CellStyle.ALIGN_JUSTIFY: entries.add(DefaultStyle.alignJustify.getStyle()); break; } if (font != null && font.getColor() != 0) { HSSFColor poiForeground = palette.getColor(font.getColor()); if (poiForeground != null && poiForeground != HSSFColor.AUTOMATIC.getInstance()) { Style foregroundDef = styleResolver.approximateForeground(poiForeground.getTriplet()[0], poiForeground.getTriplet()[1], poiForeground.getTriplet()[2]); if (foregroundDef != null) { entries.add(foregroundDef); } } } if (poiStyle.getFillForegroundColor() != 0) { HSSFColor poiBackground = palette.getColor(poiStyle.getFillForegroundColor()); if (poiBackground != null && poiBackground != HSSFColor.AUTOMATIC.getInstance()) { Style backgroundDef = styleResolver.approximateBackground(poiBackground.getTriplet()[0], poiBackground.getTriplet()[1], poiBackground.getTriplet()[2]); if (backgroundDef != null) { entries.add(backgroundDef); } } } return entries.size() > 0 ? Styles.styles(entries) : null; }
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; }//from w w w . j a v a 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; }