當前位置: 首頁>>代碼示例>>Java>>正文


Java CellStyle.setDataFormat方法代碼示例

本文整理匯總了Java中org.apache.poi.ss.usermodel.CellStyle.setDataFormat方法的典型用法代碼示例。如果您正苦於以下問題:Java CellStyle.setDataFormat方法的具體用法?Java CellStyle.setDataFormat怎麽用?Java CellStyle.setDataFormat使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.poi.ss.usermodel.CellStyle的用法示例。


在下文中一共展示了CellStyle.setDataFormat方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: defaultDataCellStyle

import org.apache.poi.ss.usermodel.CellStyle; //導入方法依賴的package包/類
/**
 * Returns the default data cell style. Obtained from:
 * http://svn.apache.org/repos/asf/poi
 * /trunk/src/examples/src/org/apache/poi/ss/examples/TimesheetDemo.java
 *
 * @param wb the wb
 * @return the cell style
 */
protected CellStyle defaultDataCellStyle(final Workbook wb) {
    CellStyle style;
    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setWrapText(true);
    style.setBorderRight(BorderStyle.THIN);
    style.setRightBorderColor(IndexedColors.BLACK.getIndex());
    style.setBorderLeft(BorderStyle.THIN);
    style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
    style.setBorderTop(BorderStyle.THIN);
    style.setTopBorderColor(IndexedColors.BLACK.getIndex());
    style.setBorderBottom(BorderStyle.THIN);
    style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
    style.setDataFormat(doubleDataFormat);
    return style;
}
 
開發者ID:TFyre,項目名稱:vaadin-gridexport,代碼行數:25,代碼來源:ExcelExport.java

示例2: doCreateCellStyle

import org.apache.poi.ss.usermodel.CellStyle; //導入方法依賴的package包/類
/** 創建每個CellStyle,並放到map之中
 * @param workbook
 * @param styleInfo
 * @param dataFormat 
 * @return
 */
public static CellStyle doCreateCellStyle(Workbook workbook,ExportCellStyleInfo styleInfo,String dataFormat) {
	if(styleInfo != null ){
		CellStyle cellStyle = workbook.createCellStyle();
		setStyleValue(ExportCellStyleInfo.class, cellStyle, styleInfo);
		if(styleInfo.getFontStyleInfo() != null){
			Font fontStyle = workbook.createFont();
			setStyleValue(ExportFontStyleInfo.class, fontStyle, styleInfo.getFontStyleInfo());
			cellStyle.setFont(fontStyle);
		}
		if(!StringUtils.isEmpty(dataFormat)){
			short format = workbook.createDataFormat().getFormat(dataFormat);
			cellStyle.setDataFormat(format);
		}
		return cellStyle;
	}
	return null;
}
 
開發者ID:long47964,項目名稱:excel-utils,代碼行數:24,代碼來源:CellStyleUtils.java

示例3: formatCellDate

import org.apache.poi.ss.usermodel.CellStyle; //導入方法依賴的package包/類
private void formatCellDate(Sheet sheet, Cell cell, String format) {
	CellStyle style = wb.createCellStyle();
	CreationHelper createHelper = wb.getCreationHelper();
	style.setDataFormat(createHelper.createDataFormat().getFormat(format));
	cell.setCellStyle(style);
	 
}
 
開發者ID:gw4e,項目名稱:gw4e.project,代碼行數:8,代碼來源:XLTestSummarySheet.java

示例4: getDecimalStyle

import org.apache.poi.ss.usermodel.CellStyle; //導入方法依賴的package包/類
/**
 * 小數格式
 *
 * @return CellStyle
 */
public CellStyle getDecimalStyle() {
    if (buildInStyleMap.containsKey(DECIMAL_STYLE_KEY)) {
        return buildInStyleMap.get(DECIMAL_STYLE_KEY);
    }

    CellStyle decimalStyle = workbook.createCellStyle();//小數樣式
    if (!buildInFormatMap.containsKey(DATA_FORMAT_KEY)) {
        DataFormat dataFormat = workbook.createDataFormat();
        buildInFormatMap.put(DATA_FORMAT_KEY, dataFormat);
    }
    decimalStyle.setDataFormat(buildInFormatMap.get(DATA_FORMAT_KEY).getFormat("0.00"));
    this.setCommonStyle(decimalStyle);
    buildInStyleMap.put(DECIMAL_STYLE_KEY, decimalStyle);
    return decimalStyle;
}
 
