Java Code Examples for org.apache.poi.ss.usermodel.Font#setColor()

The following examples show how to use org.apache.poi.ss.usermodel.Font#setColor() . 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: XLSXResponseWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
SerialWriteWorkbook() {
  this.swb = new SXSSFWorkbook(100);
  this.sh = this.swb.createSheet();

  this.rowIndex = 0;

  this.headerStyle = (XSSFCellStyle)swb.createCellStyle();
  this.headerStyle.setFillBackgroundColor(IndexedColors.BLACK.getIndex());
  //solid fill
  this.headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  Font headerFont = swb.createFont();
  headerFont.setFontHeightInPoints((short)14);
  headerFont.setBold(true);
  headerFont.setColor(IndexedColors.WHITE.getIndex());
  this.headerStyle.setFont(headerFont);
}
 
Example 2
Source File: ExportEventsImpl.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
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 );
    }
}
 
Example 3
Source File: JU_FontReport.java    From hy.common.report with Apache License 2.0 6 votes vote down vote up
@Test
public void test_FontOne() throws IOException
{
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("sheet 01");
    Row row = sheet.createRow(1);
    Font font = wb.createFont();
    font.setBold(true);
    font.setColor((short) 13);
    font.setFontHeightInPoints((short) 24);
    font.setFontName("宋体");
    CellStyle cellStyle = wb.createCellStyle();
    cellStyle.setFont(font);
    Cell cell = row.createCell(1);
    cell.setCellValue("这是测试字体格式的");
    cell.setCellStyle(cellStyle);
    FileOutputStream fileOutputStream = new FileOutputStream("D://font.xlsx");
    wb.write(fileOutputStream);
    wb.close();
}
 
Example 4
Source File: ExcelUtil.java    From RuoYi-Vue with MIT License 5 votes vote down vote up
/**
 * 创建表格样式
 * 
 * @param wb 工作薄对象
 * @return 样式列表
 */
