org.apache.poi.ss.usermodel.CellStyle.setDataFormat()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(550)

本文整理了Java中org.apache.poi.ss.usermodel.CellStyle.setDataFormat()方法的一些代码示例,展示了CellStyle.setDataFormat()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。CellStyle.setDataFormat()方法的具体详情如下:
包路径:org.apache.poi.ss.usermodel.CellStyle
类名称:CellStyle
方法名:setDataFormat

CellStyle.setDataFormat介绍

[英]set the data format (must be a valid format)
[中]设置数据格式(必须是有效格式)

代码示例

代码示例来源:origin: stackoverflow.com

cell.setCellValue(0.123); // set value as number
CellStyle style = workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat("0.000%"));
cell.setCellStyle(style);

代码示例来源:origin: stackoverflow.com

CellStyle cellStyle = wb.createCellStyle();
CreationHelper createHelper = wb.getCreationHelper();
cellStyle.setDataFormat(
  createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
cell = row.createCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);

代码示例来源:origin: looly/hutool

/**
 * 构造
 * 
 * @param workbook 工作簿
 */
public StyleSet(Workbook workbook) {
  this.workbook = workbook;
  this.headCellStyle = StyleUtil.createHeadCellStyle(workbook);
  this.cellStyle = StyleUtil.createDefaultCellStyle(workbook);
  // 默认日期格式
  this.cellStyleForDate = StyleUtil.cloneCellStyle(workbook, this.cellStyle);
  // 22表示:m/d/yy h:mm
  this.cellStyleForDate.setDataFormat((short) 22);
  // 默认数字格式
  cellStyleForNumber = StyleUtil.cloneCellStyle(workbook, this.cellStyle);
  // 2表示:0.00
  cellStyleForNumber.setDataFormat((short) 2);
}

代码示例来源:origin: looly/hutool

/**
 * 构造
 * 
 * @param workbook 工作簿
 */
public StyleSet(Workbook workbook) {
  this.workbook = workbook;
  this.headCellStyle = StyleUtil.createHeadCellStyle(workbook);
  this.cellStyle = StyleUtil.createDefaultCellStyle(workbook);
  // 默认日期格式
  this.cellStyleForDate = StyleUtil.cloneCellStyle(workbook, this.cellStyle);
  // 22表示:m/d/yy h:mm
  this.cellStyleForDate.setDataFormat((short) 22);
  // 默认数字格式
  cellStyleForNumber = StyleUtil.cloneCellStyle(workbook, this.cellStyle);
  // 2表示:0.00
  cellStyleForNumber.setDataFormat((short) 2);
}

代码示例来源: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: pentaho/pentaho-kettle

/**
 * Set specified cell format
 *
 * @param excelFieldFormat the specified format
 * @param cell             the cell to set up format
 */
private void setDataFormat( String excelFieldFormat, Cell cell ) {
 if ( log.isDebug() ) {
  logDebug( BaseMessages.getString( PKG, "ExcelWriterStep.Log.SetDataFormat", excelFieldFormat, CellReference.convertNumToColString( cell.getColumnIndex() ), cell.getRowIndex() ) );
 }
 DataFormat format = data.wb.createDataFormat();
 short formatIndex = format.getFormat( excelFieldFormat );
 CellStyle style = data.wb.createCellStyle();
 style.cloneStyleFrom( cell.getCellStyle() );
 style.setDataFormat( formatIndex );
 cell.setCellStyle( style );
}

代码示例来源:origin: stackoverflow.com

// Do this only once per file
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
  wb.getCreationHelper().createDataFormat().getFormat("#.#"));

// Create the cell
Cell c = row.createCell(2);
c.setCellValue(8.1);
c.setCellStyle(cellStyle);

代码示例来源:origin: pentaho/pentaho-kettle

cellStyle.cloneStyleFrom( cell.getCellStyle() );
cell = xlsRow.createCell( 6 );
cellStyle.setDataFormat( format.getFormat( "##0,000.0" ) );
cell.setCellStyle( cellStyle );

代码示例来源:origin: apache/tika

@Override
public void reset(XSSFWorkbook workbook) {
  style = workbook.createCellStyle();
  style.setDataFormat(workbook.getCreationHelper()
      .createDataFormat().getFormat(formatString));
}

代码示例来源: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: stackoverflow.com

private static Map<String, CellStyle> styles;

private static Map<String, CellStyle> createStyles(Workbook wb){
    Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
    DataFormat df = wb.createDataFormat();

    CellStyle style;
    Font headerFont = wb.createFont();
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
    headerFont.setFontHeightInPoints((short) 12);
    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setFont(headerFont);
    styles.put("style1", style);

    style = createBorderedStyle(wb);
    style.setAlignment(CellStyle.ALIGN_CENTER);
    style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFont(headerFont);
    style.setDataFormat(df.getFormat("d-mmm"));
    styles.put("date_style", style);
    ...
    return styles;
  }

代码示例来源:origin: stackoverflow.com

// Once per workbook - tell excel to format with with two decimal points
DataFormat fmt = wb.createDataFormat()
CellStyle cs = wb.createCellStyle();
cs.setDataFormat(fmt.getFormat("0.00"));

// Once per cell
double d200 = 200.0;
cell.setCellValue(d200);
cell.setCellStyle(cs);

代码示例来源:origin: com.bbossgroups.pdp/pdp-system

private static CellStyle getDateTimeCellStyle(XSSFWorkbook wb) {//用到
  CellStyle cellStyle = wb.createCellStyle();
  cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
  return cellStyle;
}

代码示例来源:origin: stackoverflow.com

HSSFWorkbook wb = new HSSFWorkbook();
HSSFRow row = wb.createSheet().createRow(0);
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(wb.getCreationHelper().createDataFormat().getFormat("HH:mm:ss"));
HSSFCell cell = row.createCell(1);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 1970);
cal.set(Calendar.MONTH, 0);
cal.set(Calendar.DATE, 1);
//you can set the time you need here ...
cell.setCellValue(cal);
cell.setCellStyle(cellStyle);

代码示例来源:origin: dayatang/dddlib

private CellStyle getDateStyle(String dateFormat, Workbook workbook) {
  DataFormat format = workbook.createDataFormat();
  CellStyle result = workbook.createCellStyle();
  result.setDataFormat(format.getFormat(dateFormat));
  return result;
}

代码示例来源:origin: cn.afterturn/easypoi-base

@Override
public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
  CellStyle style = workbook.createCellStyle();
  style.setAlignment(HorizontalAlignment.CENTER);
  style.setVerticalAlignment(VerticalAlignment.CENTER);
  style.setDataFormat(STRING_FORMAT);
  if (isWarp) {
    style.setWrapText(true);
  }
  return style;
}

代码示例来源:origin: org.jeecg/easypoi-base

@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;
}

代码示例来源:origin: com.github.nic-luo/rober-office

public static void setDateCellFormat(Cell cell, String format) {
  CellStyle cellStyle = touchCellStyle(cell);
  DataFormat dataFormat = cell.getSheet().getWorkbook().createDataFormat();
  cellStyle.setDataFormat(dataFormat.getFormat(format));
}

代码示例来源:origin: com.github.nic-luo/rober-office

public static void setNumberCellFormat(Cell cell, String format) {
  CellStyle cellStyle = touchCellStyle(cell);
  DataFormat dataFormat = cell.getSheet().getWorkbook().createDataFormat();
  cellStyle.setDataFormat(dataFormat.getFormat(format));
}

代码示例来源: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);
}

相关文章

微信公众号

最新文章

更多