開發者ID:goribun,項目名稱:excel-rw-annotation,代碼行數:21,代碼來源:StyleConfiguration.java

示例5: getDateStyle

import org.apache.poi.ss.usermodel.CellStyle; //導入方法依賴的package包/類
/**
 * 日期樣式 yyyy-MM-dd HH:mm
 *
 * @return CellStyle
 */
public CellStyle getDateStyle() {
    if (buildInStyleMap.containsKey(DATE_STYLE_KEY)) {
        return buildInStyleMap.get(DATE_STYLE_KEY);
    }

    CellStyle dateStyle = workbook.createCellStyle();//日期樣式
    if (!buildInFormatMap.containsKey(DATA_FORMAT_KEY)) {
        DataFormat dataFormat = workbook.createDataFormat();
        buildInFormatMap.put(DATA_FORMAT_KEY, dataFormat);
    }
    dateStyle.setDataFormat(buildInFormatMap.get(DATA_FORMAT_KEY).getFormat("yyyy-MM-dd HH:mm"));
    this.setCommonStyle(dateStyle);
    buildInStyleMap.put(DATE_STYLE_KEY, dateStyle);
    return dateStyle;
}
 
開發者ID:goribun,項目名稱:excel-rw-annotation,代碼行數:21,代碼來源:StyleConfiguration.java

示例6: getDate8Style

import org.apache.poi.ss.usermodel.CellStyle; //導入方法依賴的package包/類
/**
 * 日期樣式 yyyy/MM/dd
 *
 * @return CellStyle
 */
public CellStyle getDate8Style() {

    if (buildInStyleMap.containsKey(DATE_8_STYLE_KEY)) {
        return buildInStyleMap.get(DATE_8_STYLE_KEY);
    }

    CellStyle date8Style = workbook.createCellStyle();//年月日樣式
    if (!buildInFormatMap.containsKey(DATA_FORMAT_KEY)) {
        DataFormat dataFormat = workbook.createDataFormat();
        buildInFormatMap.put(DATA_FORMAT_KEY, dataFormat);
    }
    date8Style.setDataFormat(buildInFormatMap.get(DATA_FORMAT_KEY).getFormat("yyyy/MM/dd"));
    this.setCommonStyle(date8Style);
    buildInStyleMap.put(DATE_8_STYLE_KEY, date8Style);
    return date8Style;
}
 
開發者ID:goribun,項目名稱:excel-rw-annotation,代碼行數:22,代碼來源:StyleConfiguration.java

示例7: getCustomFormatStyle

import org.apache.poi.ss.usermodel.CellStyle; //導入方法依賴的package包/類
/**
 * 根據格式,創建返回樣式對象
 *
 * @param format 格式
 * @return 樣式對象
 */
public CellStyle getCustomFormatStyle(String format) {

    //存在對應格式直接返回
    if (customFormatStyleMap.containsKey(format)) {
        return customFormatStyleMap.get(format);
    }
    CellStyle customDateStyle = workbook.createCellStyle();
    if (!buildInFormatMap.containsKey(DATA_FORMAT_KEY)) {
        DataFormat dataFormat = workbook.createDataFormat();
        buildInFormatMap.put(DATA_FORMAT_KEY, dataFormat);
    }
    customDateStyle.setDataFormat(buildInFormatMap.get(DATA_FORMAT_KEY).getFormat(format));
    this.setCommonStyle(customDateStyle);
    //放入map緩存
    customFormatStyleMap.put(format, customDateStyle);

    return customDateStyle;
}
 
開發者ID:goribun,項目名稱:excel-rw-annotation,代碼行數:25,代碼來源:StyleConfiguration.java

示例8: addCell

import org.apache.poi.ss.usermodel.CellStyle; //導入方法依賴的package包/類
/**
 * 添加一個單元格
 * @param row 添加的行
 * @param column 添加列號
 * @param val 添加值
 * @param align 對齊方式(1:靠左;2:居中;3:靠右)
 * @return 單元格對象
 */
