Java Code Examples for org.apache.poi.ss.usermodel.CellStyle#setFont()
The following examples show how to use
org.apache.poi.ss.usermodel.CellStyle#setFont() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StyleConfiguration.java From excel-rw-annotation with Apache License 2.0 | 6 votes |
/** * 设置通用的对齐居中、边框等 * * @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); }
Example 2
Source File: ThDefaultCellStyle.java From myexcel with Apache License 2.0 | 5 votes |
@Override public CellStyle supply(Workbook workbook) { CellStyle style = workbook.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); style.setBorderBottom(BorderStyle.THIN); style.setBorderRight(BorderStyle.THIN); style.setBorderTop(BorderStyle.THIN); style.setBorderLeft(BorderStyle.THIN); Font font = workbook.createFont(); font.setBold(true); style.setFont(font); return style; }
Example 3
Source File: CrosstabXLSExporter.java From Knowage-Server with GNU Affero General Public License v3.0 | 5 votes |
public CellStyle buildDimensionCellStyle(Sheet sheet) { CellStyle cellStyle = sheet.getWorkbook().createCellStyle(); cellStyle.setAlignment(CellStyle.ALIGN_CENTER); cellStyle.setVerticalAlignment(CellStyle.ALIGN_CENTER); String headerBGColor = (String) this.getProperty(PROPERTY_DIMENSION_NAME_BACKGROUND_COLOR); logger.debug("Header background color : " + headerBGColor); short backgroundColorIndex = headerBGColor != null ? IndexedColors.valueOf(headerBGColor).getIndex() : IndexedColors.valueOf( DEFAULT_DIMENSION_NAME_BACKGROUND_COLOR).getIndex(); cellStyle.setFillForegroundColor(backgroundColorIndex); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setBorderRight(CellStyle.BORDER_THIN); cellStyle.setBorderTop(CellStyle.BORDER_THIN); String bordeBorderColor = (String) this.getProperty(PROPERTY_HEADER_BORDER_COLOR); logger.debug("Header border color : " + bordeBorderColor); short borderColorIndex = bordeBorderColor != null ? IndexedColors.valueOf(bordeBorderColor).getIndex() : IndexedColors.valueOf( DEFAULT_HEADER_BORDER_COLOR).getIndex(); cellStyle.setLeftBorderColor(borderColorIndex); cellStyle.setRightBorderColor(borderColorIndex); cellStyle.setBottomBorderColor(borderColorIndex); cellStyle.setTopBorderColor(borderColorIndex); Font font = sheet.getWorkbook().createFont(); Short headerFontSize = (Short) this.getProperty(PROPERTY_HEADER_FONT_SIZE); logger.debug("Header font size : " + headerFontSize); short headerFontSizeShort = headerFontSize != null ? headerFontSize.shortValue() : DEFAULT_HEADER_FONT_SIZE; font.setFontHeightInPoints(headerFontSizeShort); String fontName = (String) this.getProperty(PROPERTY_FONT_NAME); logger.debug("Font name : " + fontName); fontName = fontName != null ? fontName : DEFAULT_FONT_NAME; font.setFontName(fontName); String color = (String) this.getProperty(PROPERTY_DIMENSION_NAME_COLOR); logger.debug("Dimension color : " + color); short colorIndex = bordeBorderColor != null ? IndexedColors.valueOf(color).getIndex() : IndexedColors.valueOf(DEFAULT_DIMENSION_NAME_COLOR).getIndex(); font.setColor(colorIndex); font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setItalic(true); cellStyle.setFont(font); return cellStyle; }
Example 4
Source File: ExcelLogWriter.java From axelor-open-suite with GNU Affero General Public License v3.0 | 5 votes |
private CellStyle setStyle() { CellStyle cellStyle = workbook.createCellStyle(); Font font = workbook.createFont(); font.setBoldweight(Font.BOLDWEIGHT_BOLD); cellStyle.setFont(font); return cellStyle; }
Example 5
Source File: ExcelExportStylerDefaultImpl.java From easypoi with Apache License 2.0 | 5 votes |
@Override public CellStyle getHeaderStyle(short color) { CellStyle titleStyle = workbook.createCellStyle(); Font font = workbook.createFont(); font.setFontHeightInPoints((short) 12); titleStyle.setFont(font); titleStyle.setAlignment(CellStyle.ALIGN_CENTER); titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); return titleStyle; }
Example 6
Source File: SpreadsheetExporter.java From sakai with Educational Community License v2.0 | 5 votes |
private CellStyle createHeaderStyle(){ //TO-DO read style information from sakai.properties Font font = gradesWorkbook.createFont(); font.setFontName(HSSFFont.FONT_ARIAL); font.setColor(IndexedColors.PLUM.getIndex()); font.setBold(true); CellStyle cellStyle = gradesWorkbook.createCellStyle(); cellStyle.setFont(font); return cellStyle; }
Example 7
Source File: AbstractSheet.java From tools with Apache License 2.0 | 5 votes |
public static CellStyle createHeaderStyle(Workbook wb) { CellStyle headerStyle = wb.createCellStyle(); headerStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); headerStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); Font headerFont = wb.createFont(); headerFont.setFontName("Arial"); headerFont.setFontHeight(FONT_SIZE); headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); headerStyle.setFont(headerFont); headerStyle.setAlignment(CellStyle.ALIGN_CENTER); return headerStyle; }
Example 8
Source File: ExcelExportStylerBorderImpl.java From jeasypoi with Apache License 2.0 | 5 votes |
@Override public CellStyle getHeaderStyle(short color) { CellStyle titleStyle = workbook.createCellStyle(); Font font = workbook.createFont(); font.setFontHeightInPoints((short) 12); titleStyle.setFont(font); titleStyle.setBorderLeft((short) 1); // 左边框 titleStyle.setBorderRight((short) 1); // 右边框 titleStyle.setBorderBottom((short) 1); titleStyle.setBorderTop((short) 1); titleStyle.setAlignment(CellStyle.ALIGN_CENTER); titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); return titleStyle; }
Example 9
Source File: SpreadsheetExporter.java From sakai with Educational Community License v2.0 | 5 votes |
private CellStyle createHeaderStyle(){ //TO-DO read style information from sakai.properties Font font = gradesWorkbook.createFont(); font.setFontName(HSSFFont.FONT_ARIAL); font.setColor(IndexedColors.PLUM.getIndex()); font.setBold(true); CellStyle cellStyle = gradesWorkbook.createCellStyle(); cellStyle.setFont(font); return cellStyle; }
Example 10
Source File: ExcelExportStylerColorImpl.java From jeasypoi with Apache License 2.0 | 5 votes |
@Override public CellStyle getHeaderStyle(short headerColor) { CellStyle titleStyle = workbook.createCellStyle(); Font font = workbook.createFont(); font.setFontHeightInPoints((short) 24); titleStyle.setFont(font); titleStyle.setFillForegroundColor(headerColor); titleStyle.setAlignment(CellStyle.ALIGN_CENTER); titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); return titleStyle; }
Example 11
Source File: ExportUtils.java From tech-gallery with Apache License 2.0 | 5 votes |
private static void makeRowBold(HSSFWorkbook wb, Row row) { CellStyle style = wb.createCellStyle(); Font font = wb.createFont(); font.setBold(true); style.setFont(font); for (int i = 0; i < row.getLastCellNum(); i++) { row.getCell(i).setCellStyle(style); } }
Example 12
Source File: UKExcelUtil.java From youkefu with Apache License 2.0 | 5 votes |
/** * 内容样式 * @return */ @SuppressWarnings("deprecation") private CellStyle createContentCellStyle(){ CellStyle cellStyle = baseCellStyle(); Font font = wb.createFont(); font.setFontHeight((short) 200); font.setBoldweight((short)0); cellStyle.setFont(font); return cellStyle; }
Example 13
Source File: ExcelStyle.java From objectlabkit with Apache License 2.0 | 5 votes |
private void addFontFormat(ExcelCell cell, CellStyle cellStyle) { if (bold || italic || underline || header || fontColour != null || strikeout) { Font f = cell.workbook().createFont(); f.setBold(bold || header); if (underline) { f.setUnderline(Font.U_SINGLE); } if (fontColour != null) { f.setColor(fontColour.getIndex()); } f.setItalic(italic); f.setStrikeout(strikeout); cellStyle.setFont(f); } }
Example 14
Source File: ExcelHandler.java From development with Apache License 2.0 | 5 votes |
private static CellStyle initializeSheet(Workbook wb, Sheet sheet) { PrintSetup printSetup = sheet.getPrintSetup(); printSetup.setLandscape(true); sheet.setFitToPage(true); sheet.setHorizontallyCenter(true); CellStyle styleTitle; Font titleFont = wb.createFont(); titleFont.setFontHeightInPoints((short) 12); titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD); titleFont.setFontName("Arial"); styleTitle = wb.createCellStyle(); styleTitle.setFont(titleFont); return styleTitle; }
Example 15
Source File: DefaultCellStyleBuilder.java From onetwo with Apache License 2.0 | 4 votes |
protected CellStyle buildCellStyle(CellContextData cellContext){ FieldModel field = cellContext.getFieldModel(); String styleString = getStyle(field); String fontString = getFont(field); if(ExcelUtils.isBlank(field.getDataFormat()) && ExcelUtils.isBlank(styleString) && ExcelUtils.isBlank(fontString)){ return null; } if(!ExcelUtils.isBlank(styleString) && styleString.startsWith("#")){ styleString = (String)cellContext.parseValue(styleString); } if(!ExcelUtils.isBlank(field.getDataFormat())){ styleString += ";dataFormat:"+HSSFDataFormat.getBuiltinFormat(field.getDataFormat()); } if(!ExcelUtils.isBlank(fontString) && fontString.startsWith("#")){ fontString = (String)cellContext.parseValue(fontString); } String key = styleString + fontString; CellStyle cstyle = this.styleCache.get(key); if(cstyle!=null){ // System.out.println("get style from cache"); return cstyle; } cstyle = this.generator.getWorkbook().createCellStyle(); BeanWrapper bw = ExcelUtils.newBeanWrapper(cstyle); Map<String, String> styleMap = this.generator.getPropertyStringParser().parseStyle(styleString); try { for(Entry<String, String> entry : styleMap.entrySet()){ /*if(isStaticField(CellStyle.class, entry.getValue())){ Object styleValue = ReflectUtils.getStaticFieldValue(CellStyle.class, getStaticField(entry.getValue())); ReflectUtils.setProperty(cstyle, entry.getKey(), styleValue); }else{ bw.setPropertyValue(entry.getKey(), entry.getValue()); }*/ bw.setPropertyValue(entry.getKey(), getStyleValue(entry.getValue())); } } catch (Exception e) { throw ExcelUtils.wrapAsUnCheckedException(cellContext.getLocation()+" buildCellStyle error: " + e.getMessage(), e); } Font font = buildCellFont(cellContext, fontString); if(font!=null){ cstyle.setFont(font); } this.styleCache.putIfAbsent(key, cstyle); return cstyle; }
Example 16
Source File: HRExcelBuilder.java From Spring-MVC-Blueprints with MIT License | 4 votes |
@Override protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { // get data model which is passed by the Spring container @SuppressWarnings("unchecked") List<HrmsLogin> users = (List<HrmsLogin>) model.get("allUsers"); // create a new Excel sheet HSSFSheet sheet = workbook.createSheet("User List"); sheet.setDefaultColumnWidth(30); // create style for header cells CellStyle style = workbook.createCellStyle(); Font font = workbook.createFont(); font.setFontName("Arial"); style.setFillForegroundColor(HSSFColor.BLUE.index); style.setFillPattern(CellStyle.SOLID_FOREGROUND); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); font.setColor(HSSFColor.WHITE.index); style.setFont(font); // create header row HSSFRow header = sheet.createRow(0); header.createCell(0).setCellValue("Employee ID"); header.getCell(0).setCellStyle(style); header.createCell(1).setCellValue("Username"); header.getCell(1).setCellStyle(style); header.createCell(2).setCellValue("Password"); header.getCell(2).setCellStyle(style); header.createCell(3).setCellValue("Role"); header.getCell(3).setCellStyle(style); // create data rows int rowCount = 1; for (HrmsLogin account : users) { HSSFRow aRow = sheet.createRow(rowCount++); aRow.createCell(0).setCellValue(account.getHrmsEmployeeDetails().getEmpId()); aRow.createCell(1).setCellValue(account.getUsername()); aRow.createCell(2).setCellValue(account.getPassword()); aRow.createCell(3).setCellValue(account.getRole()); } }
Example 17
Source File: QbeXLSExporter.java From Knowage-Server with GNU Affero General Public License v3.0 | 4 votes |
public CellStyle buildCellStyle(Sheet sheet) { CellStyle cellStyle = sheet.getWorkbook().createCellStyle(); cellStyle.setAlignment(CellStyle.ALIGN_RIGHT); cellStyle.setVerticalAlignment(CellStyle.ALIGN_CENTER); String cellBGColor = (String) this.getProperty(PROPERTY_CELL_BACKGROUND_COLOR); logger.debug("Cell background color : " + cellBGColor); short backgroundColorIndex = cellBGColor != null ? IndexedColors.valueOf(cellBGColor).getIndex() : IndexedColors.valueOf(DEFAULT_CELL_BACKGROUND_COLOR).getIndex(); cellStyle.setFillForegroundColor(backgroundColorIndex); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setBorderRight(CellStyle.BORDER_THIN); cellStyle.setBorderTop(CellStyle.BORDER_THIN); String bordeBorderColor = (String) this.getProperty(PROPERTY_CELL_BORDER_COLOR); logger.debug("Cell border color : " + bordeBorderColor); short borderColorIndex = bordeBorderColor != null ? IndexedColors.valueOf(bordeBorderColor).getIndex() : IndexedColors.valueOf(DEFAULT_CELL_BORDER_COLOR).getIndex(); cellStyle.setLeftBorderColor(borderColorIndex); cellStyle.setRightBorderColor(borderColorIndex); cellStyle.setBottomBorderColor(borderColorIndex); cellStyle.setTopBorderColor(borderColorIndex); Font font = sheet.getWorkbook().createFont(); Short cellFontSize = (Short) this.getProperty(PROPERTY_CELL_FONT_SIZE); logger.debug("Cell font size : " + cellFontSize); short cellFontSizeShort = cellFontSize != null ? cellFontSize.shortValue() : DEFAULT_CELL_FONT_SIZE; font.setFontHeightInPoints(cellFontSizeShort); String fontName = (String) this.getProperty(PROPERTY_FONT_NAME); logger.debug("Font name : " + fontName); fontName = fontName != null ? fontName : DEFAULT_FONT_NAME; font.setFontName(fontName); String cellColor = (String) this.getProperty(PROPERTY_CELL_COLOR); logger.debug("Cell color : " + cellColor); short cellColorIndex = cellColor != null ? IndexedColors.valueOf(cellColor).getIndex() : IndexedColors.valueOf(DEFAULT_CELL_COLOR).getIndex(); font.setColor(cellColorIndex); cellStyle.setFont(font); return cellStyle; }
Example 18
Source File: SpreadsheetDataFileWriterXls.java From sakai with Educational Community License v2.0 | 4 votes |
private Workbook getAsWorkbook(List<List<Object>> spreadsheetData) { Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet(); CellStyle headerCs = wb.createCellStyle(); Iterator<List<Object>> dataIter = spreadsheetData.iterator(); // Set the header style headerCs.setBorderBottom(BorderStyle.THICK); headerCs.setFillBackgroundColor(IndexedColors.BLUE_GREY.getIndex()); // Set the font CellStyle cellStyle = null; String fontName = ServerConfigurationService.getString("spreadsheet.font"); if (fontName != null) { Font font = wb.createFont(); font.setFontName(fontName); headerCs.setFont(font); cellStyle = wb.createCellStyle(); cellStyle.setFont(font); } // By convention, the first list in the list contains column headers. Row headerRow = sheet.createRow((short)0); List<Object> headerList = dataIter.next(); for (short i = 0; i < headerList.size(); i++) { Cell headerCell = createCell(headerRow, i); headerCell.setCellValue((String)headerList.get(i)); headerCell.setCellStyle(headerCs); sheet.autoSizeColumn(i); } short rowPos = 1; while (dataIter.hasNext()) { List<Object> rowData = dataIter.next(); Row row = sheet.createRow(rowPos++); for (short i = 0; i < rowData.size(); i++) { Cell cell = createCell(row, i); Object data = rowData.get(i); if (data != null) { if (data instanceof Double) { cell.setCellValue(((Double)data).doubleValue()); } else { cell.setCellValue(data.toString()); } if (cellStyle != null) { cell.setCellStyle(cellStyle); } } } } return wb; }
Example 19
Source File: DataSetExportServicesImpl.java From dashbuilder with Apache License 2.0 | 4 votes |
private Map<String, CellStyle> createStyles(Workbook wb){ Map<String, CellStyle> styles = new HashMap<>(); CellStyle style; Font titleFont = wb.createFont(); titleFont.setFontHeightInPoints((short)12); titleFont.setBold(true); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); style.setFillForegroundColor( IndexedColors.GREY_25_PERCENT.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setFont(titleFont); style.setWrapText(false); style.setBorderBottom(BorderStyle.THIN); style.setBottomBorderColor(IndexedColors.GREY_80_PERCENT.getIndex()); styles.put("header", style); Font cellFont = wb.createFont(); cellFont.setFontHeightInPoints((short)10); cellFont.setBold(true); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.RIGHT); style.setVerticalAlignment(VerticalAlignment.BOTTOM); style.setFont(cellFont); style.setWrapText(false); style.setDataFormat(wb.createDataFormat().getFormat( BuiltinFormats.getBuiltinFormat( 3 ))); styles.put("integer_number_cell", style); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.RIGHT); style.setVerticalAlignment(VerticalAlignment.BOTTOM); style.setFont(cellFont); style.setWrapText(false); style.setDataFormat(wb.createDataFormat().getFormat(BuiltinFormats.getBuiltinFormat(4))); styles.put("decimal_number_cell", style); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.LEFT); style.setVerticalAlignment(VerticalAlignment.BOTTOM); style.setFont(cellFont); style.setWrapText(false); style.setDataFormat( (short) BuiltinFormats.getBuiltinFormat("text") ); styles.put(TEXT_CELL, style); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.BOTTOM); style.setFont(cellFont); style.setWrapText(false); style.setDataFormat(wb.createDataFormat().getFormat( DateFormatConverter.convert( Locale.getDefault(), dateFormatPattern ))); styles.put("date_cell", style); return styles; }
Example 20
Source File: CrosstabXLSExporter.java From Knowage-Server with GNU Affero General Public License v3.0 | 4 votes |
public CellStyle buildDataCellStyle(Sheet sheet) { CellStyle cellStyle = sheet.getWorkbook().createCellStyle(); cellStyle.setAlignment(CellStyle.ALIGN_RIGHT); cellStyle.setVerticalAlignment(CellStyle.ALIGN_CENTER); String cellBGColor = (String) this.getProperty(PROPERTY_CELL_BACKGROUND_COLOR); logger.debug("Cell background color : " + cellBGColor); short backgroundColorIndex = cellBGColor != null ? IndexedColors.valueOf(cellBGColor).getIndex() : IndexedColors.valueOf(DEFAULT_CELL_BACKGROUND_COLOR) .getIndex(); cellStyle.setFillForegroundColor(backgroundColorIndex); cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBorderLeft(CellStyle.BORDER_THIN); cellStyle.setBorderRight(CellStyle.BORDER_THIN); cellStyle.setBorderTop(CellStyle.BORDER_THIN); String bordeBorderColor = (String) this.getProperty(PROPERTY_CELL_BORDER_COLOR); logger.debug("Cell border color : " + bordeBorderColor); short borderColorIndex = bordeBorderColor != null ? IndexedColors.valueOf(bordeBorderColor).getIndex() : IndexedColors.valueOf( DEFAULT_CELL_BORDER_COLOR).getIndex(); cellStyle.setLeftBorderColor(borderColorIndex); cellStyle.setRightBorderColor(borderColorIndex); cellStyle.setBottomBorderColor(borderColorIndex); cellStyle.setTopBorderColor(borderColorIndex); Font font = sheet.getWorkbook().createFont(); Short cellFontSize = (Short) this.getProperty(PROPERTY_CELL_FONT_SIZE); logger.debug("Cell font size : " + cellFontSize); short cellFontSizeShort = cellFontSize != null ? cellFontSize.shortValue() : DEFAULT_CELL_FONT_SIZE; font.setFontHeightInPoints(cellFontSizeShort); String fontName = (String) this.getProperty(PROPERTY_FONT_NAME); logger.debug("Font name : " + fontName); fontName = fontName != null ? fontName : DEFAULT_FONT_NAME; font.setFontName(fontName); String cellColor = (String) this.getProperty(PROPERTY_CELL_COLOR); logger.debug("Cell color : " + cellColor); short cellColorIndex = cellColor != null ? IndexedColors.valueOf(cellColor).getIndex() : IndexedColors.valueOf(DEFAULT_CELL_COLOR).getIndex(); font.setColor(cellColorIndex); cellStyle.setFont(font); return cellStyle; }