List of usage examples for org.apache.poi.ss.usermodel Font setItalic
void setItalic(boolean italic);
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 av a2s . 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 CellStyle netxiliaStyle2Poi(Styles nxStyle, Workbook workbook, CellStyle poiStyle) { if (nxStyle == null) { return poiStyle; }// www. j a va 2 s . c o m 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.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 ww w . j a v a 2s . c om 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.ExcelFontFactory.java
License:Open Source License
/** * Returns the excel font stored in this wrapper. * * @param wrapper//from www .j a v a 2 s .c om * the font wrapper that holds all font information from the repagination. * @return the created font. */ private Font createFont(final HSSFFontWrapper wrapper) { final Font font = workbook.createFont(); font.setBold(wrapper.isBold()); font.setColor(wrapper.getColorIndex()); font.setFontName(wrapper.getFontName()); font.setFontHeightInPoints((short) wrapper.getFontHeight()); font.setItalic(wrapper.isItalic()); font.setStrikeout(wrapper.isStrikethrough()); if (wrapper.isUnderline()) { font.setUnderline(Font.U_SINGLE); } else { font.setUnderline(Font.U_NONE); } return font; }
From source file:org.primefaces.component.export.ExcelExporter.java
License:Open Source License
protected void applyFacetOptions(Workbook wb, ExporterOptions options, CellStyle facetStyle) { Font facetFont = wb.createFont(); if (options != null) { String facetFontStyle = options.getFacetFontStyle(); if (facetFontStyle != null) { if (facetFontStyle.equalsIgnoreCase("BOLD")) { facetFont.setBoldweight(Font.BOLDWEIGHT_BOLD); }//from w w w . j a va 2s. co m if (facetFontStyle.equalsIgnoreCase("ITALIC")) { facetFont.setItalic(true); } } HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette(); Color color = null; String facetBackground = options.getFacetBgColor(); if (facetBackground != null) { color = Color.decode(facetBackground); HSSFColor backgroundColor = palette.findSimilarColor(color.getRed(), color.getGreen(), color.getBlue()); ((HSSFCellStyle) facetStyle).setFillForegroundColor(backgroundColor.getIndex()); facetStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); } String facetFontColor = options.getFacetFontColor(); if (facetFontColor != null) { color = Color.decode(facetFontColor); HSSFColor facetColor = palette.findSimilarColor(color.getRed(), color.getGreen(), color.getBlue()); ((HSSFFont) facetFont).setColor(facetColor.getIndex()); } String facetFontSize = options.getFacetFontSize(); if (facetFontSize != null) { facetFont.setFontHeightInPoints(Short.valueOf(facetFontSize)); } } facetStyle.setFont(facetFont); }
From source file:org.primefaces.component.export.ExcelExporter.java
License:Open Source License
protected void applyCellOptions(Workbook wb, ExporterOptions options, CellStyle cellStyle) { Font cellFont = wb.createFont(); if (options != null) { String cellFontColor = options.getCellFontColor(); if (cellFontColor != null) { HSSFPalette palette = ((HSSFWorkbook) wb).getCustomPalette(); Color color = Color.decode(cellFontColor); HSSFColor cellColor = palette.findSimilarColor(color.getRed(), color.getGreen(), color.getBlue()); ((HSSFFont) cellFont).setColor(cellColor.getIndex()); }/*from w w w .ja v a 2 s . c o m*/ String cellFontSize = options.getCellFontSize(); if (cellFontSize != null) { cellFont.setFontHeightInPoints(Short.valueOf(cellFontSize)); } String cellFontStyle = options.getCellFontStyle(); if (cellFontStyle != null) { if (cellFontStyle.equalsIgnoreCase("BOLD")) { cellFont.setBoldweight(Font.BOLDWEIGHT_BOLD); } if (cellFontStyle.equalsIgnoreCase("ITALIC")) { cellFont.setItalic(true); } } } cellStyle.setFont(cellFont); }
From source file:org.primefaces.component.export.ExcelXExporter.java
License:Open Source License
@Override protected void applyFacetOptions(Workbook wb, ExporterOptions options, CellStyle facetStyle) { Font facetFont = wb.createFont(); facetFont.setFontName("Arial"); if (options != null) { String facetFontStyle = options.getFacetFontStyle(); if (facetFontStyle != null) { if (facetFontStyle.equalsIgnoreCase("BOLD")) { facetFont.setBoldweight(Font.BOLDWEIGHT_BOLD); }//from ww w .j a v a 2s . c o m if (facetFontStyle.equalsIgnoreCase("ITALIC")) { facetFont.setItalic(true); } } String facetBackground = options.getFacetBgColor(); if (facetBackground != null) { XSSFColor backgroundColor = new XSSFColor(Color.decode(facetBackground)); ((XSSFCellStyle) facetStyle).setFillForegroundColor(backgroundColor); facetStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); } String facetFontColor = options.getFacetFontColor(); if (facetFontColor != null) { XSSFColor facetColor = new XSSFColor(Color.decode(facetFontColor)); ((XSSFFont) facetFont).setColor(facetColor); } String facetFontSize = options.getFacetFontSize(); if (facetFontSize != null) { facetFont.setFontHeightInPoints(Short.valueOf(facetFontSize)); } } facetStyle.setFont(facetFont); }
From source file:org.primefaces.component.export.ExcelXExporter.java
License:Open Source License
@Override protected void applyCellOptions(Workbook wb, ExporterOptions options, CellStyle cellStyle) { Font cellFont = wb.createFont(); cellFont.setFontName("Arial"); if (options != null) { String cellFontColor = options.getCellFontColor(); if (cellFontColor != null) { XSSFColor cellColor = new XSSFColor(Color.decode(cellFontColor)); ((XSSFFont) cellFont).setColor(cellColor); }//from www . j ava2s . co m String cellFontSize = options.getCellFontSize(); if (cellFontSize != null) { cellFont.setFontHeightInPoints(Short.valueOf(cellFontSize)); } String cellFontStyle = options.getCellFontStyle(); if (cellFontStyle != null) { if (cellFontStyle.equalsIgnoreCase("BOLD")) { cellFont.setBoldweight(Font.BOLDWEIGHT_BOLD); } if (cellFontStyle.equalsIgnoreCase("ITALIC")) { cellFont.setItalic(true); } } } cellStyle.setFont(cellFont); }
From source file:org.primefaces.extensions.component.exporter.ExcelExporter.java
License:Apache License
protected void createCustomFonts() { Font facetFont = wb.createFont(); Font cellFont = wb.createFont(); if (cellFontColor != null) { XSSFColor cellColor = new XSSFColor(cellFontColor); ((XSSFFont) cellFont).setColor(cellColor); }//from w w w .j a v a 2 s .c om if (cellFontSize != null) { cellFont.setFontHeightInPoints((short) cellFontSize); } if (cellFontStyle.equalsIgnoreCase("BOLD")) { cellFont.setBoldweight(Font.BOLDWEIGHT_BOLD); } if (cellFontStyle.equalsIgnoreCase("ITALIC")) { cellFont.setItalic(true); } if (facetFontStyle.equalsIgnoreCase("BOLD")) { facetFont.setBoldweight(Font.BOLDWEIGHT_BOLD); } if (facetFontStyle.equalsIgnoreCase("ITALIC")) { facetFont.setItalic(true); } if (fontName != null) { cellFont.setFontName(fontName); facetFont.setFontName(fontName); } if (facetBackground != null) { XSSFColor backgroundColor = new XSSFColor(facetBackground); ((XSSFCellStyle) facetStyle).setFillForegroundColor(backgroundColor); ((XSSFCellStyle) facetStyleLeftAlign).setFillForegroundColor(backgroundColor); ((XSSFCellStyle) facetStyleCenterAlign).setFillForegroundColor(backgroundColor); ((XSSFCellStyle) facetStyleRightAlign).setFillForegroundColor(backgroundColor); facetStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); facetStyleLeftAlign.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); facetStyleCenterAlign.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); facetStyleRightAlign.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); } if (facetFontColor != null) { XSSFColor facetColor = new XSSFColor(facetFontColor); ((XSSFFont) facetFont).setColor(facetColor); } if (facetFontSize != null) { facetFont.setFontHeightInPoints((short) facetFontSize); } cellStyle.setFont(cellFont); cellStyleLeftAlign.setFont(cellFont); cellStyleCenterAlign.setFont(cellFont); cellStyleRightAlign.setFont(cellFont); facetStyle.setFont(facetFont); facetStyleLeftAlign.setFont(facetFont); facetStyleCenterAlign.setFont(facetFont); facetStyleRightAlign.setFont(facetFont); //facetStyle.setAlignment(CellStyle.ALIGN_CENTER); }