public Cell addCell(Row row, int column, Object val, int align, Class<?> fieldType){
    Cell cell = row.createCell(column);
    CellStyle style = styles.get("data"+(align>=1&&align<=3?align:""));
    try {
        if (val == null){
            cell.setCellValue("");
        } else if (val instanceof String) {
            cell.setCellValue((String) val);
        } else if (val instanceof Integer) {
            cell.setCellValue((Integer) val);
        } else if (val instanceof Long) {
            cell.setCellValue((Long) val);
        } else if (val instanceof Double) {
            cell.setCellValue((Double) val);
        } else if (val instanceof Float) {
            cell.setCellValue((Float) val);
        } else if (val instanceof Date) {
            DataFormat format = wb.createDataFormat();
            style.setDataFormat(format.getFormat("yyyy-MM-dd"));
            cell.setCellValue((Date) val);
        } else {
            if (fieldType != Class.class){
                cell.setCellValue((String)fieldType.getMethod("setValue", Object.class).invoke(null, val));
            }else{
                cell.setCellValue((String)Class.forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
                        "fieldtype."+val.getClass().getSimpleName()+"Type")).getMethod("setValue", Object.class).invoke(null, val));
            }
        }
    } catch (Exception ex) {
        log.info("Set cell value ["+row.getRowNum()+","+column+"] error: " + ex.toString());
        cell.setCellValue(val.toString());
    }
    cell.setCellStyle(style);
    return cell;
}
 
開發者ID:sombie007,項目名稱:ExcelHandle,代碼行數:44,代碼來源:ExportExcel.java

示例9: createDateStyle

import org.apache.poi.ss.usermodel.CellStyle; //導入方法依賴的package包/類
private CellStyle createDateStyle(Workbook workbook) {
    CellStyle style = workbook.createCellStyle();
    style.setDataFormat(workbook.getCreationHelper().createDataFormat()
            .getFormat("m/d/yy"));

    return style;
}
 
開發者ID:Vitaliy-Yakovchuk,項目名稱:ramus,代碼行數:8,代碼來源:Exporter.java

示例10: defaultTotalsDoubleCellStyle

import org.apache.poi.ss.usermodel.CellStyle; //導入方法依賴的package包/類
/**
 * Returns the default totals row style for Double data. Obtained from:
 * http://svn.apache.org/repos/asf/poi
 * /trunk/src/examples/src/org/apache/poi/ss/examples/TimesheetDemo.java
 *
 * @param wb the wb
 * @return the cell style
 */
protected CellStyle defaultTotalsDoubleCellStyle(final Workbook wb) {
    CellStyle style;
    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setDataFormat(doubleDataFormat);
    return style;
}
 
開發者ID:TFyre,項目名稱:vaadin-gridexport,代碼行數:19,代碼來源:ExcelExport.java

示例11: defaultTotalsIntegerCellStyle

import org.apache.poi.ss.usermodel.CellStyle; //導入方法依賴的package包/類
/**
 * Returns the default totals row style for Integer data. Obtained from:
 * http://svn.apache.org/repos/asf/poi
 * /trunk/src/examples/src/org/apache/poi/ss/examples/TimesheetDemo.java
 *
 * @param wb the wb
 * @return the cell style
 */
protected CellStyle defaultTotalsIntegerCellStyle(final Workbook wb) {
    CellStyle style;
    style = wb.createCellStyle();
    style.setAlignment(HorizontalAlignment.CENTER);
    style.setVerticalAlignment(VerticalAlignment.CENTER);
    style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    style.setDataFormat(integerDataFormat);
    return style;
}
 
開發者ID:TFyre,項目名稱:vaadin-gridexport,代碼行數:19,代碼來源:ExcelExport.java

示例12: ExcelTestHelper

import org.apache.poi.ss.usermodel.CellStyle; //導入方法依賴的package包/類
ExcelTestHelper(final String parent, boolean generateXls) throws Exception {
  this.xls = generateXls;

  // Create a test Excel sheet with all types of supported data
  Workbook wb = generateXls ? new HSSFWorkbook() : new XSSFWorkbook();

  CreationHelper creationHelper = wb.getCreationHelper();
  DataFormat dataFormat = creationHelper.createDataFormat();
  short fmt = dataFormat.getFormat("yyyy-mm-dd hh:mm:ss");
  CellStyle style = wb.createCellStyle();
  style.setDataFormat(fmt);

  Sheet sheetWithHeader = wb.createSheet("Sheet 1");

  // Create header row
  Row headerRow = sheetWithHeader.createRow((short) 0);
  headerRow.createCell(0).setCellValue("Number");
  headerRow.createCell(1).setCellValue("String1");
  headerRow.createCell(2).setCellValue("String2");
  headerRow.createCell(3).setCellValue("MyTime");
  headerRow.createCell(4).setCellValue("Formula");
  headerRow.createCell(5).setCellValue("Boolean");
  headerRow.createCell(6).setCellValue("Error");
  generateSheetData(sheetWithHeader, style, (short)1);

  Sheet sheetWithoutHeader = wb.createSheet("Sheet 2");
  generateSheetData(sheetWithoutHeader, style, (short)0);

  testFilePath = new File(parent, "excelTestFile").getPath();

  // Write the output to a file
  FileOutputStream fileOut = new FileOutputStream(testFilePath);
  wb.write(fileOut);
  fileOut.close();
}
 