private Map<String, CellStyle> createStyles(Workbook wb)
{
    // 写入各条记录,每条记录对应excel表中的一行
    Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
    CellStyle style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setBorderRight(BorderStyle.THIN);
    style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setBorderLeft(BorderStyle.THIN);
    style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setBorderTop(BorderStyle.THIN);
    style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setBorderBottom(BorderStyle.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(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    Font headerFont = wb.createFont();
    headerFont.setFontName("Arial");
    headerFont.setFontHeightInPoints((short) 10);
    headerFont.setBold(true);
    headerFont.setColor(IndexedColors.WHITE.getIndex());
    style.setFont(headerFont);
    styles.put("header", style);

    return styles;
}
 
Example 5
Source File: CrosstabXLSExporter.java    From Knowage-Server with GNU Affero General Public License v3.0 5 votes vote down vote up
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 6
Source File: LinkDefaultCellStyle.java    From myexcel with Apache License 2.0 5 votes vote down vote up
@Override
public CellStyle supply(Workbook workbook) {
    CellStyle linkStyle = workbook.createCellStyle();
    Font linkFont = workbook.createFont();
    linkFont.setUnderline(Font.U_SINGLE);
    linkFont.setColor(IndexedColors.BLUE.getIndex());
    linkStyle.setFont(linkFont);
    linkStyle.setAlignment(HorizontalAlignment.CENTER);
    linkStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    linkStyle.setBorderBottom(BorderStyle.THIN);
    linkStyle.setBorderRight(BorderStyle.THIN);
    linkStyle.setBorderLeft(BorderStyle.THIN);
    linkStyle.setBorderTop(BorderStyle.THIN);
    return linkStyle;
}
 
Example 7
Source File: FontStyle.java    From myexcel with Apache License 2.0 5 votes vote down vote up
private static Font setFontColor(Font font, CustomColor customColor, String fontColor) {
    Short colorPredefined = ColorUtil.getPredefinedColorIndex(fontColor);
    if (colorPredefined != null) {
        font.setColor(colorPredefined);
        return font;
    }
    int[] rgb = ColorUtil.getRGBByColor(fontColor);
    if (rgb == null) {
        return null;
    }
    if (customColor.isXls()) {
        short index = ColorUtil.getCustomColorIndex(customColor, rgb);
        font.setColor(index);
    } else {
        ((XSSFFont) font).setColor(new XSSFColor(new Color(rgb[0], rgb[1], rgb[2]), customColor.getDefaultIndexedColorMap()));
    }
    return font;
}
 
Example 8
Source File: ExcelStyle.java    From objectlabkit with Apache License 2.0 5 votes vote down vote up
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 9
Source File: GridStyleBuilder.java    From bdf3 with Apache License 2.0 5 votes vote down vote up
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(CellStyle.SOLID_FOREGROUND);
	dataStyle1.setFillForegroundColor((short) 11);
	dataStyle1.setFont(dataFont);
	dataStyle1.setVerticalAlignment(CellStyle.ALIGN_CENTER);
	dataStyle1.setAlignment(CellStyle.ALIGN_LEFT);
	dataStyle1.setIndention(Short.valueOf(String.valueOf((s))));
	return dataStyle1;
}
 
Example 10
Source File: SpreadsheetExporter.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
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 11
Source File: ExcelUtils.java    From mySpringBoot with Apache License 2.0 5 votes vote down vote up
/**
 * 设置内容
 *
 * @param wb
 * @param sheet
 * @param rows
 * @param rowIndex
 * @return
 */
private static int writeRowsToExcel(XSSFWorkbook wb, Sheet sheet, List<List<Object>> rows, int rowIndex) {
    int colIndex;
    Font dataFont = wb.createFont();
    dataFont.setFontName("simsun");
    dataFont.setFontHeightInPoints((short) 14);
    dataFont.setColor(IndexedColors.BLACK.index);

    XSSFCellStyle dataStyle = wb.createCellStyle();
    dataStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
    dataStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
    dataStyle.setFont(dataFont);
    setBorder(dataStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0)));
    for (List<Object> rowData : rows) {
        Row dataRow = sheet.createRow(rowIndex);
        dataRow.setHeightInPoints(25);
        colIndex = 0;
        for (Object cellData : rowData) {
            Cell cell = dataRow.createCell(colIndex);
            if (cellData != null) {
                cell.setCellValue(cellData.toString());
            } else {
                cell.setCellValue("");
            }
            cell.setCellStyle(dataStyle);
            colIndex++;
        }
        rowIndex++;
    }
    return rowIndex;
}
 
Example 12
Source File: ExcelWriter.java    From excel-boot with Artistic License 2.0 5 votes vote down vote up
public CellStyle getHeaderCellStyle(SXSSFWorkbook workbook) {
    if (headCellStyle == null) {
        headCellStyle = workbook.getXSSFWorkbook().createCellStyle();
        headCellStyle.setBorderTop(BorderStyle.NONE);
        headCellStyle.setBorderRight(BorderStyle.NONE);
        headCellStyle.setBorderBottom(BorderStyle.NONE);
        headCellStyle.setBorderLeft(BorderStyle.NONE);
        headCellStyle.setAlignment(HorizontalAlignment.CENTER);
        headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        XSSFColor color = new XSSFColor(new java.awt.Color(217, 217, 217));
        headCellStyle.setFillForegroundColor(color);
        headCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        Font font = workbook.createFont();
        font.setFontName("微软雅黑");
        font.setColor(IndexedColors.ROYAL_BLUE.index);
        font.setBold(true);
        headCellStyle.setFont(font);
        headCellStyle.setDataFormat(workbook.createDataFormat().getFormat("@"));
    }
    return headCellStyle;
}
 
