本文整理汇总了Java中org.apache.poi.ss.usermodel.DataFormat.getFormat方法的典型用法代码示例。如果您正苦于以下问题:Java DataFormat.getFormat方法的具体用法?Java DataFormat.getFormat怎么用?Java DataFormat.getFormat使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.poi.ss.usermodel.DataFormat
的用法示例。
在下文中一共展示了DataFormat.getFormat方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setCellValue
import org.apache.poi.ss.usermodel.DataFormat; //导入方法依赖的package包/类
protected static void setCellValue(Workbook wb, Cell cell, Object value) {
if (cell == null) {
return;
}
if (value instanceof Boolean) {
cell.setCellValue((Boolean)value);
} else if (value instanceof Number) {
cell.setCellValue(((Number)value).doubleValue());
} else if (value instanceof Date) {
CellStyle cellStyle = wb.createCellStyle();
DataFormat poiFormat = wb.createDataFormat();
// Format: 0x16, "m/d/yy h:mm"
final short format = poiFormat.getFormat(BuiltinFormats.getBuiltinFormat(0x16));
cellStyle.setDataFormat(format);
cell.setCellValue(((Date)value));
cell.setCellStyle(cellStyle);
} else if (value instanceof Calendar) {
cell.setCellValue(((Calendar)value));
} else if (value != null) {
cell.setCellValue(value.toString());
} else {
cell.setCellValue("");
cell.setCellType(Cell.CELL_TYPE_BLANK);
}
}
示例2: ExcelTestHelper
import org.apache.poi.ss.usermodel.DataFormat; //导入方法依赖的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();
}
示例3: getFormatPattern
import org.apache.poi.ss.usermodel.DataFormat; //导入方法依赖的package包/类
@Override
public String getFormatPattern() {
final DataFormat dataFormat = cell.getSheet().getWorkbook().createDataFormat();
final short formatIndex = getFormatIndex();
String formatPattern = dataFormat.getFormat(formatIndex);
if(formatPattern == null) {
formatPattern = "";
}
return formatPattern;
}
示例4: setDataFormat
import org.apache.poi.ss.usermodel.DataFormat; //导入方法依赖的package包/类
/**
* 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 );
}
示例5: changeValueIfNeeded
import org.apache.poi.ss.usermodel.DataFormat; //导入方法依赖的package包/类
private void changeValueIfNeeded(Cell cell) {
//validate
String originalValue = cell.getStringCellValue();
if (originalValue == null || originalValue.isEmpty()) return;
//interprets
Object result = interpreter.parseTemplate(originalValue, context, variables, formatter);
//converts
if (result != null) {
if (formatter.format() != null) {
//create data format
DataFormat df = workbook.createDataFormat();
short sdf = df.getFormat(formatter.format());
//set format
CellStyle style = cell.getCellStyle();
if (style == null) {
style = workbook.createCellStyle();
}
style.setDataFormat(sdf);
}
if (result instanceof Date) {
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue((Date) result);
} else if (result instanceof Calendar) {
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
cell.setCellValue((Calendar) result);
} else if (result instanceof Number) {
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
Number numValue = (Number) result;
cell.setCellValue(numValue.doubleValue());
} else if (result instanceof Boolean) {
cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
Boolean boolValue = (Boolean) result;
cell.setCellValue(boolValue);
} else {
String strValue = result.toString();
if (!originalValue.equals(strValue)) {
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue(strValue);
}
}
} else {
cell.setCellValue((String) null);
}
}
示例6: dateFormat
import org.apache.poi.ss.usermodel.DataFormat; //导入方法依赖的package包/类
public static short dateFormat(Workbook workbook) {
DataFormat format = workbook.createDataFormat();
return format.getFormat("mm/dd/yyyy hh:mm");
}