開發者ID:dremio,項目名稱:dremio-oss,代碼行數:36,代碼來源:ExcelTestHelper.java

示例13: createHSSFCellStyles

import org.apache.poi.ss.usermodel.CellStyle; //導入方法依賴的package包/類
private Map<String, CellStyle> createHSSFCellStyles(Workbook wb, int[] contextBgColor, int[] contextFontColor, int contextFontSize, int contextFontAlign, int[] headerBgColor,
		int[] headerFontColor, int headerFontSize, int headerAlign) {
	Map<String, CellStyle> styles = new HashMap<String, CellStyle>();

	HSSFWorkbook workbook = (HSSFWorkbook) wb;
	HSSFPalette palette = workbook.getCustomPalette();
	palette.setColorAtIndex((short) 11, (byte) contextBgColor[0], (byte) contextBgColor[1], (byte) contextBgColor[2]);
	palette.setColorAtIndex((short) 12, (byte) contextFontColor[0], (byte) contextFontColor[1], (byte) contextFontColor[2]);
	palette.setColorAtIndex((short) 13, (byte) headerBgColor[0], (byte) headerBgColor[1], (byte) headerBgColor[2]);
	palette.setColorAtIndex((short) 14, (byte) headerFontColor[0], (byte) headerFontColor[1], (byte) headerFontColor[2]);

	HSSFFont headerFont = workbook.createFont();
	headerFont.setCharSet(HSSFFont.DEFAULT_CHARSET);
	headerFont.setFontName("宋體");
	headerFont.setColor((short) 14);
	headerFont.setBold(true);
	headerFont.setFontHeightInPoints((short) headerFontSize);
	CellStyle headerStyle = this.createBorderCellStyle(workbook, true);

	headerStyle.setFont(headerFont);
	headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
	headerStyle.setFillForegroundColor((short) 13);
	this.setCellStyleAligment(headerStyle, headerAlign);
	headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
	styles.put(GridStyleType.headerStyle.name(), headerStyle);

	HSSFFont dataFont = workbook.createFont();
	dataFont.setColor((short) 12);
	dataFont.setFontHeightInPoints((short) contextFontSize);
	dataFont.setCharSet(HSSFFont.DEFAULT_CHARSET);
	dataFont.setFontName("宋體");

	CellStyle dataAlignLeftStyle = this.createBorderCellStyle(workbook, true);
	dataAlignLeftStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
	dataAlignLeftStyle.setFillForegroundColor((short) 11);
	dataAlignLeftStyle.setFont(dataFont);
	dataAlignLeftStyle.setVerticalAlignment(VerticalAlignment.CENTER);
	dataAlignLeftStyle.setWrapText(true);
	dataAlignLeftStyle.setAlignment(HorizontalAlignment.LEFT);
	styles.put(GridStyleType.dataAlignLeftStyle.name(), dataAlignLeftStyle);

	CellStyle dataAlignCenterStyle = this.createBorderCellStyle(workbook, true);
	dataAlignCenterStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
	dataAlignCenterStyle.setFillForegroundColor((short) 11);
	dataAlignCenterStyle.setFont(dataFont);
	dataAlignCenterStyle.setVerticalAlignment(VerticalAlignment.CENTER);
	dataAlignCenterStyle.setWrapText(true);
	dataAlignCenterStyle.setAlignment(HorizontalAlignment.CENTER);
	styles.put(GridStyleType.dataAlignCenterStyle.name(), dataAlignCenterStyle);

	CellStyle dataAlignRightStyle = this.createBorderCellStyle(workbook, true);
	dataAlignRightStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
	dataAlignRightStyle.setFillForegroundColor((short) 11);
	dataAlignRightStyle.setFont(dataFont);
	dataAlignRightStyle.setVerticalAlignment(VerticalAlignment.CENTER);
	dataAlignRightStyle.setWrapText(true);
	dataAlignRightStyle.setAlignment(HorizontalAlignment.RIGHT);
	styles.put(GridStyleType.dataAlignRightStyle.name(), dataAlignRightStyle);

	CellStyle dateStyle = this.createBorderCellStyle(workbook, true);
	CreationHelper helper = workbook.getCreationHelper();
	dateStyle.setDataFormat(helper.createDataFormat().getFormat("m/d/yy h:mm"));
	dateStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
	dateStyle.setFillForegroundColor((short) 11);
	dateStyle.setFont(dataFont);
	dateStyle.setVerticalAlignment(VerticalAlignment.CENTER);
	this.setCellStyleAligment(dateStyle, contextFontAlign);
	styles.put(GridStyleType.dateStyle.name(), dateStyle);

	return styles;
}
 
