List of usage examples for org.apache.poi.ss.usermodel Font setFontName
void setFontName(String name);
From source file:net.sourceforge.jaulp.export.excel.poi.ExportExcelUtils.java
License:Apache License
/** * Creates a new font from the given parameters. * * @param workbook/*from w ww .ja va2s . com*/ * the workbook * @param fontName * the font name * @param boldweight * the boldweight * @param height * the height * @return the font */ public static Font newFont(Workbook workbook, String fontName, short boldweight, short height) { Font font = workbook.createFont(); font.setFontName(fontName); font.setBoldweight(boldweight); font.setFontHeightInPoints(height); return font; }
From source file:offishell.excel.Excel.java
License:MIT License
/** * <p>//from w ww.ja v a 2 s .com * Create {@link Excel} wrapper. * </p> * * @param path * @param book */ private Excel(Path path, XSSFWorkbook book) { this.path = path; this.book = book; this.excel = Locator.file(path); this.sheet = book.getSheetAt(0); this.baseStyle = book.createCellStyle(); this.dateStyle = book.createCellStyle(); CreationHelper helper = book.getCreationHelper(); DataFormat dateFormat = helper.createDataFormat(); Font font = book.createFont(); font.setFontName(" Medium"); font.setFontHeightInPoints((short) 10); baseStyle.setFont(font); baseStyle.setAlignment(HorizontalAlignment.CENTER); baseStyle.setVerticalAlignment(VerticalAlignment.CENTER); baseStyle.setShrinkToFit(true); baseStyle.setWrapText(true); dateStyle.cloneStyleFrom(baseStyle); dateStyle.setDataFormat(dateFormat.getFormat("yyyy/mm/dd")); }
From source file:org.alanwilliamson.openbd.plugin.spreadsheet.SpreadSheetFormatOptions.java
License:Open Source License
public static Font createCommentFont(Workbook workbook, cfStructData _struct) throws Exception { Font font = workbook.createFont(); if (_struct.containsKey("bold")) { if (_struct.getData("bold").getBoolean()) font.setBoldweight(Font.BOLDWEIGHT_BOLD); else//from w w w .j av a 2 s . c om font.setBoldweight(Font.BOLDWEIGHT_NORMAL); } if (_struct.containsKey("color")) { String v = _struct.getData("color").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'color' (" + v + ")"); } else font.setColor(s); } if (_struct.containsKey("font")) { font.setFontName(_struct.getData("font").getString()); } if (_struct.containsKey("italic")) { font.setItalic(_struct.getData("italic").getBoolean()); } if (_struct.containsKey("strikeout")) { font.setStrikeout(_struct.getData("strikeout").getBoolean()); } if (_struct.containsKey("underline")) { font.setUnderline((byte) _struct.getData("underline").getInt()); } if (_struct.containsKey("size")) { font.setFontHeightInPoints((short) _struct.getData("size").getInt()); } return font; }
From source file:org.alanwilliamson.openbd.plugin.spreadsheet.SpreadSheetFormatOptions.java
License:Open Source License
public static CellStyle createCellStyle(Workbook workbook, cfStructData _struct) throws Exception { CellStyle style = workbook.createCellStyle(); if (_struct.containsKey("alignment")) { String v = _struct.getData("alignment").getString(); Short s = lookup_alignment.get(v); if (s == null) { throw new Exception("invalid parameter for 'alignment' (" + v + ")"); } else//w ww. ja v a 2 s . c o m style.setAlignment(s); } if (_struct.containsKey("bottomborder")) { String v = _struct.getData("bottomborder").getString(); Short s = lookup_border.get(v); if (s == null) { throw new Exception("invalid parameter for 'bottomborder' (" + v + ")"); } else style.setBorderBottom(s); } if (_struct.containsKey("topborder")) { String v = _struct.getData("topborder").getString(); Short s = lookup_border.get(v); if (s == null) { throw new Exception("invalid parameter for 'topborder' (" + v + ")"); } else style.setBorderTop(s); } if (_struct.containsKey("leftborder")) { String v = _struct.getData("leftborder").getString(); Short s = lookup_border.get(v); if (s == null) { throw new Exception("invalid parameter for 'leftborder' (" + v + ")"); } else style.setBorderLeft(s); } if (_struct.containsKey("rightborder")) { String v = _struct.getData("rightborder").getString(); Short s = lookup_border.get(v); if (s == null) { throw new Exception("invalid parameter for 'rightborder' (" + v + ")"); } else style.setBorderRight(s); } if (_struct.containsKey("bottombordercolor")) { String v = _struct.getData("bottombordercolor").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'bottombordercolor' (" + v + ")"); } else style.setBottomBorderColor(s); } if (_struct.containsKey("topbordercolor")) { String v = _struct.getData("topbordercolor").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'topbordercolor' (" + v + ")"); } else style.setTopBorderColor(s); } if (_struct.containsKey("leftbordercolor")) { String v = _struct.getData("leftbordercolor").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'leftbordercolor' (" + v + ")"); } else style.setLeftBorderColor(s); } if (_struct.containsKey("rightbordercolor")) { String v = _struct.getData("rightbordercolor").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'rightbordercolor' (" + v + ")"); } else style.setRightBorderColor(s); } if (_struct.containsKey("fillpattern")) { String v = _struct.getData("fillpattern").getString(); Short s = lookup_fillpatten.get(v); if (s == null) { throw new Exception("invalid parameter for 'fillpattern' (" + v + ")"); } else style.setFillPattern(s); } if (_struct.containsKey("fgcolor")) { String v = _struct.getData("fgcolor").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'fgcolor' (" + v + ")"); } else style.setFillForegroundColor(s); } if (_struct.containsKey("bgcolor")) { String v = _struct.getData("bgcolor").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'bgcolor' (" + v + ")"); } else style.setFillBackgroundColor(s); } if (_struct.containsKey("textwrap")) { Boolean b = _struct.getData("textwrap").getBoolean(); style.setWrapText(b); } if (_struct.containsKey("hidden")) { Boolean b = _struct.getData("hidden").getBoolean(); style.setHidden(b); } if (_struct.containsKey("locked")) { Boolean b = _struct.getData("locked").getBoolean(); style.setLocked(b); } if (_struct.containsKey("indent")) { style.setIndention((short) _struct.getData("indent").getInt()); } if (_struct.containsKey("rotation")) { style.setRotation((short) _struct.getData("rotation").getInt()); } if (_struct.containsKey("dateformat")) { style.setDataFormat(workbook.createDataFormat().getFormat(_struct.getData("dateformat").getString())); } // Manage the fonts Font f = workbook.createFont(); if (_struct.containsKey("strikeout")) { f.setStrikeout(true); } if (_struct.containsKey("bold")) { Boolean b = _struct.getData("bold").getBoolean(); f.setBoldweight(b ? Font.BOLDWEIGHT_BOLD : Font.BOLDWEIGHT_NORMAL); } if (_struct.containsKey("underline")) { String v = _struct.getData("underline").getString(); Byte b = lookup_underline.get(v); if (b == null) { throw new Exception("invalid parameter for 'underline' (" + v + ")"); } else f.setUnderline(b); } if (_struct.containsKey("color")) { String v = _struct.getData("color").getString(); Short s = lookup_colors.get(v); if (s == null) { throw new Exception("invalid parameter for 'color' (" + v + ")"); } else f.setColor(s); } if (_struct.containsKey("fontsize")) { int s = _struct.getData("fontsize").getInt(); f.setFontHeightInPoints((short) s); } if (_struct.containsKey("font")) { f.setFontName(_struct.getData("font").getString()); } style.setFont(f); return style; }
From source file:org.cgiar.ccafs.ap.summaries.projects.xlsx.BaseXLS.java
License:Open Source License
/** * This method writes the title box into the given sheet. * /* ww w . j av a 2s . co m*/ * @param sheet is the sheet where you want to write the title box. * @param text is the title of the report. */ public void writeTitleBox(Sheet sheet, String text) { XSSFDrawing draw = (XSSFDrawing) sheet.createDrawingPatriarch(); XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 1, 1, 1, 1, 3, 6); anchor.setAnchorType(2); XSSFTextBox textbox = draw.createTextbox(anchor); textbox.setFillColor(TEXTBOX_BACKGROUND_COLOR_RGB.getRed(), TEXTBOX_BACKGROUND_COLOR_RGB.getGreen(), TEXTBOX_BACKGROUND_COLOR_RGB.getBlue()); textbox.setVerticalAlignment(VerticalAlignment.CENTER); XSSFRichTextString stringX = new XSSFRichTextString(); Font font = workbook.createFont(); font.setFontHeightInPoints((short) 20); font.setFontName("Tahoma"); font.setColor(TEXTBOX_FONT_COLOR_INDEX); stringX.append(text); stringX.applyFont(font); textbox.setText(stringX); }
From source file:org.eclipse.nebula.widgets.nattable.extension.poi.PoiExcelExporter.java
License:Open Source License
private CellStyle getExcelCellStyle(Color fg, Color bg, FontData fontData, String dataFormat, int hAlign, int vAlign, boolean vertical) { CellStyle xlCellStyle = xlCellStyles .get(new ExcelCellStyleAttributes(fg, bg, fontData, dataFormat, hAlign, vAlign, vertical)); if (xlCellStyle == null) { xlCellStyle = xlWorkbook.createCellStyle(); if (applyBackgroundColor) { // Note: xl fill foreground = background setFillForegroundColor(xlCellStyle, bg); xlCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); }// w w w . j a v a 2 s. c om Font xlFont = xlWorkbook.createFont(); setFontColor(xlFont, fg); xlFont.setFontName(fontData.getName()); xlFont.setFontHeightInPoints((short) fontData.getHeight()); xlCellStyle.setFont(xlFont); if (vertical) xlCellStyle.setRotation((short) 90); switch (hAlign) { case SWT.CENTER: xlCellStyle.setAlignment(CellStyle.ALIGN_CENTER); break; case SWT.LEFT: xlCellStyle.setAlignment(CellStyle.ALIGN_LEFT); break; case SWT.RIGHT: xlCellStyle.setAlignment(CellStyle.ALIGN_RIGHT); break; } switch (vAlign) { case SWT.TOP: xlCellStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP); break; case SWT.CENTER: xlCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); break; case SWT.BOTTOM: xlCellStyle.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM); break; } if (dataFormat != null) { CreationHelper createHelper = xlWorkbook.getCreationHelper(); xlCellStyle.setDataFormat(createHelper.createDataFormat().getFormat(dataFormat)); } xlCellStyles.put(new ExcelCellStyleAttributes(fg, bg, fontData, dataFormat, hAlign, vAlign, vertical), xlCellStyle); } return xlCellStyle; }
From source file:org.eclipse.scada.ae.ui.views.export.excel.impl.ExportEventsImpl.java
License:Open Source License
private void makeHeader(final List<Field> columns, final HSSFSheet sheet) { final Font font = sheet.getWorkbook().createFont(); font.setFontName("Arial"); font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setColor(HSSFColor.WHITE.index); final CellStyle style = sheet.getWorkbook().createCellStyle(); style.setFont(font);//from w w w .j ava 2 s. c o m style.setFillForegroundColor(HSSFColor.BLACK.index); style.setFillPattern(PatternFormatting.SOLID_FOREGROUND); final HSSFRow row = sheet.createRow(0); for (int i = 0; i < columns.size(); i++) { final Field field = columns.get(i); final HSSFCell cell = row.createCell(i); cell.setCellValue(field.getHeader()); cell.setCellStyle(style); } }
From source file:org.gaia.gui.reports.ExcelReportExporter.java
License:Open Source License
/** * generate file EXCEL/*w w w. j a v a 2 s . c o m*/ * * @param table * @param template */ public static void generateExcel(SortableTreeTable table, ReportTemplate template) { FileOutputStream fileOut = null; XSSFWorkbook wb = new XSSFWorkbook(); try { String excelFilename = generateFileName(template); fileOut = new FileOutputStream(excelFilename); List<TemplateColumnItem> items = ReportBuilder.orderColumns(template.getTemplateColumnItems()); TemplateColumnItem item; int colMax = table.getColumnModel().getColumnCount(); if (items.size() < colMax) { colMax = items.size(); } XSSFSheet bomSheet = (XSSFSheet) wb.createSheet(template.getTemplateName()); XSSFRow headerRow = (XSSFRow) bomSheet.createRow(0); XSSFCellStyle headerStyle = (XSSFCellStyle) wb.createCellStyle(); Font font = wb.createFont(); font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); font.setFontName("Tahoma"); headerStyle.setFont(font); headerStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); headerStyle.setBorderTop(XSSFCellStyle.BORDER_THIN); headerStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN); headerStyle.setBorderRight(XSSFCellStyle.BORDER_THIN); headerStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index); headerStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); for (int i = 0; i < colMax; i++) { XSSFCell cell = (XSSFCell) headerRow.createCell(i); cell.setCellType(XSSFCell.CELL_TYPE_STRING); cell.setCellValue(table.getColumnName(i)); cell.setCellStyle(headerStyle); } XSSFCellStyle cellStyle = (XSSFCellStyle) wb.createCellStyle(); font = wb.createFont(); font.setFontName("Tahoma"); cellStyle.setFont(font); cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN); for (int j = 0; j < table.getRowCount(); j++) { XSSFRow row = (XSSFRow) bomSheet.createRow(j + 1); for (int i = 0; i < colMax; i++) { XSSFCell cell = (XSSFCell) row.createCell(i); cell.setCellStyle(cellStyle); Object value = table.getValueAt(j, i); item = (TemplateColumnItem) items.get(i); if (value != null && !value.equals(StringUtils.EMPTY_STRING)) { Class<?> clazz = Class.forName(item.getReturnType()); //used for Snapshot Export if (clazz == String.class && NumberUtils.isInteger(value.toString())) { clazz = Integer.class; } else if (clazz == String.class && NumberUtils.isNumber(value.toString())) { clazz = BigDecimal.class; } if (short.class.isAssignableFrom(clazz) || Short.class.isAssignableFrom(clazz)) { cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(((Short) value).intValue()); } else if (int.class.isAssignableFrom(clazz) || Integer.class.isAssignableFrom(clazz)) { cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(Integer.parseInt(value.toString())); } else if (long.class.isAssignableFrom(clazz) || Long.class.isAssignableFrom(clazz)) { cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(((Long) value).intValue()); } else if (float.class.isAssignableFrom(clazz) || Float.class.isAssignableFrom(clazz)) { cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(((Float) value).doubleValue()); } else if (BigDecimal.class.isAssignableFrom(clazz)) { cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(Double.parseDouble(value.toString())); } else if (double.class.isAssignableFrom(clazz) || Double.class.isAssignableFrom(clazz)) { cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue((Double) value); } else if (Date.class.isAssignableFrom(clazz)) { cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(HSSFDateUtil.getExcelDate((java.sql.Date) value)); } else { cell.setCellType(XSSFCell.CELL_TYPE_STRING); cell.setCellValue(value.toString()); } } } } for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) { bomSheet.autoSizeColumn(i); } SortableTreeTableModel model = (SortableTreeTableModel) table.getTreeTableModel(); AbstractSortableTreeTableNode root = (AbstractSortableTreeTableNode) model.getRoot(); groupNode(root, bomSheet); wb.write(fileOut); fileOut.flush(); fileOut.close(); nodeList.clear(); groupList.clear(); allNodeList.clear(); openExcel(excelFilename); } catch (ClassNotFoundException | IOException ex) { Exceptions.printStackTrace(ex); } finally { try { fileOut.close(); } catch (IOException ex) { logger.error(ex); } } }
From source file:org.gedantic.web.servlet.WorkbookCreator.java
License:Open Source License
/** * Setup styles.//from w ww . ja v a2 s . c o m */ private void setupStyles() { styleTitle = wb.createCellStyle(); Font title_font = wb.createFont(); title_font.setFontName("Helvetica"); title_font.setColor(IndexedColors.BLACK.getIndex()); title_font.setFontHeightInPoints((short) 24); styleTitle.setFont(title_font); styleSubtitle = wb.createCellStyle(); Font subtitle_font = wb.createFont(); subtitle_font.setFontName("Helvetica"); subtitle_font.setColor(IndexedColors.GREY_50_PERCENT.getIndex()); subtitle_font.setFontHeightInPoints((short) 18); styleSubtitle.setFont(subtitle_font); styleHyperlink = wb.createCellStyle(); Font hlink_font = wb.createFont(); hlink_font.setFontName("Helvetica"); hlink_font.setUnderline(Font.U_SINGLE); hlink_font.setColor(IndexedColors.BLUE.getIndex()); styleHyperlink.setFont(hlink_font); styleNormal = wb.createCellStyle(); Font normal_font = wb.createFont(); normal_font.setFontName("Helvetica"); normal_font.setColor(IndexedColors.BLACK.getIndex()); normal_font.setFontHeightInPoints((short) 12); styleNormal.setFont(normal_font); styleNormal.setWrapText(true); styleHeader = wb.createCellStyle(); Font header_font = wb.createFont(); header_font.setFontName("Helvetica"); header_font.setColor(IndexedColors.WHITE.getIndex()); header_font.setBold(true); header_font.setFontHeightInPoints((short) 12); XSSFColor bg = new XSSFColor(new java.awt.Color(0x28, 0x60, 0x90)); ((XSSFCellStyle) styleHeader).setFillForegroundColor(bg); styleHeader.setFillPattern(CellStyle.SOLID_FOREGROUND); styleHeader.setFont(header_font); }
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 ww . j av a 2 s . com */ 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); } }