Example 13
Source File: StyleUtil.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
private static void buildFont(Workbook workbook, CellStyle cellStyle, WriteFont writeFont, boolean isHead) {
    Font font = null;
    if (isHead) {
        font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)14);
        font.setBold(true);
        cellStyle.setFont(font);
    }
    if (writeFont == null) {
        return;
    }
    if (!isHead) {
        font = workbook.createFont();
        cellStyle.setFont(font);
    }
    if (writeFont.getFontName() != null) {
        font.setFontName(writeFont.getFontName());
    }
    if (writeFont.getFontHeightInPoints() != null) {
        font.setFontHeightInPoints(writeFont.getFontHeightInPoints());
    }
    if (writeFont.getItalic() != null) {
        font.setItalic(writeFont.getItalic());
    }
    if (writeFont.getStrikeout() != null) {
        font.setStrikeout(writeFont.getStrikeout());
    }
    if (writeFont.getColor() != null) {
        font.setColor(writeFont.getColor());
    }
    if (writeFont.getTypeOffset() != null) {
        font.setTypeOffset(writeFont.getTypeOffset());
    }
    if (writeFont.getUnderline() != null) {
        font.setUnderline(writeFont.getUnderline());
    }
    if (writeFont.getCharset() != null) {
        font.setCharSet(writeFont.getCharset());
    }
    if (writeFont.getBold() != null) {
        font.setBold(writeFont.getBold());
    }
}
 
Example 14
Source File: ExcelImportServer.java    From autopoi with Apache License 2.0 4 votes vote down vote up
private void createErrorCellStyle(Workbook workbook) {
	errorCellStyle = workbook.createCellStyle();
	Font font = workbook.createFont();
	font.setColor(Font.COLOR_RED);
	errorCellStyle.setFont(font);
}
 