開發者ID:bsteker,項目名稱:bdf2,代碼行數:72,代碼來源:GridStyleBuilder.java

示例14: buildGridExcelData

import org.apache.poi.ss.usermodel.CellStyle; //導入方法依賴的package包/類
private int buildGridExcelData(ReportGrid gridModel, Sheet sheet, int starDataRow, Map<String, CellStyle> styles) {
	CellStyle dataAlignLeftStyle = styles.get(GridStyleType.dataAlignLeftStyle.name());
	CellStyle dataAlignCenterStyle = styles.get(GridStyleType.dataAlignCenterStyle.name());
	CellStyle dataAlignRightStyle = styles.get(GridStyleType.dataAlignRightStyle.name());
	
	IFillCellInterceptor interceptor = ContextHolder.getBean(IFillCellInterceptor.BEAN_ID);
	List<ReportGridHeader> bottomGridExcelHeader = new ArrayList<ReportGridHeader>();
	this.calculateBottomColumnHeader(gridModel.getGridHeaderModelList(), bottomGridExcelHeader);
	
	List<Map<String, Object>> excelDatas = gridModel.getGridDataModel().getDatas();
	String treeColumn = gridModel.getGridDataModel().getTreeColumn();
	int excelDataIndex = 0;
	int rowSize = excelDatas.size();
	Cell cell;
	Row row;
	for (int rowNum = starDataRow; rowNum <= starDataRow + rowSize - 1; rowNum++) {
		row = sheet.createRow(rowNum);
		Map<String, Object> map = excelDatas.get(excelDataIndex);
		int j = 0;
		for (ReportGridHeader header : bottomGridExcelHeader) {
			Object value = map.get(header.getColumnName());
			int dataAlign = header.getDataAlign();
			cell = row.createCell(j);
			if (dataAlign == 1) {
				cell.setCellStyle(dataAlignCenterStyle);
			} else if (dataAlign == 2) {
				cell.setCellStyle(dataAlignRightStyle);
			} else {
				cell.setCellStyle(dataAlignLeftStyle);
			}
			if (value != null) {
				if (header.getColumnName().equalsIgnoreCase(treeColumn)) {
					int level = this.calculateIndentationCount(value.toString());
					cell.setCellStyle(new GridStyleBuilder().createIndentationCellStyle(sheet.getWorkbook(), level == 0 ? 0 : level * 2));
					cell.setCellValue(value.toString());
				} else {
					if (value instanceof BigDecimal || value instanceof Float || value instanceof Double){
				         CellStyle style = cell.getCellStyle();  
						 DataFormat format = sheet.getWorkbook().createDataFormat();  
						 String displayFormat = header.getDisplayFormat();
						 if (StringUtils.isEmpty(displayFormat))
							 style.setDataFormat(format.getFormat("#,##0.00")); 
						 else
							 style.setDataFormat(format.getFormat(displayFormat));
						 cell.setCellStyle(style);
					}
					interceptor.fillCellValue(sheet, row, cell, value);
				}

			} else {
				cell.setCellValue("");
			}
			sheet.setColumnWidth(cell.getColumnIndex(), header.getWidth() / 6 > 255 ? 254 * 256 : header.getWidth() / 6 * 256);
			j++;
		}
		excelDataIndex++;
	}
	return starDataRow + rowSize;

}
 
開發者ID:bsteker,項目名稱:bdf2,代碼行數:61,代碼來源:ExcelReportBuilder.java


注:本文中的org.apache.poi.ss.usermodel.CellStyle.setDataFormat方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。