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

The following examples show how to use org.apache.poi.ss.usermodel.CellStyle#setVerticalAlignment() . 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: WorkbookContext.java    From Octopus with MIT License 6 votes vote down vote up
public CellStyle getCellStyle(Field field) {
    CellStyle style = cellStyleMap.get(field);
    if (style == null) {
        style = book.createCellStyle();
        Font font = book.createFont();
        FieldCellStyle fieldCellStyle = field.getFieldCellStyle();


        font.setFontHeightInPoints(fieldCellStyle.getFontSize());
        font.setBold(fieldCellStyle.isBold());
        ColorUtils.setColor(book, font, fieldCellStyle.getColor());
        style.setFont(font);
        ColorUtils.setForegroundColor(book, style, fieldCellStyle.getForegroundColor());

        setStyleBorder(style, fieldCellStyle.getBorder());
        ColorUtils.setBorderColor(book, style, fieldCellStyle.getBorderColor());

        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyleMap.put(field, style);
        return style;
    }
    return style;
}
 
Example 2
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 3
Source File: Over23CandidacyProcessDA.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected void exportXLSLine(final Sheet sheet, final CellStyle cellStyle, final List<Object> cells,
        final int offset) {

    final org.apache.poi.ss.usermodel.Row row = sheet.createRow(sheet.getLastRowNum() + offset);
    cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

    int count = 0;
    for (final Object cellValue : cells) {
        if (++count == 3) {
            cellStyle.setAlignment(HorizontalAlignment.LEFT);
        } else {
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
        }
        addColumn(cellStyle, row, cellValue);
    }
}
 
Example 4
Source File: XLSXWriter.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * create a library of cell styles
 */
private static Map<String, CellStyle> createStyles(Workbook wb){
  Map<String, CellStyle> styles = new HashMap<>();

  CellStyle style;
  Font headerFont = wb.createFont();
  headerFont.setBold(true);
  style = createBorderedStyle(wb);
  style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
  style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  style.setVerticalAlignment(VerticalAlignment.TOP);
  style.setWrapText(true);
  style.setFont(headerFont);
  styles.put("header", style);

  style = createBorderedStyle(wb);
  style.setVerticalAlignment(VerticalAlignment.TOP);
  style.setWrapText(true);    
  styles.put("body", style);

  return styles;
}
 
Example 5
Source File: ExcelExportStylerColorImpl.java    From jeasypoi with Apache License 2.0 6 votes vote down vote up
@Override
public CellStyle stringSeptailStyle(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.setFillForegroundColor((short) 41); // 填充的背景颜色
	style.setFillPattern(CellStyle.SOLID_FOREGROUND); // 填充图案
	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: 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 7
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 8
Source File: ExcelExportStylerDefaultImpl.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.setAlignment(CellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    style.setDataFormat(STRING_FORMAT);
    if (isWarp) {
        style.setWrapText(true);
    }
    return style;
}
 
Example 9
Source File: ExcelExportStylerDefaultImpl.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.setAlignment(CellStyle.ALIGN_CENTER);
    titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    return titleStyle;
}
 
Example 10
Source File: TdDefaultCellStyle.java    From myexcel with Apache License 2.0 5 votes vote down vote up
@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.setBorderLeft(BorderStyle.THIN);
    style.setBorderTop(BorderStyle.THIN);
    return style;
}
 
Example 11
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 12
Source File: ExcelExportStylerBorderImpl.java    From autopoi 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 13
Source File: ExcelExportStylerBorderImpl.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.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 14
Source File: ExcelExportStylerDefaultImpl.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.setAlignment(CellStyle.ALIGN_CENTER);
	titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
	titleStyle.setWrapText(true);
	return titleStyle;
}
 