Example 15
Source File: ExcelExport.java    From frpMgr with MIT License 4 votes vote down vote up
/**
	 * 创建表格样式
	 * @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;
	}
 
Example 16
Source File: QbeXLSExporter.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public CellStyle buildHeaderCellStyle(Sheet sheet) {

		CellStyle cellStyle = sheet.getWorkbook().createCellStyle();
		cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
		cellStyle.setVerticalAlignment(CellStyle.ALIGN_CENTER);

		String headerBGColor = (String) this.getProperty(PROPERTY_HEADER_BACKGROUND_COLOR);
		logger.debug("Header background color : " + headerBGColor);
		short backgroundColorIndex = headerBGColor != null ? IndexedColors.valueOf(headerBGColor).getIndex()
				: IndexedColors.valueOf(DEFAULT_HEADER_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 headerColor = (String) this.getProperty(PROPERTY_HEADER_COLOR);
		logger.debug("Header color : " + headerColor);
		short headerColorIndex = bordeBorderColor != null ? IndexedColors.valueOf(headerColor).getIndex()
				: IndexedColors.valueOf(DEFAULT_HEADER_COLOR).getIndex();
		font.setColor(headerColorIndex);

		font.setBoldweight(Font.BOLDWEIGHT_BOLD);
		cellStyle.setFont(font);
		return cellStyle;
	}
 
Example 17
Source File: CalendarDemo.java    From poi with Apache License 2.0 4 votes vote down vote up
/**
	 * cell styles used for formatting calendar sheets
	 */
	private static Map<String, CellStyle> createStyles(Workbook wb){
		Map<String, CellStyle> styles = new HashMap<>();

		short borderColor = IndexedColors.GREY_50_PERCENT.getIndex();

		CellStyle style;
		Font titleFont = wb.createFont();
		titleFont.setFontHeightInPoints((short)48);
		titleFont.setColor(IndexedColors.DARK_BLUE.getIndex());
		style = wb.createCellStyle();
//		style.setAlignment(HorizontalAlignment.CENTER);
//		style.setVerticalAlignment(VerticalAlignment.CENTER);
		style.setFont(titleFont);
		styles.put("title", style);

		Font monthFont = wb.createFont();
		monthFont.setFontHeightInPoints((short)12);
		monthFont.setColor(IndexedColors.WHITE.getIndex());
//		monthFont.setBold(true);
		style = wb.createCellStyle();
//		style.setAlignment(HorizontalAlignment.CENTER);
//		style.setVerticalAlignment(VerticalAlignment.CENTER);
		style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
//		style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		style.setFont(monthFont);
		styles.put("month", style);

		Font dayFont = wb.createFont();
		dayFont.setFontHeightInPoints((short)14);
//		dayFont.setBold(true);
		style = wb.createCellStyle();
//		style.setAlignment(HorizontalAlignment.LEFT);
//		style.setVerticalAlignment(VerticalAlignment.TOP);
		style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
//		style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//		style.setBorderLeft(BorderStyle.THIN);
		style.setLeftBorderColor(borderColor);
//		style.setBorderBottom(BorderStyle.THIN);
		style.setBottomBorderColor(borderColor);
		style.setFont(dayFont);
		styles.put("weekend_left", style);

		style = wb.createCellStyle();
//		style.setAlignment(HorizontalAlignment.CENTER);
//		style.setVerticalAlignment(VerticalAlignment.TOP);
		style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
//		style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//		style.setBorderRight(BorderStyle.THIN);
		style.setRightBorderColor(borderColor);
//		style.setBorderBottom(BorderStyle.THIN);
		style.setBottomBorderColor(borderColor);
		styles.put("weekend_right", style);

		style = wb.createCellStyle();
//		style.setAlignment(HorizontalAlignment.LEFT);
//		style.setVerticalAlignment(VerticalAlignment.TOP);
//		style.setBorderLeft(BorderStyle.THIN);
		style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
//		style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		style.setLeftBorderColor(borderColor);
//		style.setBorderBottom(BorderStyle.THIN);
		style.setBottomBorderColor(borderColor);
		style.setFont(dayFont);
		styles.put("workday_left", style);

		style = wb.createCellStyle();
//		style.setAlignment(HorizontalAlignment.CENTER);
//		style.setVerticalAlignment(VerticalAlignment.TOP);
		style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
//		style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//		style.setBorderRight(BorderStyle.THIN);
		style.setRightBorderColor(borderColor);
//		style.setBorderBottom(BorderStyle.THIN);
		style.setBottomBorderColor(borderColor);
		styles.put("workday_right", style);

		style = wb.createCellStyle();
//		style.setBorderLeft(BorderStyle.THIN);
		style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
//		style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//		style.setBorderBottom(BorderStyle.THIN);
		style.setBottomBorderColor(borderColor);
		styles.put("grey_left", style);

		style = wb.createCellStyle();
		style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
//		style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//		style.setBorderRight(BorderStyle.THIN);
		style.setRightBorderColor(borderColor);
//		style.setBorderBottom(BorderStyle.THIN);
		style.setBottomBorderColor(borderColor);
		styles.put("grey_right", style);

		return styles;
	}
 
