/** * 设置表头的单元格样式 * * @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; }
private void configFont(Font font) { font.setBold(isBold()); font.setItalic(isItalic()); font.setStrikeout(isStrikeout()); font.setUnderline(isUnderline() ? Font.U_SINGLE : Font.U_NONE); if (getFontSize() != null) { font.setFontHeightInPoints(fontSize.shortValue()); } if (getFontColor() != null) { if (font instanceof XSSFFont) { ((XSSFFont)font).setColor(new XSSFColor(toRgbByte(fontColor))); } else { font.setColor(fontColor.getIndex()); } } }
private static org.apache.poi.ss.usermodel.Font createPoiFont(PoiWorkbook workbook, Font other) { org.apache.poi.ss.usermodel.Font poiFont = workbook.getPoiWorkbook().createFont(); poiFont.setFontHeightInPoints((short) Math.round(other.getSizeInPoints())); poiFont.setFontName(other.getFamily()); final org.apache.poi.ss.usermodel.Color poiTextColor = workbook.getPoiColor(other.getColor()); if (poiFont instanceof HSSFFont && poiTextColor instanceof HSSFColor) { poiFont.setColor(((HSSFColor) poiTextColor).getIndex()); } else if (poiFont instanceof XSSFFont && poiTextColor instanceof XSSFColor) { ((XSSFFont) poiFont).setColor((XSSFColor) poiTextColor); } else { // it should both either be XSSF _or_ HSSF implementations so this // line should never be reached. throw new IllegalStateException(); } poiFont.setBold(other.isBold()); poiFont.setItalic(other.isItalic()); poiFont.setStrikeout(other.isStrikeThrough()); poiFont.setUnderline(other.isUnderlined() ? org.apache.poi.ss.usermodel.Font.U_SINGLE : org.apache.poi.ss.usermodel.Font.U_NONE); return poiFont; }
private int addLangCell(Row header, TmxSegement segment) { int CellNum = header.getLastCellNum(); if (-1 == CellNum) { CellNum = 0; } Cell createCell = header.createCell(CellNum); CellStyle cellStyle = wb.createCellStyle(); XSSFFont headerFont = (XSSFFont) wb.createFont(); headerFont.setBold(true); cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); cellStyle.setFont(headerFont); createCell.setCellValue(segment.getLangCode()); createCell.setCellStyle(cellStyle); sh.setColumnWidth(CellNum, (100 * 7 + 5) / 7 * 256); return CellNum; }
@Override public void addColourToFont(Workbook workbook, Font font, String colour) { if(colour == null) { return ; } if(IStyle.TRANSPARENT_VALUE.equals(colour)) { return ; } if(font instanceof XSSFFont) { XSSFFont xFont = (XSSFFont)font; XSSFColor xColour = getXColour(colour); if(xColour != null) { xFont.setColor(xColour); } } }
@Override public Font correctFontColorIfBackground( FontManager fm, Workbook wb, BirtStyle birtStyle, Font font ) { CSSValue bgColour = birtStyle.getProperty( StyleConstants.STYLE_BACKGROUND_COLOR ); int bgRgb[] = parseColour( bgColour == null ? null : bgColour.getCssText(), "white" ); XSSFColor colour = ((XSSFFont)font).getXSSFColor(); int fgRgb[] = rgbOnly( colour.getARgb() ); if( ( fgRgb[0] == 255 ) && ( fgRgb[1] == 255 ) && ( fgRgb[2] == 255 ) ) { fgRgb[0]=fgRgb[1]=fgRgb[2]=0; } else if( ( fgRgb[0] == 0 ) && ( fgRgb[1] == 0 ) && ( fgRgb[2] == 0 ) ) { fgRgb[0]=fgRgb[1]=fgRgb[2]=255; } if( ( bgRgb[ 0 ] == fgRgb[ 0 ] ) && ( bgRgb[ 1 ] == fgRgb[ 1 ] ) && ( bgRgb[ 2 ] == fgRgb[ 2 ] ) ) { IStyle addedStyle = new AreaStyle( fm.getCssEngine() ); addedStyle.setColor( contrastColour( bgRgb ) ); return fm.getFontWithExtraStyle( font, addedStyle ); } else { return font; } }
/** * See http://thinktibits.blogspot.co.uk/2012/12/Java-POI-XLS-XLSX-Change-Cell-Font-Color-Example.html * Currently only for xlsx * @param wb * @param sheet */ private static void styleHeader(Workbook wb, Sheet sheet){ if(XSSFWorkbook.class.isInstance(wb) && XSSFSheet.class.isInstance(sheet)){ XSSFWorkbook my_workbook = (XSSFWorkbook)wb; XSSFCellStyle my_style = my_workbook.createCellStyle(); XSSFFont my_font=my_workbook.createFont(); my_font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); my_style.setFont(my_font); Row row = sheet.getRow(0); if(row!=null && row.getFirstCellNum()>=0){ for(int i = row.getFirstCellNum() ; i<= row.getLastCellNum();i++){ Cell cell = row.getCell(i); if(cell!=null){ cell.setCellStyle(my_style); } } } } }
/** * Gets the string representation of the given <code>Font</code>. * @param f A <code>Font</code>. * @return The string representation. */ private String getRepresentation(Font f) { // Colors that need an instanceof check Color fontColor; if (f instanceof HSSFFont) { HSSFFont hf = (HSSFFont) f; fontColor = hf.getHSSFColor((HSSFWorkbook) myWorkbook); } else if (f instanceof XSSFFont) { XSSFFont xf = (XSSFFont) f; fontColor = xf.getXSSFColor(); } else throw new IllegalArgumentException("Bad Font type: " + f.getClass().getName()); return getRepresentation(f.getBoldweight(), f.getItalic(), fontColor, f.getFontName(), f.getFontHeightInPoints(), f.getUnderline(), f.getStrikeout(), f.getCharSet(), f.getTypeOffset()); }
private static Font findFont(Workbook workbook, Font font) { int numFonts = workbook.getNumberOfFonts(); for (short i = 0; i < numFonts; i++) { Font f = workbook.getFontAt(i); if (f.getBoldweight() == font.getBoldweight() && f.getItalic() == font.getItalic() && f.getColor() == font.getColor() && f.getFontHeight() == font.getFontHeight() && f.getUnderline() == font.getUnderline() && f.getFontName().equals(font.getFontName()) && f.getTypeOffset() == font.getTypeOffset() ) { if (!(font instanceof XSSFFont && f instanceof XSSFFont) || ((XSSFFont) font).getXSSFColor().getARGBHex().equals(((XSSFFont) f).getXSSFColor().getARGBHex())) { if (DEBUG) System.err.println(" Found existing, matching Font!"); return f; } } } if (DEBUG) System.err.println(" Did NOT find existing, matching Font!"); return null; }
/** * Public constructor. * @param os output stream */ public XLSXTranslatorOutputFormat(final OutputStream os) { if (os == null) { throw new NullPointerException("The output stream is null"); } this.os = os; // Temporary files will be compressed this.wb.setCompressTempFiles(true); // Define default style Font defaultFont = this.wb.createFont(); defaultFont.setFontName("Arial"); defaultFont.setFontHeightInPoints((short) 10); this.defaultStyle = this.wb.createCellStyle(); this.defaultStyle.setFont(defaultFont); // Define header style Font headerFont = this.wb.createFont(); headerFont.setFontName(defaultFont.getFontName()); headerFont.setFontHeightInPoints(defaultFont.getFontHeightInPoints()); headerFont.setItalic(true); this.headerStyle = this.wb.createCellStyle(); this.headerStyle.setFillForegroundColor(IndexedColors.ORANGE.getIndex()); this.headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); this.headerStyle.setFont(headerFont); // Define link style Font linkfont = this.wb.createFont(); linkfont.setFontName(defaultFont.getFontName()); linkfont.setFontHeightInPoints(defaultFont.getFontHeightInPoints()); linkfont.setUnderline(XSSFFont.U_SINGLE); linkfont.setColor(IndexedColors.BLUE.getIndex()); this.linkStyle = this.wb.createCellStyle(); this.linkStyle.setFont(linkfont); }
/** * @param wb * @return {code XSSFFont} */ public static XSSFFont createXSSFFont(XSSFWorkbook wb){ String message="XSSFWorkbook must not be null!"; Objects.requireNonNull(wb, () -> message); XSSFFont cellfont=wb.createFont(); cellfont.setFontName("新宋体"); cellfont.setFontHeightInPoints((short) 12); cellfont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); cellfont.setColor(new XSSFColor(new Color(50,73,38))); return cellfont; }
/** * @param wb * @param fontFamily * @param height * @param color * @param bold * @return */ public static XSSFFont createColorXSSFFont(XSSFWorkbook wb,String fontFamily,short height,XSSFColor color,boolean bold){ String message="XSSFWorkbook must not be null!"; Objects.requireNonNull(wb, () -> message); XSSFFont cellfont=wb.createFont(); cellfont.setFontName(fontFamily); cellfont.setFontHeightInPoints(height); cellfont.setColor(color); if(bold){ cellfont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); } return cellfont; }
/** * @param sheet * @param pos 位置【row,col】 * @param style * @param v */ public static void setCell(final XSSFWorkbook wb,final XSSFSheet sheet,int[] pos,CellStyle style,FontStyle font,String v){ XSSFRow row = sheet.getRow(pos[0]); XSSFCell cell = CellValueUtil.createCellIfNotPresent(row, pos[1]); XSSFCellStyle _style = Objects .requireNonNull(new XSSFCellStyleLib(wb).get(style), "specified style not defined!"); XSSFFont _font = Objects .requireNonNull(new XSSFFontStyleLib(wb).get(font), "specified font not defined!"); _style.setFont(_font); setCellProperties(cell, v, _style); }
/** * 返回副标题字体样式 * @param wb * @return */ public static XSSFCellStyle getCaptionCellStyle(XSSFWorkbook wb){ //default cellStyle XSSFCellStyle cellStyle=createXSSFCellStyle(wb); //default font XSSFFont cellfont=XSSFontUtil.createXSSFFont(wb); cellStyle.setFont(cellfont); return cellStyle; }
/** * 返回excel内容字体样式 * @param wb * @return */ public static XSSFCellStyle getcontentCellStyle(XSSFWorkbook wb){ String message="XSSFWorkbook must not be null!"; Objects.requireNonNull(wb, () -> message); XSSFCellStyle contentStyle=createCenterXSSFCellStyle(wb); //fill enlarged font XSSFFont enlargedFont=XSSFontUtil.createColorXSSFFont(wb, "新宋体", (short) 16, new XSSFColor(Color.WHITE), true); contentStyle.setFont(enlargedFont); return contentStyle; }
public XSSFFont get(FontStyle k){ XSSFFont v=FONT_STYLE_MAP.get(k); if(null==v){ throw new FontStyleNotFoundException("the font style have not been defined!"); } return v; }
private static XSSFCellStyle createCellStyle(Workbook workbook) { XSSFCellStyle xstyle = (XSSFCellStyle) workbook.createCellStyle(); XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontHeightInPoints((short) 11); xstyle.setFont(font); return xstyle; }
/** * 查找一模一样的字体对象 * * @author ZhengWei(HY) * @createDate 2017-11-16 * @version v1.0 * * @param i_Workbook * @param i_Font * @return */ public final static XSSFFont findFont(XSSFWorkbook i_Workbook ,XSSFFont i_Font) { return i_Workbook.getStylesSource().findFont(i_Font.getBold() ,i_Font.getColor() ,i_Font.getFontHeight() ,i_Font.getFontName() ,i_Font.getItalic() ,i_Font.getStrikeout() ,i_Font.getTypeOffset() ,i_Font.getUnderline()); }
/** * 创建单元格样式 * * @param workbook workbook * @param isBold 是否加粗 * @return 返回单元格样式 */ private CellStyle createCellFontBoldStyle(Workbook workbook, boolean isBold) { XSSFFont font = (XSSFFont) workbook.createFont(); font.setBold(isBold); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setFont(font); return cellStyle; }
private XSSFCellStyle createXSSFCellStyle(Workbook wb, int[] bgColor, int[] fontColor, int fontSize) { SXSSFWorkbook workbook = (SXSSFWorkbook) wb; XSSFFont titleFont = (XSSFFont) workbook.createFont(); titleFont.setCharSet(HSSFFont.DEFAULT_CHARSET); titleFont.setFontName("宋体"); XSSFColor color9 = new XSSFColor(new java.awt.Color(fontColor[0], fontColor[1], fontColor[2])); XSSFColor color10 = new XSSFColor(new java.awt.Color(bgColor[0], bgColor[1], bgColor[2])); if (!(fontColor[0] == 0 && fontColor[1] == 0 && fontColor[2] == 0)) { titleFont.setColor(color9); } titleFont.setBold(true); titleFont.setFontHeightInPoints((short) fontSize); XSSFCellStyle titleStyle = (XSSFCellStyle) createBorderCellStyle(workbook, true); titleStyle.setFont(titleFont); titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); titleStyle.setFillForegroundColor(color10); titleStyle.setAlignment(HorizontalAlignment.CENTER); titleStyle.setVerticalAlignment(VerticalAlignment.CENTER); return titleStyle; }
/** * 设置表体的单元格样式 * * @return */ public XSSFCellStyle getBodyStyle() { // 创建单元格样式 XSSFCellStyle cellStyle = wb.createCellStyle(); // 创建单元格内容显示不下时自动换行 //cellStyle.setWrapText(true); // 设置单元格字体样式 XSSFFont font = wb.createFont(); // 设置字体加粗 font.setFontName("宋体"); font.setFontHeight((short) 200); cellStyle.setFont(font); return cellStyle; }
/** * Create and send a form from the saved configuration. * * @param workbook Excel Workbook * @param boardStyle all properties suitable on the style of a cell * @return a font */ protected static Font getFontStyle(Workbook workbook, TableStyle boardStyle) { XSSFFont font = (XSSFFont) workbook.createFont(); font.setFontName(boardStyle.getFontName()); font.setFontHeightInPoints(boardStyle.getFontSize()); font.setBold(boardStyle.isBold()); font.setColor(boardStyle.getFontColor()); return font; }
private Color getColor(Font font) { if (helper instanceof HSSFHtmlHelper) { return ((HSSFWorkbook) sheet.getWorkbook()).getCustomPalette() .getColor(font.getColor()); } else { return ((XSSFFont) font).getXSSFColor(); } }
private static void writeHeader(final List<String> fieldNames, final Workbook workbook, final Sheet sheet) { final Row header = sheet.createRow(0); final CellStyle headerStyle = workbook.createCellStyle(); final XSSFFont font = ((XSSFWorkbook) workbook).createFont(); int counter = 0; Cell headerCell; font.setBold(true); headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); headerStyle.setFont(font); for (final String fieldName : fieldNames) { headerCell = header.createCell(counter++); headerCell.setCellValue(fieldName); headerCell.setCellStyle(headerStyle); } }
public static XSSFCellStyle setStyle(XSSFWorkbook workbook) { //设置字体; XSSFFont font = workbook.createFont(); //设置字体大小; font.setFontHeightInPoints((short) 20); //设置字体名字; font.setFontName("Courier New"); //font.setItalic(true); //font.setStrikeout(true); //设置样式; XSSFCellStyle style = workbook.createCellStyle(); //设置底边框; style.setBorderBottom(XSSFCellStyle.BORDER_THIN); //设置底边框颜色; style.setBottomBorderColor(new XSSFColor(Color.BLACK)); //设置左边框; style.setBorderLeft(XSSFCellStyle.BORDER_THIN); //设置左边框颜色; style.setLeftBorderColor(new XSSFColor(Color.BLACK)); //设置右边框; style.setBorderRight(XSSFCellStyle.BORDER_THIN); //设置右边框颜色; style.setRightBorderColor(new XSSFColor(Color.BLACK)); //设置顶边框; style.setBorderTop(XSSFCellStyle.BORDER_THIN); //设置顶边框颜色; style.setTopBorderColor(new XSSFColor(Color.BLACK)); //在样式用应用设置的字体; style.setFont(font); //设置自动换行; style.setWrapText(false); //设置水平对齐的样式为居中对齐; style.setAlignment(XSSFCellStyle.ALIGN_CENTER); //设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); return style; }
@Override protected void startSerialize( RootNode rootNode, OutputStream outputStream ) throws Exception { workbook = new XSSFWorkbook(); sheet = workbook.createSheet( "Sheet1" ); XSSFFont boldFont = workbook.createFont(); boldFont.setBold( true ); XSSFCellStyle boldCellStyle = workbook.createCellStyle(); boldCellStyle.setFont( boldFont ); // build schema for ( Node child : rootNode.getChildren() ) { if ( child.isCollection() ) { if ( !child.getChildren().isEmpty() ) { Node node = child.getChildren().get( 0 ); XSSFRow row = sheet.createRow( 0 ); int cellIdx = 0; for ( Node property : node.getChildren() ) { if ( property.isSimple() ) { XSSFCell cell = row.createCell( cellIdx++ ); cell.setCellValue( property.getName() ); cell.setCellStyle( boldCellStyle ); } } } } } }
@Override public PoiFont createFont(com.dua3.utility.text.Font font) { XSSFFont poiFont = (XSSFFont) poiWorkbook.createFont(); poiFont.setFontName(font.getFamily()); poiFont.setFontHeight(((short) Math.round(20*font.getSizeInPoints()))); poiFont.setColor(getPoiColor(font.getColor()).getIndex()); poiFont.setBold(font.isBold()); poiFont.setItalic(font.isItalic()); poiFont.setUnderline(font.isUnderlined() ? org.apache.poi.ss.usermodel.Font.U_SINGLE : org.apache.poi.ss.usermodel.Font.U_NONE); poiFont.setStrikeout(font.isStrikeThrough()); return new PoiFont(this, poiFont); }
/** * @param color * @param size * @param bold * @return Font */ protected XSSFFont getFont(XSSFColor color, short size, boolean bold) { XSSFFont font = wb.createFont(); font.setFontHeightInPoints(size); font.setFontName("Tahoma"); font.setColor(color); if(bold) font.setBoldweight(Font.BOLDWEIGHT_BOLD); return font; }
protected XSSFFont getFont(int color, short size, boolean bold) { XSSFFont font = wb.createFont(); font.setFontHeightInPoints(size); font.setFontName("Tahoma"); font.setColor((short)color); if(bold) font.setBoldweight(Font.BOLDWEIGHT_BOLD); return font; }
private Font readFont(XSSFFont poiFont) { Font font = new Font(); XSSFColor color = poiFont.getXSSFColor(); if (color == null) { font.setColor(Color.fromARGB(0, 0, 0)); } else { font.setColor(readColor(color)); } font.setName(poiFont.getFontName()); font.setSize(poiFont.getFontHeightInPoints()); font.setUnderline(poiFont.getUnderline() != org.apache.poi.ss.usermodel.Font.U_NONE); font.setItalic(poiFont.getItalic()); font.setBold(poiFont.getBoldweight() == org.apache.poi.ss.usermodel.Font.BOLDWEIGHT_BOLD); return font; }
private XSSFWorkbook createWorkbook() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFFont fontTitle = workbook.createFont(); fontTitle.setBold(true); titleStyle = workbook.createCellStyle(); titleStyle.setFont(fontTitle); titleStyle.setAlignment(CellStyle.ALIGN_CENTER); XSSFFont font = workbook.createFont(); font.setBold(true); headerStyle = workbook.createCellStyle(); headerStyle.setFillForegroundColor(new XSSFColor(new java.awt.Color(200, 200, 200))); headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); headerStyle.setFont(font); headerStyle.setBorderBottom(CellStyle.BORDER_THIN); headerStyle.setBorderTop(CellStyle.BORDER_THIN); headerStyle.setBorderLeft(CellStyle.BORDER_THIN); headerStyle.setBorderRight(CellStyle.BORDER_THIN); headerStyle.setAlignment(CellStyle.ALIGN_CENTER); tableStyle = workbook.createCellStyle(); tableStyle.setBorderBottom(CellStyle.BORDER_THIN); tableStyle.setBorderTop(CellStyle.BORDER_THIN); tableStyle.setBorderLeft(CellStyle.BORDER_THIN); tableStyle.setBorderRight(CellStyle.BORDER_THIN); return workbook; }