List of usage examples for org.apache.poi.ss.usermodel Font getItalic
boolean getItalic();
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 ava2s .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.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 {/* www . ja v a 2 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/*from ww w . jav a 2 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; }
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 www .j av a2 s . c o 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.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 w ww. j ava2s . 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:org.pentaho.reporting.engine.classic.core.modules.output.table.xls.helper.HSSFFontWrapper.java
License:Open Source License
/** * Creates a HSSFFontWrapper for the excel font. * * @param font//from ww w . j a v a 2 s . c o m * the font. */ public HSSFFontWrapper(final Font font) { if (font == null) { throw new NullPointerException("Font is null"); } if (font.getColor() < 0) { throw new IllegalArgumentException("Negative color index is not allowed"); } fontName = normalizeFontName(font.getFontName()); fontHeight = font.getFontHeightInPoints(); bold = font.getBold(); italic = font.getItalic(); underline = (font.getUnderline() != HSSFFont.U_NONE); strikethrough = font.getStrikeout(); colorIndex = font.getColor(); }
From source file:org.tiefaces.components.websheet.utility.CellStyleUtility.java
License:MIT License
/** * Gets the cell font style.//from w w w . j a v a2 s .co m * * @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(); }
From source file:ru.icc.cells.ssdc.DataLoader.java
License:Apache License
private void fillFont(CFont font, Font excelFont) { font.setName(excelFont.getFontName()); // TODO CFont font //font.setColor( excelFont.getColor() ); font.setHeight(excelFont.getFontHeight()); font.setHeightInPoints(excelFont.getFontHeightInPoints()); // TODO ? ? Boldweight, ?? ? ? short boldWeight = excelFont.getBoldweight(); if (boldWeight >= 700) font.setBold(true);//w ww .j a v a2 s .c om font.setItalic(excelFont.getItalic()); font.setStrikeout(excelFont.getStrikeout()); byte underline = excelFont.getUnderline(); if (underline != Font.U_NONE) font.setUnderline(true); if (underline == Font.U_DOUBLE || underline == Font.U_DOUBLE_ACCOUNTING) font.setDoubleUnderline(true); }
From source file:ru.spb.nicetu.tableviewer.server.XlsToHtml.java
License:Apache License
private void fontStyle(CellStyle style, Formatter out, boolean isBuiltIn) { Font font = wb.getFontAt(style.getFontIndex()); if (font.getBoldweight() >= HSSFFont.BOLDWEIGHT_BOLD) styleOut("font-weight", "bold", out, isBuiltIn); if (font.getItalic()) styleOut("font-style", "italic", out, isBuiltIn); if (font.getFontName() != null && !"".equals(font.getFontName())) styleOut("font-family", font.getFontName(), out, isBuiltIn); int fontheight = font.getFontHeightInPoints(); if (fontheight == 9) { //fix for stupid ol Windows fontheight = 10;/*from w w w .j a v a 2 s. co m*/ } styleOut("font-size", "" + fontheight + "pt", out, isBuiltIn); // Font color is handled with the other colors }