Example 18
Source File: Util.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
private static void copyCell(Cell oldCell, Cell newCell, List<CellStyle> styleList) {
	if (styleList != null) {
		if (oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()) {
			newCell.setCellStyle(oldCell.getCellStyle());
		} else {
			DataFormat newDataFormat = newCell.getSheet().getWorkbook().createDataFormat();

			CellStyle newCellStyle = getSameCellStyle(oldCell, newCell, styleList);
			if (newCellStyle == null) {

				Font oldFont = oldCell.getSheet().getWorkbook().getFontAt(oldCell.getCellStyle().getFontIndex());

				Font newFont = newCell.getSheet().getWorkbook().findFont(oldFont.getBoldweight(), oldFont.getColor(), oldFont.getFontHeight(),
						oldFont.getFontName(), oldFont.getItalic(), oldFont.getStrikeout(), oldFont.getTypeOffset(), oldFont.getUnderline());
				if (newFont == null) {
					newFont = newCell.getSheet().getWorkbook().createFont();
					newFont.setBoldweight(oldFont.getBoldweight());
					newFont.setColor(oldFont.getColor());
					newFont.setFontHeight(oldFont.getFontHeight());
					newFont.setFontName(oldFont.getFontName());
					newFont.setItalic(oldFont.getItalic());
					newFont.setStrikeout(oldFont.getStrikeout());
					newFont.setTypeOffset(oldFont.getTypeOffset());
					newFont.setUnderline(oldFont.getUnderline());
					newFont.setCharSet(oldFont.getCharSet());
				}

				short newFormat = newDataFormat.getFormat(oldCell.getCellStyle().getDataFormatString());
				newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
				newCellStyle.setFont(newFont);
				newCellStyle.setDataFormat(newFormat);

				newCellStyle.setAlignment(oldCell.getCellStyle().getAlignment());
				newCellStyle.setHidden(oldCell.getCellStyle().getHidden());
				newCellStyle.setLocked(oldCell.getCellStyle().getLocked());
				newCellStyle.setWrapText(oldCell.getCellStyle().getWrapText());
				newCellStyle.setBorderBottom(oldCell.getCellStyle().getBorderBottom());
				newCellStyle.setBorderLeft(oldCell.getCellStyle().getBorderLeft());
				newCellStyle.setBorderRight(oldCell.getCellStyle().getBorderRight());
				newCellStyle.setBorderTop(oldCell.getCellStyle().getBorderTop());
				newCellStyle.setBottomBorderColor(oldCell.getCellStyle().getBottomBorderColor());
				newCellStyle.setFillBackgroundColor(oldCell.getCellStyle().getFillBackgroundColor());
				newCellStyle.setFillForegroundColor(oldCell.getCellStyle().getFillForegroundColor());
				newCellStyle.setFillPattern(oldCell.getCellStyle().getFillPattern());
				newCellStyle.setIndention(oldCell.getCellStyle().getIndention());
				newCellStyle.setLeftBorderColor(oldCell.getCellStyle().getLeftBorderColor());
				newCellStyle.setRightBorderColor(oldCell.getCellStyle().getRightBorderColor());
				newCellStyle.setRotation(oldCell.getCellStyle().getRotation());
				newCellStyle.setTopBorderColor(oldCell.getCellStyle().getTopBorderColor());
				newCellStyle.setVerticalAlignment(oldCell.getCellStyle().getVerticalAlignment());

				styleList.add(newCellStyle);
			}
			newCell.setCellStyle(newCellStyle);
		}
	}
	switch (oldCell.getCellType()) {
	case Cell.CELL_TYPE_STRING:
		newCell.setCellValue(oldCell.getStringCellValue());
		break;
	case Cell.CELL_TYPE_NUMERIC:
		newCell.setCellValue(oldCell.getNumericCellValue());
		break;
	case Cell.CELL_TYPE_BLANK:
		newCell.setCellType(Cell.CELL_TYPE_BLANK);
		break;
	case Cell.CELL_TYPE_BOOLEAN:
		newCell.setCellValue(oldCell.getBooleanCellValue());
		break;
	case Cell.CELL_TYPE_ERROR:
		newCell.setCellErrorValue(oldCell.getErrorCellValue());
		break;
	case Cell.CELL_TYPE_FORMULA:
		newCell.setCellFormula(oldCell.getCellFormula());
		formulaInfoList.add(new FormulaInfo(oldCell.getSheet().getSheetName(), oldCell.getRowIndex(), oldCell.getColumnIndex(), oldCell.getCellFormula()));
		break;
	default:
		break;
	}
}
 
Example 19
Source File: ExcelImportServer.java    From easypoi with Apache License 2.0 4 votes vote down vote up
private void createErrorCellStyle(Workbook workbook) {
    errorCellStyle = workbook.createCellStyle();
    Font font = workbook.createFont();
    font.setColor(Font.COLOR_RED);
    errorCellStyle.setFont(font);
}
 
Example 20
Source File: HRExcelBuilder.java    From Spring-MVC-Blueprints with MIT License 4 votes vote down vote up
@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());
          
       }
       
}