本文整理汇总了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;
}
示例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;
}
示例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);
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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();
}
示例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;
}
示例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;
}