Java Code Examples for org.apache.poi.ss.usermodel.CellStyle#setAlignment()

The following examples show how to use org.apache.poi.ss.usermodel.CellStyle#setAlignment() . 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: ExcelExportStylerColorImpl.java    From autopoi with Apache License 2.0 8 votes vote down vote up
@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 2
Source File: ExcelExportServer.java    From jeasypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 创建 表头改变
 * 
 * @param entity
 * @param sheet
 * @param workbook
 * @param feildWidth
 */
public int createHeaderRow(ExportParams entity, Sheet sheet, Workbook workbook, int feildWidth) {
	Row row = sheet.createRow(0);
	row.setHeight(entity.getTitleHeight());
	createStringCell(row, 0, entity.getTitle(), getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
	for (int i = 1; i <= feildWidth; i++) {
		createStringCell(row, i, "", getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
	}
	sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, feildWidth));
	if (entity.getSecondTitle() != null) {
		row = sheet.createRow(1);
		row.setHeight(entity.getSecondTitleHeight());
		CellStyle style = workbook.createCellStyle();
		style.setAlignment(CellStyle.ALIGN_RIGHT);
		createStringCell(row, 0, entity.getSecondTitle(), style, null);
		for (int i = 1; i <= feildWidth; i++) {
			createStringCell(row, i, "", getExcelExportStyler().getHeaderStyle(entity.getHeaderColor()), null);
		}
		sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, feildWidth));
		return 2;
	}
	return 1;
}
 
Example 3
Source File: ExportTimetableXLS.java    From unitime with Apache License 2.0 6 votes vote down vote up
protected CellStyle getGridNameStyle(P p) {
	String styleId = "grid-name";
	CellStyle style = iStyles.get(styleId);
	if (style == null) {
		style = iWorkbook.createCellStyle();
        style.setBorderLeft(BorderStyle.THIN);
        style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setBorderTop(BorderStyle.THIN);
        style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex());
        style.setFont(getFont(p));
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.TOP);
        style.setWrapText(true);
        iStyles.put(styleId, style);
	}
	return style;
}
 
Example 4
Source File: ExcelExportStylerDefaultImpl.java    From jeasypoi with Apache License 2.0 5 votes vote down vote up
@Override
public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {
	CellStyle style = workbook.createCellStyle();
	style.setAlignment(CellStyle.ALIGN_CENTER);
	style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
	style.setDataFormat(STRING_FORMAT);
	if (isWarp) {
		style.setWrapText(true);
	}
	return style;
}
 
Example 5
Source File: ExcelExportStylerDefaultImpl.java    From autopoi with Apache License 2.0 5 votes vote down vote up
@Override
public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
	CellStyle style = workbook.createCellStyle();
	style.setAlignment(CellStyle.ALIGN_CENTER);
	style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
	style.setDataFormat(STRING_FORMAT);
	if (isWarp) {
		style.setWrapText(true);
	}
	return style;
}
 
Example 6
Source File: ExcelExportStylerBorderImpl.java    From easypoi with Apache License 2.0 5 votes vote down vote up
@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 7
Source File: ExcelExportStylerBorderImpl.java    From easypoi with Apache License 2.0 5 votes vote down vote up
@Override
public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
    CellStyle style = workbook.createCellStyle();
    style.setBorderLeft((short) 1); // 左边框
    style.setBorderRight((short) 1); // 右边框
    style.setBorderBottom((short) 1);
    style.setBorderTop((short) 1);
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setDataFormat(STRING_FORMAT);
    if (isWarp) {
        style.setWrapText(true);
    }
    return style;
}
 
Example 8
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 9
Source File: ExcelExportStylerDefaultImpl.java    From easypoi with Apache License 2.0 5 votes vote down vote up
@Override
public CellStyle getTitleStyle(short color) {
    CellStyle titleStyle = workbook.createCellStyle();
    titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
    titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    titleStyle.setWrapText(true);
    return titleStyle;
}
 
Example 10
Source File: ExcelExportStylerBorderImpl.java    From jeasypoi with Apache License 2.0 5 votes vote down vote up
@Override
public CellStyle getTitleStyle(short color) {
	CellStyle titleStyle = workbook.createCellStyle();
	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);
	titleStyle.setWrapText(true);
	return titleStyle;
}
 