Example 15
Source File: ExportTimetableXLS.java    From unitime with Apache License 2.0 5 votes vote down vote up
protected CellStyle getMeetingHeaderStyle(P p, P parent, Color bgColor) {
	if (bgColor == null) bgColor = Color.WHITE;
	String styleId = "meeting-header-" + Integer.toHexString(bgColor.getRGB()) + (p.iItalics ? "-italics" : "");
	CellStyle style = iStyles.get(styleId);
	if (style == null) {
		style = iWorkbook.createCellStyle();
        style.setBorderTop(BorderStyle.THICK);
        style.setTopBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderLeft(BorderStyle.THICK);
        style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
        style.setBorderRight(BorderStyle.THICK);
        style.setRightBorderColor(IndexedColors.BLACK.getIndex());
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.TOP);
        style.setFont(getFont(parent));
        String colorId = Integer.toHexString(bgColor.getRGB());
		Short color = iColors.get(colorId);
		if (color == null) {
			HSSFPalette palette = ((HSSFWorkbook)iWorkbook).getCustomPalette();
			HSSFColor clr = palette.findSimilarColor(bgColor.getRed(), bgColor.getGreen(), bgColor.getBlue());
			color = (clr == null ? IndexedColors.BLACK.getIndex() : clr.getIndex());
			iColors.put(colorId, color);
		}
        style.setFillForegroundColor(color);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setWrapText(true);
        iStyles.put(styleId, style);
	}
	return style;
}
 
Example 16
Source File: ExcelUtil.java    From supplierShop 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 17
Source File: DataSetExportServicesImpl.java    From dashbuilder with Apache License 2.0 4 votes vote down vote up
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 18
Source File: ExcelUtil.java    From LuckyFrameWeb with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * 填充excel数据
 * @param index 序号
 *
 */
public void fillExcelData(int index)
{
    int startNo = index * sheetSize;
    int endNo = Math.min(startNo + sheetSize, list.size());
    // 写入各条记录,每条记录对应excel表中的一行
    CellStyle cs = wb.createCellStyle();
    cs.setAlignment(HorizontalAlignment.CENTER);
    cs.setVerticalAlignment(VerticalAlignment.CENTER);
    for (int i = startNo; i < endNo; i++)
    {
        Row row = sheet.createRow(i + 1 - startNo);
        // 得到导出对象.
        T vo = list.get(i);
        for (int j = 0; j < fields.size(); j++)
        {
            // 获得field.
            Field field = fields.get(j);
            // 设置实体类私有属性可访问
            field.setAccessible(true);
            Excel attr = field.getAnnotation(Excel.class);
            try
            {
                // 设置行高
                row.setHeight((short) (attr.height() * 20));
                // 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
                if (attr.isExport())
                {
                    // 创建cell
                    Cell cell = row.createCell(j);
                    cell.setCellStyle(cs);
                    if (vo == null)
                    {
                        // 如果数据存在就填入,不存在填入空格.
                        cell.setCellValue("");
                        continue;
                    }

                    // 用于读取对象中的属性
                    Object value = getTargetValue(vo, field, attr);
                    String dateFormat = attr.dateFormat();
                    String readConverterExp = attr.readConverterExp();
                    if (StringUtils.isNotEmpty(dateFormat))
                    {
                        cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
                    }
                    else if (StringUtils.isNotEmpty(readConverterExp))
                    {
                        cell.setCellValue(convertByExp(String.valueOf(value), readConverterExp));
                    }
                    else
                    {
                        cell.setCellType(CellType.STRING);
                        // 如果数据存在就填入,不存在填入空格.
                        cell.setCellValue(StringUtils.isNull(value) ? attr.defaultValue() : value + attr.suffix());
                    }
                }
            }
            catch (Exception e)
            {
                log.error("导出Excel失败{}", e.getMessage());
            }
        }
    }
}
 
Example 19
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 20
Source File: AlignVertical.java    From excel-io with MIT License 4 votes vote down vote up
@Override
public void accept(final CellStyle style) {
    style.setVerticalAlignment(this.value);
}