本文整理了Java中org.apache.poi.ss.usermodel.CellStyle
类的一些代码示例,展示了CellStyle
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。CellStyle
类的具体详情如下:
包路径:org.apache.poi.ss.usermodel.CellStyle
类名称:CellStyle
暂无
代码示例来源:origin: alibaba/easyexcel
/**
*
* @param workbook
* @return
*/
public static CellStyle buildDefaultCellStyle(Workbook workbook) {
CellStyle newCellStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short)14);
font.setBold(true);
newCellStyle.setFont(font);
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.setBorderBottom(BorderStyle.THIN);
newCellStyle.setBorderLeft(BorderStyle.THIN);
return newCellStyle;
}
代码示例来源:origin: stackoverflow.com
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("format sheet");
CellStyle style;
DataFormat format = wb.createDataFormat();
Row row;
Cell cell;
short rowNum = 0;
short colNum = 0;
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("0.0"));
cell.setCellStyle(style);
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("#,##0.0000"));
cell.setCellStyle(style);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
代码示例来源:origin: org.apache.poi/poi
/**
* Sets the format properties of the given style based on the given map.
*
* @param style cell style
* @param workbook parent workbook
* @param properties map of format properties (String -> Object)
* @see #getFormatProperties(CellStyle)
*/
private static void setFormatProperties(CellStyle style, Workbook workbook, Map<String, Object> properties) {
style.setAlignment(getHorizontalAlignment(properties, ALIGNMENT));
style.setVerticalAlignment(getVerticalAlignment(properties, VERTICAL_ALIGNMENT));
style.setBorderBottom(getBorderStyle(properties, BORDER_BOTTOM));
style.setBorderLeft(getBorderStyle(properties, BORDER_LEFT));
style.setBorderRight(getBorderStyle(properties, BORDER_RIGHT));
style.setBorderTop(getBorderStyle(properties, BORDER_TOP));
style.setBottomBorderColor(getShort(properties, BOTTOM_BORDER_COLOR));
style.setDataFormat(getShort(properties, DATA_FORMAT));
style.setFillPattern(getFillPattern(properties, FILL_PATTERN));
style.setFillForegroundColor(getShort(properties, FILL_FOREGROUND_COLOR));
style.setFillBackgroundColor(getShort(properties, FILL_BACKGROUND_COLOR));
style.setFont(workbook.getFontAt(getInt(properties, FONT)));
style.setHidden(getBoolean(properties, HIDDEN));
style.setIndention(getShort(properties, INDENTION));
style.setLeftBorderColor(getShort(properties, LEFT_BORDER_COLOR));
style.setLocked(getBoolean(properties, LOCKED));
style.setRightBorderColor(getShort(properties, RIGHT_BORDER_COLOR));
style.setRotation(getShort(properties, ROTATION));
style.setTopBorderColor(getShort(properties, TOP_BORDER_COLOR));
style.setWrapText(getBoolean(properties, WRAP_TEXT));
}
代码示例来源:origin: looly/hutool
/**
* 给cell设置颜色
*
* @param cellStyle {@link CellStyle}
* @param color 背景颜色
* @param fillPattern 填充方式 {@link FillPatternType}枚举
* @return {@link CellStyle}
*/
public static CellStyle setColor(CellStyle cellStyle, short color, FillPatternType fillPattern) {
cellStyle.setFillForegroundColor(color);
cellStyle.setFillPattern(fillPattern);
return cellStyle;
}
代码示例来源:origin: looly/hutool
/**
* 设置cell文本对齐样式
*
* @param cellStyle {@link CellStyle}
* @param halign 横向位置
* @param valign 纵向位置
* @return {@link CellStyle}
*/
public static CellStyle setAlign(CellStyle cellStyle, HorizontalAlignment halign, VerticalAlignment valign) {
cellStyle.setAlignment(halign);
cellStyle.setVerticalAlignment(valign);
return cellStyle;
}
代码示例来源:origin: stackoverflow.com
// Aqua background
CellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.BIG_SPOTS);
row.setRowStyle(style);
代码示例来源:origin: com.namics.oss.java.tools/java-tools
protected Map<Integer, String> writeHeaderRow(final Sheet sheet, final Map<String, Object> map, final Map<String, String> mapping) {
LOG.debug("Create header for {}", map);
Map<Integer, String> result = new HashMap<>();
Row row = sheet.createRow(this.headerRow);
Set<String> keys = map.keySet();
int index = 0;
for (String key : keys) {
if (mapping.containsKey(key)) {
result.put(index, key);
Cell cell = row.createCell(index);
cell.setCellValue(new XSSFRichTextString(mapping.get(key)));
CellStyle keyStyle = sheet.getWorkbook().createCellStyle();
Font f = sheet.getWorkbook().createFont();
f.setBold(false);
keyStyle.setFont(f);
cell.setCellStyle(keyStyle);
index++;
}
}
return result;
}
代码示例来源:origin: cn.afterturn/easypoi-base
@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(HorizontalAlignment.CENTER);
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
return titleStyle;
}
代码示例来源:origin: pentaho/pentaho-kettle
boolean cellExisted = true;
Cell cell = xlsRow.getCell( posX );
if ( cell == null ) {
cellExisted = false;
cell = xlsRow.createCell( posX );
cell.setCellStyle( data.getCachedStyle( fieldNr ) );
} else {
Cell styleCell = getCellFromReference( styleRef );
if ( styleCell != null && cell != styleCell ) {
cell.setCellStyle( styleCell.getCellStyle() );
String link = data.inputRowMeta.getValueMeta( data.linkfieldnrs[ fieldNr ] ).getString( row[ data.linkfieldnrs[ fieldNr ] ] );
if ( !Utils.isEmpty( link ) ) {
CreationHelper ch = data.wb.getCreationHelper();
} else {
Font origFont = data.wb.getFontAt( cell.getCellStyle().getFontIndex() );
Font hlink_font = data.wb.createFont();
hlink_font.setColor( IndexedColors.BLUE.getIndex() );
CellStyle style = cell.getCellStyle();
style.setFont( hlink_font );
cell.setCellStyle( style );
data.cacheLinkStyle( fieldNr, cell.getCellStyle() );
代码示例来源:origin: stackoverflow.com
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(1);
// Create a new font and alter it.
Font font = wb.createFont();
font.setCharSet(FontCharset.ARABIC.getValue());
font.setFontHeightInPoints((short)24);
font.setFontName("B Nazanin");
font.setItalic(true);
font.setStrikeout(true);
// Fonts are set into a style so create a new one to use.
CellStyle style = wb.createCellStyle();
style.setFont(font);
// Create a cell and put a value in it.
Cell cell = row.createCell(1);
cell.setCellValue("سلام");
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
代码示例来源:origin: cn.afterturn/easypoi-base
@Override
public CellStyle getHeaderStyle(short color) {
CellStyle titleStyle = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
titleStyle.setFont(font);
titleStyle.setBorderLeft(BorderStyle.THIN);
titleStyle.setBorderRight(BorderStyle.THIN);
titleStyle.setBorderBottom(BorderStyle.THIN);
titleStyle.setBorderTop(BorderStyle.THIN);
titleStyle.setAlignment(HorizontalAlignment.CENTER);
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
return titleStyle;
}
代码示例来源:origin: cn.afterturn/easypoi-base
@Override
public CellStyle getTitleStyle(short color) {
CellStyle titleStyle = workbook.createCellStyle();
titleStyle.setBorderLeft(BorderStyle.THIN);
titleStyle.setBorderRight(BorderStyle.THIN);
titleStyle.setBorderBottom(BorderStyle.THIN);
titleStyle.setBorderTop(BorderStyle.THIN);
titleStyle.setAlignment(HorizontalAlignment.CENTER);
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
titleStyle.setWrapText(true);
return titleStyle;
}
代码示例来源:origin: hyberbin/J-Excel
/**
* 给sheet加上标题 居中对齐
* @param sheet
* @param row
* @param length
* @param data
*/
public static void addTitle(Sheet sheet,int row,int length,String data){
Row sheetRow = sheet.createRow(row);
for(int i=0;i<length;i++){
sheetRow.createCell(i);
}
CellStyle style = sheet.getWorkbook().createCellStyle(); // 样式对象
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
CellRangeAddress cellRangeAddress = new CellRangeAddress(row, row, 0, length - 1);
sheet.addMergedRegion(cellRangeAddress);
Cell cell = sheetRow.getCell(0);
cell.setCellStyle(style);
cell.setCellValue(data);
}
代码示例来源:origin: stackoverflow.com
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow((short) 1);
// Aqua background
CellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.BIG_SPOTS);
Cell cell = row.createCell((short) 1);
cell.setCellValue("X");
cell.setCellStyle(style);
// Orange "foreground", foreground being the fill foreground not the font color.
style = wb.createCellStyle();
style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 2);
cell.setCellValue("X");
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
代码示例来源:origin: com.jslsolucoes/tagria-lib
private void header(Sheet sheet, Workbook workbook) {
Row sheetRow = sheet.createRow(0);
int cell = 0;
for (Header header : table.getHeaders()) {
Cell sheetCel = sheetRow.createCell(cell);
sheetCel.setCellValue(header.getContent());
CellStyle cellStyle = workbook.createCellStyle();
if ("center".equals(header.getAlign()))
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
else if ("left".equals(header.getAlign()))
cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
else if ("right".equals(header.getAlign()))
cellStyle.setAlignment(CellStyle.ALIGN_RIGHT);
sheetCel.setCellStyle(cellStyle);
cell++;
}
}
代码示例来源:origin: stackoverflow.com
Workbook wb = new XSSFWorkbook();
Sheet sh = wb.createSheet("Sheet1");
CellStyle left = wb.createCellStyle();
left.setAlignment(CellStyle.ALIGN_LEFT);
CellStyle center = wb.createCellStyle();
center.setAlignment(CellStyle.ALIGN_CENTER);
Row r1 = sh.createRow(1);
Cell c1 = r1.createCell(1);
c1.setCellStyle(left);
c1.setCellValue("Left justified text");
Cell c2 = r1.createCell(2);
c2.setCellStyle(center);
c2.setCellValue(1234);
Cell c3 = r1.createCell(3);
c3.setCellStyle(left);
c3.setCellValue("More Left Justified Text");
FileOutputStream fileOut = new FileOutputStream("CellAlignTest.xlsx");
wb.write(fileOut);
wb.close();
fileOut.close();
代码示例来源:origin: pentaho/pentaho-kettle
stepData.sheet = stepData.wb.createSheet();
stepData.file = null;
stepData.clearStyleCache( numOfFields );
DataFormat format = stepData.wb.createDataFormat();
Row xlsRow = stepData.sheet.createRow( 0 );
Cell cell = xlsRow.createCell( 5 );
CellStyle cellStyle = stepData.wb.createCellStyle();
cellStyle.setBorderRight( BorderStyle.THICK );
cellStyle.setFillPattern( FillPatternType.FINE_DOTS );
cell.setCellStyle( cellStyle );
cellStyle = stepData.wb.createCellStyle();
cellStyle.cloneStyleFrom( cell.getCellStyle() );
cell = xlsRow.createCell( 6 );
cellStyle.setDataFormat( format.getFormat( "##0,000.0" ) );
cell.setCellStyle( cellStyle );
代码示例来源:origin: stackoverflow.com
for (int i = 0; i < 10000; i++) {
Row row = sheet.createRow(i);
Cell cell = row.createCell((short) 0);
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
style.setFont(font);
cell.setCellStyle(style);
}
代码示例来源:origin: stackoverflow.com
XSSFWorkbook wb = new XSSFWorkbook();
CellStyle borderStyle = wb.createCellStyle();
borderStyle.setBorderBottom(CellStyle.BORDER_THIN);
borderStyle.setBorderLeft(CellStyle.BORDER_THIN);
borderStyle.setBorderRight(CellStyle.BORDER_THIN);
borderStyle.setBorderTop(CellStyle.BORDER_THIN);
borderStyle.setAlignment(CellStyle.ALIGN_CENTER);
Sheet sheet = wb.createSheet("Test Sheet");
Row row = sheet.createRow(1);
for (int i = 1; i <= 5; ++i) {
Cell cell = row.createCell(i);
cell.setCellStyle(borderStyle);
if (i == 1) {
cell.setCellValue("Centred Text");
}
}
sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 5));
代码示例来源:origin: hyberbin/J-Excel
public void outputIntAdapter(DataBean dataBean, Object fieldValue, String fieldName,Cell cell) throws AdapterException {
log.debug("in DefaultOutputAdapter:outputIntAdapter fieldName:{} fieldValue:{}",fieldName,fieldValue);
if(ObjectHelper.isNullOrEmptyString(fieldValue)) return;
Workbook workbook = cell.getSheet().getWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
CreationHelper createHelper = workbook.getCreationHelper();
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("#"));
cell.setCellValue(NumberUtils.format(fieldValue,0));
cell.setCellStyle(cellStyle);
}
内容来源于网络,如有侵权,请联系作者删除!