Example 11
Source File: AbstractSheet.java    From tools with Apache License 2.0 5 votes vote down vote up
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 12
Source File: ExcelExportStylerDefaultImpl.java    From jeasypoi with Apache License 2.0 5 votes vote down vote up
@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 13
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 14
Source File: AbstractSheet.java    From tools with Apache License 2.0 4 votes vote down vote up
public static CellStyle createLeftWrapStyle(Workbook wb) {
	CellStyle wrapStyle = wb.createCellStyle();
	wrapStyle.setWrapText(true);
	wrapStyle.setAlignment(CellStyle.ALIGN_LEFT);
	return wrapStyle;
}
 
Example 15
Source File: CrosstabXLSExporter.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public CellStyle buildNACellStyle(Sheet sheet) {
	CellStyle cellStyleForNA = this.buildDataCellStyle(sheet);
	cellStyleForNA.setAlignment(CellStyle.ALIGN_CENTER);
	return cellStyleForNA;
}
 
Example 16
Source File: StyleManager.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Create a new POI CellStyle based upon a BIRT style.
 * @param birtStyle
 * The BIRT style to base the CellStyle upon.
 * @return
 * The CellStyle whose attributes are described by the BIRT style. 
 */
private CellStyle createStyle( BirtStyle birtStyle ) {
	CellStyle poiStyle = workbook.createCellStyle();
	// Font
	Font font = fm.getFont(birtStyle);
	if( font != null ) {
		poiStyle.setFont(font);
	}
	// Alignment
	poiStyle.setAlignment(smu.poiAlignmentFromBirtAlignment(birtStyle.getString( StyleConstants.STYLE_TEXT_ALIGN )));
	// Background colour
	smu.addBackgroundColourToStyle(workbook, poiStyle, birtStyle.getString( StyleConstants.STYLE_BACKGROUND_COLOR ));
	// Top border 
	smu.applyBorderStyle(workbook, poiStyle, BorderSide.TOP, birtStyle.getProperty(StyleConstants.STYLE_BORDER_TOP_COLOR), birtStyle.getProperty(StyleConstants.STYLE_BORDER_TOP_STYLE), birtStyle.getProperty(StyleConstants.STYLE_BORDER_TOP_WIDTH));
	// Left border 
	smu.applyBorderStyle(workbook, poiStyle, BorderSide.LEFT, birtStyle.getProperty(StyleConstants.STYLE_BORDER_LEFT_COLOR), birtStyle.getProperty(StyleConstants.STYLE_BORDER_LEFT_STYLE), birtStyle.getProperty(StyleConstants.STYLE_BORDER_LEFT_WIDTH));
	// Right border 
	smu.applyBorderStyle(workbook, poiStyle, BorderSide.RIGHT, birtStyle.getProperty(StyleConstants.STYLE_BORDER_RIGHT_COLOR), birtStyle.getProperty(StyleConstants.STYLE_BORDER_RIGHT_STYLE), birtStyle.getProperty(StyleConstants.STYLE_BORDER_RIGHT_WIDTH));
	// Bottom border 
	smu.applyBorderStyle(workbook, poiStyle, BorderSide.BOTTOM, birtStyle.getProperty(StyleConstants.STYLE_BORDER_BOTTOM_COLOR), birtStyle.getProperty(StyleConstants.STYLE_BORDER_BOTTOM_STYLE), birtStyle.getProperty(StyleConstants.STYLE_BORDER_BOTTOM_WIDTH));
	// Number format
	smu.applyNumberFormat(workbook, birtStyle, poiStyle, locale);
	// Whitespace/wrap
	if( CSSConstants.CSS_PRE_VALUE.equals( birtStyle.getString( StyleConstants.STYLE_WHITE_SPACE ) ) ) {
		poiStyle.setWrapText( true );
	}
	// Vertical alignment
	if( CSSConstants.CSS_TOP_VALUE.equals( birtStyle.getString( StyleConstants.STYLE_VERTICAL_ALIGN ) ) ) {
		poiStyle.setVerticalAlignment( CellStyle.VERTICAL_TOP );
	} else if ( CSSConstants.CSS_MIDDLE_VALUE.equals( birtStyle.getString( StyleConstants.STYLE_VERTICAL_ALIGN ) ) ) {
		poiStyle.setVerticalAlignment( CellStyle.VERTICAL_CENTER );
	} else if ( CSSConstants.CSS_BOTTOM_VALUE.equals( birtStyle.getString( StyleConstants.STYLE_VERTICAL_ALIGN ) ) ) {
		poiStyle.setVerticalAlignment( CellStyle.VERTICAL_BOTTOM );
	} 
	// Rotation
	CSSValue rotation = birtStyle.getProperty( BirtStyle.TEXT_ROTATION );
	if( rotation instanceof FloatValue ) {
		poiStyle.setRotation( (short) ((FloatValue)rotation).getFloatValue() );
	}

	styles.add(new StylePair( birtStyle.clone(), poiStyle ) );
	return poiStyle;
}
 
