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 ); 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 ); } }
/** 创建每个CellStyle,并放到map之中 * @param workbook * @param styleInfo * @param dataFormat * @return */ public static CellStyle doCreateCellStyle(Workbook workbook,ExportCellStyleInfo styleInfo,String dataFormat) { if(styleInfo != null ){ CellStyle cellStyle = workbook.createCellStyle(); setStyleValue(ExportCellStyleInfo.class, cellStyle, styleInfo); if(styleInfo.getFontStyleInfo() != null){ Font fontStyle = workbook.createFont(); setStyleValue(ExportFontStyleInfo.class, fontStyle, styleInfo.getFontStyleInfo()); cellStyle.setFont(fontStyle); } if(!StringUtils.isEmpty(dataFormat)){ short format = workbook.createDataFormat().getFormat(dataFormat); cellStyle.setDataFormat(format); } return cellStyle; } return null; }
public void setCellStyleFont(Workbook workbook, CellStyle style, int i) { Font font = workbook.createFont(); if (i == 0) { // 正常 } else if (i == 4) { // 下划线 font.setUnderline(Font.U_SINGLE); style.setFont(font); } else if (i == 2) { // 倾斜 font.setItalic(true); style.setFont(font); } else if (i == 1) { // 加粗 font.setBold(true); style.setFont(font); } }
/** * Returns the default title style. Obtained from: * http://svn.apache.org/repos/asf/poi * /trunk/src/examples/src/org/apache/poi/ss/examples/TimesheetDemo.java * * @param wb the wb * @return the cell style */ protected CellStyle defaultTitleCellStyle(final Workbook wb) { CellStyle style; final Font titleFont = wb.createFont(); titleFont.setFontHeightInPoints((short) 18); titleFont.setBold(true); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); style.setFont(titleFont); return style; }
/** * Returns the default header style. Obtained from: * http://svn.apache.org/repos/asf/poi * /trunk/src/examples/src/org/apache/poi/ss/examples/TimesheetDemo.java * * @param wb the wb * @return the cell style */ protected CellStyle defaultHeaderCellStyle(final Workbook wb) { CellStyle style; final Font monthFont = wb.createFont(); monthFont.setFontHeightInPoints((short) 11); monthFont.setColor(IndexedColors.WHITE.getIndex()); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setFont(monthFont); style.setWrapText(true); return style; }
public CellStyle createIndentationCellStyle(Workbook workbook, int s) { CellStyle dataStyle1 = this.createBorderCellStyle(workbook, true); Font dataFont = workbook.createFont(); dataFont.setColor((short) 12); dataFont.setFontHeightInPoints((short) 10); dataStyle1.setFillPattern(FillPatternType.SOLID_FOREGROUND); dataStyle1.setFillForegroundColor((short) 11); dataStyle1.setFont(dataFont); dataStyle1.setVerticalAlignment(VerticalAlignment.CENTER); dataStyle1.setAlignment(HorizontalAlignment.LEFT); dataStyle1.setIndention(Short.valueOf(String.valueOf((s)))); return dataStyle1; }
CellStyle createHeadStyle() { if (headerCellStyle != null) { return headerCellStyle; } CellStyle style = wb.createCellStyle(); // 设置这些样式 style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setAlignment(CellStyle.ALIGN_CENTER); // 生成一个字体 Font font = wb.createFont(); font.setColor(IndexedColors.BLACK.index); font.setFontHeightInPoints((short) 12); font.setBoldweight(Font.BOLDWEIGHT_BOLD); // 把字体应用到当前的样式 style.setFont(font); headerCellStyle = style; return style; }
CellStyle createHeadDateStyle() { if (headDateCellStyle != null) { return headDateCellStyle; } CellStyle cellStyle = wb.createCellStyle(); DataFormat format = wb.createDataFormat(); cellStyle.setDataFormat(format.getFormat("m/d/yy h:mm")); // 设置这些样式 cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // 生成一个字体 Font font = wb.createFont(); font.setColor(IndexedColors.BLACK.index); font.setFontHeightInPoints((short) 12); font.setBoldweight(Font.BOLDWEIGHT_BOLD); // 把字体应用到当前的样式 cellStyle.setFont(font); headDateCellStyle = cellStyle; return cellStyle; }
/** * Initialisation des polices */ protected void initFonts() { Font fontHeader = workbook.createFont(); fontHeader.setFontHeightInPoints(getHeaderFontHeight()); fontHeader.setFontName(getFontName()); fontHeader.setBoldweight(Font.BOLDWEIGHT_BOLD); setFontColor(fontHeader, colorRegistry, HEADER_FONT_COLOR_INDEX); registerFont(FONT_HEADER_NAME, fontHeader); Font fontNormal = workbook.createFont(); fontNormal.setFontHeightInPoints(getNormalFontHeight()); fontNormal.setFontName(getFontName()); registerFont(FONT_NORMAL_NAME, fontNormal); Font fontLink = workbook.createFont(); fontLink.setFontHeightInPoints(getNormalFontHeight()); fontLink.setFontName(getFontName()); fontLink.setUnderline(Font.U_SINGLE); setFontColor(fontLink, colorRegistry, LINK_FONT_COLOR_INDEX); registerFont(FONT_LINK_NAME, fontLink); }
private CellStyle createLinkStyle(boolean bordered) { final Font hlinkFont = wb.createFont(); hlinkFont.setUnderline(Font.U_SINGLE); hlinkFont.setColor(IndexedColors.BLUE.getIndex()); final CellStyle style; if (bordered) { style = createBorderedStyle(wb); } else { style = wb.createCellStyle(); } style.setFont(hlinkFont); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setIndention((short) 1); style.setWrapText(true); return style; }
/** * 初始化字体样式 * * @param font * @param fontBean */ private void initFont(Font font, FontStyleBean fontBean) { font.setFontName(fontBean.getName()); font.setFontHeightInPoints(fontBean.getSize()); if (fontBean.getBoldWeight() != null) { font.setBoldweight(fontBean.getBoldWeight()); } if (fontBean.getItalic() != null) { font.setItalic(fontBean.getItalic()); } if (fontBean.getUnderLine() != null) { font.setUnderline(fontBean.getUnderLine()); } if (fontBean.getColor() != null) { ColorBean cbean = fontBean.getColor(); ((XSSFFont) font).setColor(new XSSFColor(new Color(cbean.getR(), cbean.getG(), cbean.getB()))); } }
/** * @throws TechnicalException * is thrown if you have a technical error (format, configuration, datas, ...) in NoraUi. */ void openOutputData() throws TechnicalException { this.dataOutExtension = validExtension(dataOutPath); try (FileInputStream fileOut = new FileInputStream(dataOutPath + scenarioName + "." + dataOutExtension);) { initWorkbook(fileOut, dataOutExtension); } catch (final IOException e) { throw new TechnicalException(Messages.getMessage(TechnicalException.TECHNICAL_ERROR_MESSAGE_DATA_IOEXCEPTION), e); } styleSuccess = workbook.createCellStyle(); final Font fontSuccess = workbook.createFont(); fontSuccess.setColor(HSSFColor.HSSFColorPredefined.GREEN.getIndex()); styleSuccess.setFont(fontSuccess); styleFailed = workbook.createCellStyle(); final Font fontFailed = workbook.createFont(); fontFailed.setColor(HSSFColor.HSSFColorPredefined.RED.getIndex()); styleFailed.setFont(fontFailed); styleWarning = workbook.createCellStyle(); final Font fontWarning = workbook.createFont(); fontWarning.setColor(HSSFColor.HSSFColorPredefined.ORANGE.getIndex()); styleWarning.setFont(fontWarning); }
/** * 创建表格样式 * @param wb 工作薄对象 * @return 样式列表 */ private Map<String, CellStyle> createStyles(Workbook wb) { Map<String, CellStyle> styles = new HashMap<String, CellStyle>(); CellStyle style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); Font titleFont = wb.createFont(); titleFont.setFontName("Arial"); titleFont.setFontHeightInPoints((short) 16); titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD); style.setFont(titleFont); styles.put("title", style); style = wb.createCellStyle(); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); Font dataFont = wb.createFont(); dataFont.setFontName("Arial"); dataFont.setFontHeightInPoints((short) 10); style.setFont(dataFont); styles.put("data", style); style = wb.createCellStyle(); style.cloneStyleFrom(styles.get("data")); style.setAlignment(CellStyle.ALIGN_LEFT); styles.put("data1", style); style = wb.createCellStyle(); style.cloneStyleFrom(styles.get("data")); style.setAlignment(CellStyle.ALIGN_CENTER); styles.put("data2", style); style = wb.createCellStyle(); style.cloneStyleFrom(styles.get("data")); style.setAlignment(CellStyle.ALIGN_RIGHT); styles.put("data3", style); style = wb.createCellStyle(); style.cloneStyleFrom(styles.get("data")); // style.setWrapText(true); style.setAlignment(CellStyle.ALIGN_CENTER); style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); Font headerFont = wb.createFont(); headerFont.setFontName("Arial"); headerFont.setFontHeightInPoints((short) 10); headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); headerFont.setColor(IndexedColors.WHITE.getIndex()); style.setFont(headerFont); styles.put("header", style); return styles; }
/** * 复制字体 * * @author ZhengWei(HY) * @createDate 2017-03-18 * @version v1.0 * * @param i_FromFont 源字体 * @param i_ToFont 目标字体 */ public final static void copyFont(Font i_FromFont ,Font i_ToFont) { i_ToFont.setBold( i_FromFont.getBold()); i_ToFont.setCharSet( i_FromFont.getCharSet()); i_ToFont.setColor( i_FromFont.getColor()); i_ToFont.setFontHeight( i_FromFont.getFontHeight()); i_ToFont.setFontHeightInPoints(i_FromFont.getFontHeightInPoints()); i_ToFont.setFontName( i_FromFont.getFontName()); i_ToFont.setItalic( i_FromFont.getItalic()); i_ToFont.setStrikeout( i_FromFont.getStrikeout()); i_ToFont.setTypeOffset( i_FromFont.getTypeOffset()); i_ToFont.setUnderline( i_FromFont.getUnderline()); }
/** * 创建一个新的字体,字体从i_DataCell中克隆出来。 * * @author ZhengWei(HY) * @createDate 2017-09-11 * @version v1.0 * * @param i_ID 标记ID。由调用者设定 * @param i_DataCell 被克隆的单元格样式 * @return */ public synchronized Font getFontByCopy(String i_ID ,Cell i_DataCell ,RTemplate i_RTemplate) { Short v_FontID = this.fontsByCopy.get(i_ID); Font v_Font = null; if ( v_FontID == null ) { v_Font = this.workbook.createFont(); ExcelHelp.copyFont(this.workbook.getFontAt(i_DataCell.getCellStyle().getFontIndex()) ,v_Font); // 2017-09-20 动态格式的ID均加 Short.MAX_VALUE this.fonts.putRow(i_RTemplate ,String.valueOf(Short.MAX_VALUE + v_Font.getIndex()) ,v_Font); this.fontsByCopy.put(i_ID ,v_Font.getIndex()); } else { v_Font = this.getFont(i_RTemplate ,Short.MAX_VALUE + v_FontID); } return v_Font; }
/** * 设置通用的对齐居中、边框等 * * @param style 样式 */ private void setCommonStyle(CellStyle style) { // 设置单元格居中对齐、自动换行 style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setWrapText(true); //设置单元格字体 if (!buildInFontMap.containsKey(FONT_KEY)) { Font font = workbook.createFont(); //通用字体 font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontName("宋体"); font.setFontHeight((short) 200); buildInFontMap.put(FONT_KEY, font); } style.setFont(buildInFontMap.get(FONT_KEY)); // 设置单元格边框为细线条 style.setBorderLeft(CellStyle.BORDER_THIN); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBorderRight(CellStyle.BORDER_THIN); style.setBorderTop(CellStyle.BORDER_THIN); }
/** * Fixed all properties suitable for cell-related style. * * @param workbook Excel Workbook * @param boardStyle all properties suitable on the style of a cell * @param font a font * @return the customized style */ protected static CellStyle getCellStyle(Workbook workbook, TableStyle boardStyle, Font font) { XSSFCellStyle cellStyle = (XSSFCellStyle) workbook.createCellStyle(); if (boardStyle.getFillColor() != null) { cellStyle.setFillForegroundColor(boardStyle.getFillColor()); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); } cellStyle.setBorderLeft(boardStyle.getCellBorderLeft()); cellStyle.setBorderRight(boardStyle.getCellBorderRight()); cellStyle.setBorderTop(boardStyle.getCellBorderTop()); cellStyle.setBorderBottom(boardStyle.getCellBorderBottom()); cellStyle.setAlignment(boardStyle.getAlignment()); cellStyle.setBorderColor(BorderSide.LEFT, boardStyle.getBorderColor()); cellStyle.setBorderColor(BorderSide.RIGHT, boardStyle.getBorderColor()); cellStyle.setBorderColor(BorderSide.TOP, boardStyle.getBorderColor()); cellStyle.setBorderColor(BorderSide.BOTTOM, boardStyle.getBorderColor()); if (font != null) { cellStyle.setFont(font); } return cellStyle; }
private void fontStyle(Font font) { if (font.getBoldweight() >= Font.BOLDWEIGHT_BOLD) out.format(" font-weight: bold;%n"); if (font.getItalic()) out.format(" font-style: italic;%n"); out.format(" font-family: %s;%n", font.getFontName()); int fontheight = font.getFontHeightInPoints(); if (fontheight == 9) { fontheight = 10; } out.format(" font-size: %dpt;%n", fontheight); helper.styleColor(out, "color", getColor(font)); }
public CellStyle getBoldHeading() { if (boldHeading == null) { boldHeading = workbook.createCellStyle(); Font font = workbook.createFont(); font.setBold(true); boldHeading.setFont(font); } return boldHeading; }
@Override public void startExport(List<? extends IExportColumn<?>> columnList) throws Exception { if(m_started) throw new IllegalArgumentException("The writer was already started"); m_started = true; m_columnList = columnList; Workbook wb = m_workbook = createWorkbook(); Font defaultFont = wb.createFont(); defaultFont.setFontHeightInPoints((short) 10); defaultFont.setFontName("Arial"); CellStyle dcs = m_defaultCellStyle = wb.createCellStyle(); dcs.setFont(defaultFont); // FIXME Date format must be locale dependent? CellStyle dates = m_dateStyle = wb.createCellStyle(); dates.setDataFormat(wb.createDataFormat().getFormat("d-m-yyyy")); dates.setFont(defaultFont); CellStyle curs = m_currencyStyle = wb.createCellStyle(); curs.setDataFormat(wb.createDataFormat().getFormat("#,##0.00")); curs.setFont(defaultFont); CellStyle nums = m_numberStyle = wb.createCellStyle(); nums.setDataFormat(wb.createDataFormat().getFormat("#0")); nums.setFont(defaultFont); Font headerFont = wb.createFont(); headerFont.setFontHeightInPoints((short) 10); headerFont.setColor((short) 0xc); headerFont.setBold(true); headerFont.setFontName("Arial"); CellStyle hds = m_headerStyle = wb.createCellStyle(); hds.setBorderBottom(BorderStyle.THIN); hds.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); hds.setFont(headerFont); createNewSheet(columnList); }
/** * * @return */ protected CellStyle crateCaptionCellStyle() { Font font = workbook.createFont(); font.setColor(Font.COLOR_NORMAL); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setWrapText(false); font.setFontHeight((short) 250); cellStyle.setFont(font); cellStyle.setFillForegroundColor(IndexedColors.BLUE_GREY.index); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); cellStyle.setAlignment(CellStyle.ALIGN_CENTER); return cellStyle; }
/** * 表头单元格样式 * * @return */ protected CellStyle crateTitleCellStyle() { Font font = workbook.createFont(); font.setColor(Font.COLOR_NORMAL); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setWrapText(false); font.setFontHeight((short) 250); cellStyle.setFont(font); cellStyle.setFillForegroundColor(HSSFColor.BLUE_GREY.index); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); short border = 1; setCellBorder(cellStyle, border, border, border, border); cellStyle.setAlignment(CellStyle.ALIGN_LEFT); 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()); } } }
/** * * @Title: getCellStyle * @Description: TODO(设置表头样式) * @param wb * @return */ private CellStyle getHeadCellStyle(Workbook wb) { CellStyle style = wb.createCellStyle(); Font font = wb.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 12);// 设置字体大小 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗 style.setFillForegroundColor(HSSFColor.LIME.index);// 设置背景色 style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setAlignment(HSSFCellStyle.SOLID_FOREGROUND);// 让单元格居中 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setBorderTop(CellStyle.BORDER_THIN); style.setBorderLeft(CellStyle.BORDER_THIN); style.setBorderRight(CellStyle.BORDER_THIN); style.setBorderBottom(CellStyle.BORDER_THIN); // style.setWrapText(true);//设置自动换行 style.setFont(font); return style; }
/** * Set a bold font for the given cell with a given font size (in pt). * * @param wb * the workbook that contains the cell * @param cell * the cell where the text is contained * @param size * the size in pt of the text */ public static void setBold(Workbook wb, HSSFCell cell, short size) { Font font = wb.createFont(); font.setFontHeightInPoints((short) size); font.setFontName("Arial"); font.setColor(IndexedColors.BLACK.getIndex()); font.setBold(true); font.setItalic(false); CellStyle style = wb.createCellStyle(); style.setFont(font); cell.setCellStyle(style); }
/** * Set a link to a cell. The link type should one of {@link Hyperlink} * * @param wb * the workbook which contains the cell * @param cell * the cell where the link is stored * @param address * the cell destination address * @param linkType * the type selected among {@link Hyperlink} */ public static void setLink(Workbook wb, HSSFCell cell, String address, int linkType) { CreationHelper helper = wb.getCreationHelper(); CellStyle style = wb.createCellStyle(); Font font = wb.createFont(); font.setUnderline(Font.U_SINGLE); font.setColor(IndexedColors.BLUE.getIndex()); style.setFont(font); Hyperlink link = helper.createHyperlink(linkType); link.setAddress(address); cell.setHyperlink(link); cell.setCellStyle(style); }
/** * header样式 * * @return */ public CellStyle getHeaderStyle() { CellStyle headerStyle = workbook.createCellStyle(); // 设置单元格的背景颜色为淡蓝色 headerStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index); headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); // 设置单元格居中对齐 headerStyle.setAlignment(CellStyle.ALIGN_CENTER); // 设置单元格垂直居中对齐 headerStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 创建单元格内容显示不下时自动换行 headerStyle.setWrapText(true); // 设置单元格字体样式 Font font = workbook.createFont(); // 设置字体加粗 font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontName("宋体"); font.setFontHeight((short) 200); headerStyle.setFont(font); // 设置单元格边框为细线条 setBorder(headerStyle); return headerStyle; }
/** * 文本样式 * * @return */ public CellStyle getTextStyle() { CellStyle textStyle = workbook.createCellStyle(); // 设置单元格居中对齐 textStyle.setAlignment(CellStyle.ALIGN_CENTER); // 设置单元格垂直居中对齐 textStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 创建单元格内容显示不下时自动换行 textStyle.setWrapText(true); // 设置单元格字体样式 Font font = workbook.createFont(); // 设置字体加粗 font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontName("宋体"); font.setFontHeight((short) 200); textStyle.setFont(font); // 设置单元格边框为细线条 setBorder(textStyle); return textStyle; }
public CellStyle getNumberStyle() { CellStyle numberStyle = workbook.createCellStyle(); // 设置单元格居中对齐 numberStyle.setAlignment(CellStyle.ALIGN_CENTER); // 设置单元格垂直居中对齐 numberStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 创建单元格内容显示不下时自动换行 numberStyle.setWrapText(true); // 设置单元格字体样式 Font font = workbook.createFont(); // 设置字体加粗 font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontName("宋体"); font.setFontHeight((short) 200); numberStyle.setFont(font); // 设置单元格边框为细线条 setBorder(numberStyle); return numberStyle; }
public CellStyle getDateStyle() { CellStyle dateStyle = workbook.createCellStyle(); // 设置单元格居中对齐 dateStyle.setAlignment(CellStyle.ALIGN_CENTER); // 设置单元格垂直居中对齐 dateStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); DataFormat format = workbook.createDataFormat(); dateStyle.setDataFormat(format.getFormat("yyyy-MM-dd HH:mm")); // 创建单元格内容显示不下时自动换行 dateStyle.setWrapText(true); // 设置单元格字体样式 Font font = workbook.createFont(); // 设置字体加粗 font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontName("宋体"); font.setFontHeight((short) 200); dateStyle.setFont(font); setBorder(dateStyle); return dateStyle; }
@Override public PoiFont createFont(com.dua3.utility.text.Font font) { Font poiFont = 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); }
@Test public void test2() throws IOException{ Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet("new sheet"); // Create a row and put some cells in it. Rows are 0 based. Row row = sheet.createRow(1); // Create a cell and put a value in it. // Style the cell with borders all around. CellStyle style = wb.createCellStyle(); // style.setFillBackgroundColor(IndexedColors.AUTOMATIC.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.index); Font font = wb.createFont(); font.setFontHeightInPoints((short)24); font.setFontName("Courier New"); font.setItalic(true); font.setStrikeout(true); style.setFont(font); CellUtil.createCell(row, 1, "nihao",style); //style.setFont(font); // Write the output to a file FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); }
/** * @param color * @param font * @return CellStyle */ protected XSSFCellStyle getCellStyle(XSSFColor color, Font font) { XSSFCellStyle style = wb.createCellStyle(); style.setFillForegroundColor(color); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setWrapText(true); // new line style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); if(font != null) style.setFont(font); return style; }
private CellStyle getHeaderCellStyle(final Workbook wb) { CellStyle cellStyle = wb.createCellStyle(); Font boldFont = wb.createFont(); boldFont.setBoldweight(Font.BOLDWEIGHT_BOLD); cellStyle.setFont(boldFont); return cellStyle; }
public static void main(String[] args) throws Exception { String dataPath = "src/featurescomparison/workingwithformattingfeatures/workingwithfonts/data/"; Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet("new sheet"); // Create a row and put some cells in it. Rows are 0 based. Row row = sheet.createRow(1); // Create a new font and alter it. Font font = wb.createFont(); font.setFontHeightInPoints((short)24); font.setFontName("Courier New"); font.setItalic(true); font.setStrikeout(true); // Fonts are set into a style so create a new one to use. CellStyle style = wb.createCellStyle(); style.setFont(font); // Create a cell and put a value in it. Cell cell = row.createCell(1); cell.setCellValue("This is a test of fonts"); cell.setCellStyle(style); // Write the output to a file FileOutputStream fileOut = new FileOutputStream(dataPath + "ApacheFonts.xls"); wb.write(fileOut); fileOut.close(); System.out.println("Apache Fonts Created."); }
private CellStyle createTitleBarStyle(Workbook wb) { CellStyle style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); Font font = wb.createFont(); font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontHeight((short) (font.getFontHeight() * 1.5)); style.setFont(font); return style; }
protected CellStyle createColumnHeaderStyle(Workbook wb) { CellStyle style = wb.createCellStyle(); style.setAlignment(CellStyle.ALIGN_CENTER); style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); Font font = wb.createFont(); font.setBoldweight(Font.BOLDWEIGHT_BOLD); style.setFont(font); style.setBorderTop(CellStyle.BORDER_THIN); style.setBorderRight(CellStyle.BORDER_THIN); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBorderLeft(CellStyle.BORDER_THIN); return style; }