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

The following examples show how to use org.apache.poi.ss.usermodel.CellStyle#setWrapText() . 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 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 2
Source File: ExcelExportStylerColorImpl.java    From autopoi 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 3
Source File: AbstractSheet.java    From tools with Apache License 2.0 5 votes vote down vote up
public static CellStyle createLeftWrapStyle(Workbook wb) {
	CellStyle wrapStyle = wb.createCellStyle();
	wrapStyle.setWrapText(true);
	wrapStyle.setAlignment(HorizontalAlignment.LEFT);
	wrapStyle.setVerticalAlignment(VerticalAlignment.CENTER);
	return wrapStyle;
}
 
Example 4
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 5
Source File: StyleUtil.java    From easyexcel with Apache License 2.0 5 votes vote down vote up
/**
 * @param workbook
 * @return
 */
public static CellStyle buildDefaultCellStyle(Workbook workbook) {
    CellStyle newCellStyle = workbook.createCellStyle();
    newCellStyle.setWrapText(true);
    newCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    newCellStyle.setAlignment(HorizontalAlignment.CENTER);
    newCellStyle.setLocked(true);
    newCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    newCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    newCellStyle.setBorderTop(BorderStyle.THIN);
    newCellStyle.setBorderBottom(BorderStyle.THIN);
    newCellStyle.setBorderLeft(BorderStyle.THIN);
    newCellStyle.setBorderRight(BorderStyle.THIN);
    return newCellStyle;
}
 
Example 6
Source File: ExcelExportStylerColorImpl.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 7
Source File: ExcelExportStylerColorImpl.java    From jeasypoi 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: ExcelExportStylerColorImpl.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.setFillForegroundColor(color); // 填充的背景颜色
	titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
	titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
	titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); // 填充图案
	titleStyle.setWrapText(true);
	return titleStyle;
}
 
Example 9
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 10
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 11
Source File: ExcelUtil.java    From danyuan-application with Apache License 2.0 5 votes vote down vote up
/**
 * 复制一个单元格样式到目的单元格样式
 *
 * @param fromStyle
 * @param toStyle
 */
public static void copyCellStyle(CellStyle fromStyle, CellStyle toStyle) {
	toStyle.setAlignment(fromStyle.getAlignmentEnum());
	// 边框和边框颜色
	toStyle.setBorderBottom(fromStyle.getBorderBottomEnum());
	toStyle.setBorderLeft(fromStyle.getBorderLeftEnum());
	toStyle.setBorderRight(fromStyle.getBorderRightEnum());
	toStyle.setBorderTop(fromStyle.getBorderTopEnum());
	toStyle.setTopBorderColor(fromStyle.getTopBorderColor());
	toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor());
	toStyle.setRightBorderColor(fromStyle.getRightBorderColor());
	toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor());
	
	// 背景和前景
	toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor());
	toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor());
	
	// 数据格式
	toStyle.setDataFormat(fromStyle.getDataFormat());
	toStyle.setFillPattern(fromStyle.getFillPatternEnum());
	// toStyle.setFont(fromStyle.getFont(null));
	toStyle.setHidden(fromStyle.getHidden());
	toStyle.setIndention(fromStyle.getIndention());// 首行缩进
	toStyle.setLocked(fromStyle.getLocked());
	toStyle.setRotation(fromStyle.getRotation());// 旋转
	toStyle.setVerticalAlignment(fromStyle.getVerticalAlignmentEnum());
	toStyle.setWrapText(fromStyle.getWrapText());
	
}
 
Example 12
Source File: ExcelExportStylerBorderImpl.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.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: 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 14
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(FillPatternType.SOLID_FOREGROUND);
	Font headerFont = wb.createFont();
	headerFont.setFontName("Arial");
	headerFont.setFontHeight(FONT_SIZE);
	headerFont.setBold(true);
	headerStyle.setFont(headerFont);
	headerStyle.setAlignment(HorizontalAlignment.CENTER);
	headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
	headerStyle.setWrapText(true);
	return headerStyle;
}
 
Example 15
Source File: ExportTimetableXLS.java    From unitime with Apache License 2.0 5 votes vote down vote up
protected CellStyle getMeetingFooterStyle(P p, P parent, Color bgColor) {
	if (bgColor == null) bgColor = Color.WHITE;
	String styleId = "meeting-footer-" + Integer.toHexString(bgColor.getRGB());
	CellStyle style = iStyles.get(styleId);
	if (style == null) {
		style = iWorkbook.createCellStyle();
        style.setBorderBottom(BorderStyle.THICK);
        style.setBottomBorderColor(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.WHITE.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: ExcelExportStylerDefaultImpl.java    From easypoi 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 17
Source File: UKExcelUtil.java    From youkefu with Apache License 2.0 4 votes vote down vote up
/**
 * 构建头部
 */
@SuppressWarnings("deprecation")
private void createHead(){
	Row row = sheet.createRow(rowNum); 

	// 设置第一行 
	Cell cell = row.createCell(0); 
	row.setHeight((short) 1100); 

	// 定义单元格为字符串类型 
	cell.setCellType(HSSFCell.ENCODING_UTF_16); 
	cell.setCellValue(new XSSFRichTextString(this.headTitle)); 

	// 指定合并区域 
	if(rowNum>0) {
		sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum,0,(cellNumber-1))); 
	}

	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) 400); 
	cellStyle.setFont(font); 
	cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
	cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
	cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
	cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
	cell.setCellStyle(cellStyle); 
	for(int i=1;i<this.cellNumber;i++){
		Cell cell3= row.createCell(i); 
		cell3.setCellStyle(cellStyle);
	}
	rowNum ++;
}
 
Example 18
Source File: XLSPrinter.java    From unitime with Apache License 2.0 4 votes vote down vote up
public XLSPrinter(OutputStream output, boolean checkLast) {
	iOutput = output;
	iCheckLast = checkLast;
	iWorkbook = new HSSFWorkbook();
	iSheet = iWorkbook.createSheet();
	iSheet.setDisplayGridlines(false);
	iSheet.setPrintGridlines(false);
	iSheet.setFitToPage(true);
	iSheet.setHorizontallyCenter(true);
       PrintSetup printSetup = iSheet.getPrintSetup();
       printSetup.setLandscape(true);
       iSheet.setAutobreaks(true);
       printSetup.setFitHeight((short)1);
       printSetup.setFitWidth((short)1);
       iStyles = new HashMap<String, CellStyle>();
       
       CellStyle style;
       
       style = iWorkbook.createCellStyle();
       style.setBorderBottom(BorderStyle.THIN);
       style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
       style.setAlignment(HorizontalAlignment.LEFT);
       style.setVerticalAlignment(VerticalAlignment.TOP);
       style.setFont(getFont(true, false, false, Color.BLACK));
       style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
       style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
       style.setWrapText(true);
       iStyles.put("header", style);
       
       style = iWorkbook.createCellStyle();
       style.setAlignment(HorizontalAlignment.LEFT);
       style.setVerticalAlignment(VerticalAlignment.TOP);
       style.setFont(getFont(false, false, false, Color.BLACK));
       style.setWrapText(true);
       iStyles.put("plain", style);
       
       style = iWorkbook.createCellStyle();
       style.setAlignment(HorizontalAlignment.RIGHT);
       style.setVerticalAlignment(VerticalAlignment.TOP);
       style.setFont(getFont(false, false, false, Color.BLACK));
       iStyles.put("number", style);
}
 
Example 19
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 20
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());
    }
}