Example 17
Source File: QbeXLSExporter.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
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: StyleUtil.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
private static void buildCellStyle(Workbook workbook, CellStyle cellStyle, WriteCellStyle writeCellStyle,
    boolean isHead) {
    buildFont(workbook, cellStyle, writeCellStyle.getWriteFont(), isHead);
    if (writeCellStyle.getDataFormat() != null) {
        cellStyle.setDataFormat(writeCellStyle.getDataFormat());
    }
    if (writeCellStyle.getHidden() != null) {
        cellStyle.setHidden(writeCellStyle.getHidden());
    }
    if (writeCellStyle.getLocked() != null) {
        cellStyle.setLocked(writeCellStyle.getLocked());
    }
    if (writeCellStyle.getQuotePrefix() != null) {
        cellStyle.setQuotePrefixed(writeCellStyle.getQuotePrefix());
    }
    if (writeCellStyle.getHorizontalAlignment() != null) {
        cellStyle.setAlignment(writeCellStyle.getHorizontalAlignment());
    }
    if (writeCellStyle.getWrapped() != null) {
        cellStyle.setWrapText(writeCellStyle.getWrapped());
    }
    if (writeCellStyle.getVerticalAlignment() != null) {
        cellStyle.setVerticalAlignment(writeCellStyle.getVerticalAlignment());
    }
    if (writeCellStyle.getRotation() != null) {
        cellStyle.setRotation(writeCellStyle.getRotation());
    }
    if (writeCellStyle.getIndent() != null) {
        cellStyle.setIndention(writeCellStyle.getIndent());
    }
    if (writeCellStyle.getBorderLeft() != null) {
        cellStyle.setBorderLeft(writeCellStyle.getBorderLeft());
    }
    if (writeCellStyle.getBorderRight() != null) {
        cellStyle.setBorderRight(writeCellStyle.getBorderRight());
    }
    if (writeCellStyle.getBorderTop() != null) {
        cellStyle.setBorderTop(writeCellStyle.getBorderTop());
    }
    if (writeCellStyle.getBorderBottom() != null) {
        cellStyle.setBorderBottom(writeCellStyle.getBorderBottom());
    }
    if (writeCellStyle.getLeftBorderColor() != null) {
        cellStyle.setLeftBorderColor(writeCellStyle.getLeftBorderColor());
    }
    if (writeCellStyle.getRightBorderColor() != null) {
        cellStyle.setRightBorderColor(writeCellStyle.getRightBorderColor());
    }
    if (writeCellStyle.getTopBorderColor() != null) {
        cellStyle.setTopBorderColor(writeCellStyle.getTopBorderColor());
    }
    if (writeCellStyle.getBottomBorderColor() != null) {
        cellStyle.setBottomBorderColor(writeCellStyle.getBottomBorderColor());
    }
    if (writeCellStyle.getFillPatternType() != null) {
        cellStyle.setFillPattern(writeCellStyle.getFillPatternType());
    }
    if (writeCellStyle.getFillBackgroundColor() != null) {
        cellStyle.setFillBackgroundColor(writeCellStyle.getFillBackgroundColor());
    }
    if (writeCellStyle.getFillForegroundColor() != null) {
        cellStyle.setFillForegroundColor(writeCellStyle.getFillForegroundColor());
    }
    if (writeCellStyle.getShrinkToFit() != null) {
        cellStyle.setShrinkToFit(writeCellStyle.getShrinkToFit());
    }
}
 
