Refine search
private static XSSFFont createDefaultFont() { CTFont ctFont = CTFont.Factory.newInstance(); XSSFFont xssfFont=new XSSFFont(ctFont, 0, null); xssfFont.setFontHeightInPoints(XSSFFont.DEFAULT_FONT_SIZE); xssfFont.setColor(XSSFFont.DEFAULT_FONT_COLOR);//setTheme xssfFont.setFontName(XSSFFont.DEFAULT_FONT_NAME); xssfFont.setFamily(FontFamily.SWISS); xssfFont.setScheme(FontScheme.MINOR); return xssfFont; }
/** * Finds a font that matches the one with the supplied attributes, * where color is the actual Color-value, not the indexed color */ public XSSFFont findFont(boolean bold, Color color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) { for (XSSFFont font : fonts) { if ( (font.getBold() == bold) && font.getXSSFColor().equals(color) && font.getFontHeight() == fontHeight && font.getFontName().equals(name) && font.getItalic() == italic && font.getStrikeout() == strikeout && font.getTypeOffset() == typeOffset && font.getUnderline() == underline) { return font; } } return null; }
Workbook wb = new XSSFWorkbook("myWorkbook.xlsx"); Row row=sheet.getRow(0); CellStyle style=null; XSSFFont defaultFont= wb.createFont(); defaultFont.setFontHeightInPoints((short)10); defaultFont.setFontName("Arial"); defaultFont.setColor(IndexedColors.BLACK.getIndex()); defaultFont.setBold(false); defaultFont.setItalic(false); XSSFFont font= wb.createFont(); font.setFontHeightInPoints((short)10); font.setFontName("Arial"); font.setColor(IndexedColors.WHITE.getIndex()); font.setBold(true); font.setItalic(false); style=row.getRowStyle(); style.setFillBackgroundColor(IndexedColors.DARK_BLUE.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setAlignment(CellStyle.ALIGN_CENTER); style.setFont(font);
/** * Finds a font that matches the one with the supplied attributes, * where color is the indexed-value, not the actual color. */ public XSSFFont findFont(boolean bold, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) { for (XSSFFont font : fonts) { if ( (font.getBold() == bold) && font.getColor() == color && font.getFontHeight() == fontHeight && font.getFontName().equals(name) && font.getItalic() == italic && font.getStrikeout() == strikeout && font.getTypeOffset() == typeOffset && font.getUnderline() == underline) { return font; } } return null; }
style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setFillForegroundColor(buildXSSFColor(bgcolor)); fontSize=colStyle.getFontSize(); font.setFontHeight(fontSize); Boolean bold=cellStyle.getBold(); if(customStyle!=null && customStyle.getBold()!=null){ font.setBold(bold); font.setItalic(italic); font.setUnderline((byte)1); forecolor="0,0,0"; font.setColor(buildXSSFColor(forecolor)); String fontFamily=cellStyle.getFontFamily(); if(customStyle!=null && customStyle.getFontFamily()!=null){ font.setFontName(fontFamily); style.setFont(font);
titleFont.setFontHeightInPoints((short)48); titleFont.setColor(new XSSFColor(new java.awt.Color(39, 51, 89), wb.getStylesSource().getIndexedColors())); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); style.setFont(titleFont); styles.put("title", style); monthFont.setFontHeightInPoints((short)12); monthFont.setColor(new XSSFColor(new java.awt.Color(255, 255, 255), wb.getStylesSource().getIndexedColors())); monthFont.setBold(true); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); dayFont.setFontHeightInPoints((short)14); dayFont.setBold(true); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.LEFT);
fontDefault.setColor((short) HSSFFont.COLOR_NORMAL); fontDefault.setFontHeight((short) (font_size / 0.05)); fontDefault.setFontName("Tahoma"); styleDefault.setAlignment(XSSFCellStyle.ALIGN_CENTER); styleDefault.setBorderBottom(XSSFCellStyle.BORDER_THIN); styleDefault.setBorderTop(XSSFCellStyle.BORDER_THIN); styleDefault.setBorderLeft(XSSFCellStyle.BORDER_THIN); styleDefault.setBorderRight(XSSFCellStyle.BORDER_THIN); headerFont.setBold(true); rowHeaderStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); rowHeaderStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
protected void createExcelHeader(XSSFWorkbook workbook, XSSFSheet tableSheet) throws Exception { XSSFFont f = workbook.createFont(); f.setFontHeightInPoints((short) 12); f.setColor(IndexedColors.BLACK.getIndex()); XSSFDataFormat df = workbook.createDataFormat(); XSSFCellStyle cs = workbook.createCellStyle(); cs.setFont(f); cs.setDataFormat(df.getFormat("text")); cs.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); cs.setFillPattern(FillPatternType.SOLID_FOREGROUND); cs.setWrapText(true); XSSFCreationHelper createHelper = workbook.getCreationHelper(); XSSFRow rh = tableSheet.createRow(0); for (int i = 0; i < table.getColumnCount(); i++) { XSSFCell c1 = rh.createCell(i); c1.setCellStyle(cs); c1.setCellValue(createHelper.createRichTextString(table.getColumnName(i))); c1.setCellType(XSSFCell.CELL_TYPE_STRING); } }
private void fillHeaderRow(XSSFWorkbook xssfWorkbook, XSSFSheet sheet, Integer rowNum, Locale locale) { XSSFRow headerLine = sheet.createRow(rowNum); XSSFFont font = xssfWorkbook.createFont(); font.setFontHeightInPoints((short) 10); font.setFontName("ARIAL"); font.setItalic(false); font.setBold(true); font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setColor(HSSFColor.BLACK.index); XSSFCellStyle style = xssfWorkbook.createCellStyle(); style.setFont(font); Lists.newArrayList(MaintenanceEventsElementsReportEnum.values()).forEach( e -> createHeaderCell(e, headerLine, style, locale)); }
private void configureStyle(XSSFCellStyle style) { XSSFFont font = style.getFont(); if (font.getBold()) { _style.bold(); if (font.getItalic()) { _style.italic(); if (font.getUnderline() != FontUnderline.NONE.getByteValue()) { _style.underline(); if (style.getFillPatternEnum() == FillPatternType.SOLID_FOREGROUND) { XSSFColor fillForegroundXSSFColor = style .getFillForegroundXSSFColor(); String argb = fillForegroundXSSFColor.getARGBHex(); if (argb != null) { final XSSFFont stdFont = _stylesTable.getStyleAt(0).getFont(); final short fontHeight = style.getFont().getFontHeightInPoints(); if (stdFont.getFontHeightInPoints() != fontHeight) { _style.fontSize(fontHeight, SizeUnit.PT); XSSFColor fontColor = style.getFont().getXSSFColor(); if (fontColor != null) { String argbHex = fontColor.getARGBHex();
CellStyle createHeaderStyle(Workbook wb){ XSSFCellStyle xstyle = (XSSFCellStyle)wb.createCellStyle(); XSSFColor header = new XSSFColor(new Color(79, 129, 189)); xstyle.setFillForegroundColor(header); xstyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); XSSFFont font = (XSSFFont)wb.createFont(); font.setColor(IndexedColors.WHITE.index); xstyle.setFont(font); return xstyle; }
@Override public void prepareColumn(TableModel tableModel, XSSFSheet sheet, int col) { XSSFWorkbook wb = sheet.getWorkbook(); XSSFDataFormat df = wb.createDataFormat(); cellStyle = wb.createCellStyle(); XSSFFont f = wb.createFont(); f.setFontHeightInPoints((short) 12); f.setBold(true); f.setColor(IndexedColors.BLACK.getIndex()); cellStyle.setFont(f); cellStyle.setDataFormat(df.getFormat("text")); cellStyle.setBorderTop(CellStyle.BORDER_THIN); cellStyle.setBorderColor(BorderSide.TOP, new XSSFColor(new Color(0, 0, 0))); cellStyle.setBorderRight(CellStyle.BORDER_THIN); cellStyle.setBorderColor(BorderSide.RIGHT, new XSSFColor(new Color(0, 0, 0))); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBorderColor(BorderSide.BOTTOM, new XSSFColor(new Color(0, 0, 0))); cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setBorderColor(BorderSide.LEFT, new XSSFColor(new Color(0, 0, 0))); }
@Override public void prepareColumn(TableModel tableModel, XSSFSheet sheet, int col) { XSSFWorkbook wb = sheet.getWorkbook(); XSSFDataFormat df = wb.createDataFormat(); cellStyle = wb.createCellStyle(); XSSFFont f = wb.createFont(); f.setFontHeightInPoints((short) 12); f.setColor(IndexedColors.BLACK.getIndex()); cellStyle.setFont(f); cellStyle.setDataFormat(df.getFormat("m/d/yy")); }
styleNew.setAlignment(styleOld.getAlignment()); styleNew.setBorderBottom(styleOld.getBorderBottom()); styleNew.setBorderLeft(styleOld.getBorderLeft()); styleNew.setBorderRight(styleOld.getBorderRight()); styleNew.setBorderTop(styleOld.getBorderTop()); fontNew.setBoldweight(fontOld.getBoldweight()); fontNew.setCharSet(fontOld.getCharSet()); fontNew.setColor(fontOld.getColor()); fontNew.setFontName(fontOld.getFontName()); fontNew.setFontHeight(fontOld.getFontHeight()); fontNew.setItalic(fontOld.getItalic()); fontNew.setStrikeout(fontOld.getStrikeout()); fontNew.setTypeOffset(fontOld.getTypeOffset()); fontNew.setUnderline(fontOld.getUnderline()); return fontNew;
if (font == null || color != null && !font.getXSSFColor().equals(color) || color == null && font.getXSSFColor().getIndexed() != indexedColor) { font = workbook.createFont(); font.setColor(color); } else { font.setColor(indexedColor); font.setBold(boldWeight); font.setFontHeight(fontHeight); font.setFontName(name); font.setItalic(italic); font.setStrikeout(strikeout); font.setTypeOffset(typeOffset); font.setUnderline(underline);
style1.setAlignment(HorizontalAlignment.RIGHT); style1.setDataFormat(fmt.getFormat("0.0%")); styles.put("percent", style1); style2.setAlignment(HorizontalAlignment.CENTER); style2.setDataFormat(fmt.getFormat("0.0X")); styles.put("coeff", style2); headerFont.setBold(true); style5.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); style5.setFillPattern(FillPatternType.SOLID_FOREGROUND);
private static CellStyle getHeaderCellStyle(XSSFWorkbook wb){ // Cell Style XSSFCellStyle cellStyle = wb.createCellStyle(); XSSFFont font = wb.createFont(); // Font // font.setColor(HSSFColor.RED.index); font.setBold(true); font.setFamily(FontFamily.MODERN); // font.setFontHeight((short)300); cellStyle.setFont(font); // Align cellStyle.setAlignment(HorizontalAlignment.CENTER); // Border Line // cellStyle.setBorderTop(BorderStyle.THICK); // cellStyle.setBorderBottom(BorderStyle.DASH_DOT); // cellStyle.setBorderLeft(BorderStyle.DASHED); // cellStyle.setBorderRight(BorderStyle.DASHED); // Foreground color cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); return cellStyle; }
style.setAlign(Alignment.left); VerticalAlignment valign=cellStyle.getVerticalAlignmentEnum(); if(valign.equals(VerticalAlignment.BOTTOM)){ style.setValign(Alignment.bottom); style.setValign(Alignment.middle); XSSFFont font=cellStyle.getFont(); if(font.getBold()){ style.setBold(true); if(font.getItalic()){ style.setItalic(true); if(font.getUnderline()!=Font.U_NONE){ style.setUnderline(true); XSSFColor color=font.getXSSFColor(); if(color!=null){ String rgb=color.getARGBHex(); int fontSize=font.getFontHeight()/20; style.setFontSize(fontSize); BorderStyle borderStyle=cellStyle.getBorderLeftEnum();
@Override public void colorStyles(CellStyle style, Formatter out) { XSSFCellStyle cs = (XSSFCellStyle) style; styleColor(out, "background-color", cs.getFillForegroundXSSFColor()); styleColor(out, "color", cs.getFont().getXSSFColor()); }
/** * 设置表头的单元格样式 * * @return */ public XSSFCellStyle getHeadStyle() { // 创建单元格样式 XSSFCellStyle cellStyle = wb.createCellStyle(); // 设置单元格的背景颜色为淡蓝色 cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index); // 创建单元格内容显示不下时自动换行 //cellStyle.setWrapText(true); // 设置单元格字体样式 XSSFFont font = wb.createFont(); // 设置字体加粗 font.setFontName("宋体"); font.setFontHeight((short) 200); cellStyle.setFont(font); return cellStyle; }