Example 19
Source File: ExcelView.java    From Spring-MVC-Blueprints with MIT License 4 votes vote down vote up
@Override
protected void buildExcelDocument(Map<String, Object> model,
		Workbook workbook, HttpServletRequest request,
		HttpServletResponse response) throws Exception {
	
	Sheet sheet = workbook.createSheet("sheet 1");
	
	List<HrmsLogin> users = (List<HrmsLogin>) model.get("allUsers");
	
	Row row = null;
	Cell cell = null;
	int r = 0;
	int c = 0;
	
	//Style for header cell
	CellStyle style = workbook.createCellStyle();
	style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.index);
	style.setFillPattern(CellStyle.SOLID_FOREGROUND);
	style.setAlignment(CellStyle.ALIGN_CENTER);
	
	//Create header cells
	row = sheet.createRow(r++);
	
	cell = row.createCell(c++);
	cell.setCellStyle(style);
	cell.setCellValue("Employee ID");
	
	cell = row.createCell(c++);
	cell.setCellStyle(style);
	cell.setCellValue("Username");
	
	cell = row.createCell(c++);
	cell.setCellStyle(style);
	cell.setCellValue("Password");
	
	cell = row.createCell(c++);
	cell.setCellStyle(style);
	cell.setCellValue("Role");
	
	
	//Create data cell
	for(HrmsLogin user: users){
		row = sheet.createRow(r++);
		c = 0;
		row.createCell(c++).setCellValue(user.getHrmsEmployeeDetails().getEmpId());
		row.createCell(c++).setCellValue(user.getUsername());
		row.createCell(c++).setCellValue(user.getPassword());
		row.createCell(c++).setCellValue(user.getRole());

	}
	for(int i = 0 ; i < 4; i++)
		sheet.autoSizeColumn(i, true);
	
}
 
Example 20
Source File: UKExcelUtil.java    From youkefu with Apache License 2.0 4 votes vote down vote up
/**
	 * 构建副标题
	 */
	@SuppressWarnings("deprecation")
	private void createSubHead(){
		
		CellStyle cellStyle = wb.createCellStyle(); 
		cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 指定单元格居中对齐 
		cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐 
		cellStyle.setWrapText(true);// 指定单元格自动换行 

		// 设置单元格字体 
		Font font = wb.createFont(); 
		//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); 
		font.setFontName("宋体"); 
		font.setFontHeight((short) 180); 
		cellStyle.setFont(font); 
		
		cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		
		
		CellStyle leftStyle = wb.createCellStyle(); 
		leftStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 指定单元格居中对齐 
		leftStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 指定单元格垂直居中对齐 
		leftStyle.setWrapText(true);// 指定单元格自动换行 
		leftStyle.setFont(font); 
		
		leftStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
		leftStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		leftStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
		leftStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		
		Row row1 = sheet.createRow(rowNum); 
		row1.setHeight((short) 440); 
		
		
		
		StringBuffer strb = new StringBuffer();
//		if(false){	//增加 过滤器
//			
//		}else{
//			
//		}
		strb.append("报表生成日期:").append(getNowDate()) ;
		
		Cell cell2 = row1.createCell(0); 
		cell2.setCellType(HSSFCell.ENCODING_UTF_16); 
		cell2.setCellValue(new XSSFRichTextString(strb.toString())); 
		cell2.setCellStyle(leftStyle);
		// 指定合并区域 
		if(rowNum>1) {
			sheet.addMergedRegion(new CellRangeAddress(rowNum,  rowNum, 0, (cellNumber-1)));
		}
		
		for(int i=1;i<this.cellNumber;i++){
			Cell cell3= row1.createCell(i); 
			cell3.setCellStyle(cellStyle);
		}
		rowNum ++;
	}