本文整理匯總了Java中org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted方法的典型用法代碼示例。如果您正苦於以下問題:Java DateUtil.isCellDateFormatted方法的具體用法?Java DateUtil.isCellDateFormatted怎麽用?Java DateUtil.isCellDateFormatted使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.poi.ss.usermodel.DateUtil
的用法示例。
在下文中一共展示了DateUtil.isCellDateFormatted方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getNativeValueType
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
@Override
public ValueType getNativeValueType(int columnIndex) throws ParseException {
Cell cell = getCurrentCell(columnIndex);
final int type = cell.getCellType();
if (type == Cell.CELL_TYPE_BLANK) {
return ValueType.EMPTY;
} else if (type == Cell.CELL_TYPE_STRING) {
return ValueType.STRING;
} else if (type == Cell.CELL_TYPE_NUMERIC) {
if (DateUtil.isCellDateFormatted(cell)) {
return ValueType.DATE;
} else {
return ValueType.NUMBER;
}
} else if (type == Cell.CELL_TYPE_FORMULA) {
if (cell.getCachedFormulaResultType() == Cell.CELL_TYPE_NUMERIC) {
return ValueType.NUMBER;
} else {
return ValueType.STRING;
}
} else {
return ValueType.STRING;
}
}
示例2: getCellValue
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
/**
* 獲取單元格的值
*
* @param cell
* @return
*/
public static Object getCellValue(Cell cell) {
Object cellValue = null;
CellType cellType = cell.getCellTypeEnum();// CellType.forInt(cell.getCellType());
if (cellType == CellType.STRING) {
cellValue = cell.getStringCellValue();
} else if (cellType == CellType.NUMERIC) {
if (DateUtil.isCellDateFormatted(cell)) {
cellValue = cell.getDateCellValue();
} else {
cellValue = cell.getNumericCellValue();
}
} else if (cellType == CellType.BOOLEAN) {
cellValue = cell.getBooleanCellValue();
} else if (cellType == CellType.FORMULA) {
cellValue = cell.getCellFormula();
} else if (cellType == CellType.BLANK) {
cellValue = "";
}
return cellValue;
}
示例3: getValueOrEmptyAsObject
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
public static Object getValueOrEmptyAsObject(FormulaEvaluator evaluator, Cell cell) {
final CellValue cellValue = evaluator.evaluate(cell);
if (cellValue == null) {
return "";
}
switch (cellValue.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
return Boolean.valueOf(cellValue.getBooleanValue());
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue();
}
BigDecimal bd = BigDecimal.valueOf(cell.getNumericCellValue()).setScale(DECIMAL_PRECISION, BigDecimal.ROUND_HALF_UP);
return bd.stripTrailingZeros();
case Cell.CELL_TYPE_STRING:
return cellValue.getStringValue();
case Cell.CELL_TYPE_ERROR:
return "ERRO";
default:
return "";
}
}
示例4: getCellString
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
private String getCellString(Cell cell, boolean checkIfDate) {
if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) {
if (checkIfDate && cell.getCellType() != Cell.CELL_TYPE_STRING && DateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
return df.format(date);
}
else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
String str = NumberToTextConverter.toText(cell.getNumericCellValue());
if (str != null) str = str.replace(".", ",");
return getStr(str);
}
else {
cell.setCellType(Cell.CELL_TYPE_STRING);
return getStr(cell.getStringCellValue());
}
}
return null;
}
示例5: intercepterCellValue
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
public void intercepterCellValue(Cell cell, CellWrapper cellWrapper, String interceptor) throws Exception {
if (cell == null) {
cellWrapper.setValue(null);
} else {
int cellType = cell.getCellType();
switch (cellType) {
case Cell.CELL_TYPE_BLANK:
cellWrapper.setValue(null);
break;
case Cell.CELL_TYPE_BOOLEAN:
cellWrapper.setValue(Boolean.valueOf(cell.getBooleanCellValue()));
break;
case Cell.CELL_TYPE_ERROR:
cellWrapper.setValue(cell.getErrorCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
cellWrapper.setValue(cell.getCellFormula());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
cellWrapper.setValue(date);
} else {
cellWrapper.setValue(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_STRING:
cellWrapper.setValue(cell.getStringCellValue());
break;
}
}
logger.debug("before interceptor column number[" + cellWrapper.getColumn() + "] cell value[" + cellWrapper.getValue() + "] cell intercepter[" + interceptor + "]");
this.intercepterCellValue(cellWrapper, interceptor);
logger.debug("after interceptor cell value[" + cellWrapper.getValue() + "]");
}
示例6: getCellPrimitiveValue
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
private static Object getCellPrimitiveValue(Cell cell, int cellType) {
if (cellType == Cell.CELL_TYPE_NUMERIC) {
if (DateUtil.isCellDateFormatted(cell)) {
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(DateUtil.getJavaDate(cell.getNumericCellValue()));
return calendar.getTime();
} else {
return DECIMAL_FORMAT.format(cell.getNumericCellValue());
}
} else if (cellType == Cell.CELL_TYPE_STRING) {
if (StringUtils.hasText(cell.getStringCellValue())) {
return cell.getStringCellValue();
}
}
return null;
}
示例7: getStringValue
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
public String getStringValue(Cell cell) {
if (cell == null) {
return null;
}
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
return cell.getStringCellValue();
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat format = new SimpleDateFormat("yyyy'-'MM'-'dd");
return format.format(cell.getDateCellValue());
}
return Double.toString(cell.getNumericCellValue());
case Cell.CELL_TYPE_BOOLEAN:
return Boolean.toString(cell.getBooleanCellValue());
case Cell.CELL_TYPE_FORMULA:
return getStringFormulaValue(cell);
case Cell.CELL_TYPE_BLANK:
return getStringRangeValue(cell);
default:
System.out.println(cell.getCellType());
return null;
}
}
示例8: getCellValue
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
private Object getCellValue(Cell cellObject) {
Object cellValue = null;
if (cellObject != null) {
switch (cellObject.getCellType()) {
case HSSFCell.CELL_TYPE_BOOLEAN:
cellValue = cellObject.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_STRING:
cellValue = cellObject.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cellObject)) {
cellValue = cellObject.getDateCellValue();
} else {
cellValue = cellObject.getNumericCellValue();
}
break;
default:
break;
}
}
return cellValue;
}
示例9: getValue
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
/** 從表格之中讀取數據並進行處理
* @param fieldInfo
* @param cell 當前cell
* @return
* @throws Exception
*/
public static Object getValue(ImportFieldInfo fieldInfo, Cell cell) throws Exception{
int size = fieldInfo.getTypeChain().size();
Class<?> type = fieldInfo.getTypeChain().get(size - 1);
String dateFormat = fieldInfo.getDateFormat();
int cellType = cell.getCellType();
Object obj = null ;
switch (cellType) {
case Cell.CELL_TYPE_BLANK:
return null;
case Cell.CELL_TYPE_BOOLEAN:
obj = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_STRING:
obj = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)){
obj = DateUtil.getJavaDate(cell.getNumericCellValue());
}else if(Number.class.isAssignableFrom(type) || ClassUtils.isBaseNumberType(type)){
//當pojo字段類型是數字型時以數字形式獲取
obj = cell.getNumericCellValue();
}else{
//其他類型都以string獲取
obj = DATA_FORMATTER.formatCellValue(cell);
}
break;
case Cell.CELL_TYPE_ERROR:
return null;
}
if(fieldInfo.getImportProcessor() != null){
obj = fieldInfo.getImportProcessor().process(obj);
}
obj = ConvertUtils.convertIfNeccesary(obj, type, dateFormat);
return obj;
}
示例10: dateOrNumberProcessing
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
/**
* @param cell
* @return
*/
private String dateOrNumberProcessing(Cell cell) {
String txt;
if (DateUtil.isCellDateFormatted(cell)) {
final DateFormat formatter = new SimpleDateFormat(Constants.DATE_FORMAT);
txt = String.valueOf(formatter.format(cell.getDateCellValue()));
logger.debug("CELL_TYPE_NUMERIC (date): {}", txt);
} else {
txt = String.valueOf(cell.getNumericCellValue());
logger.debug("CELL_TYPE_NUMERIC: {}", txt);
}
return txt;
}
示例11: getValue
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
@SuppressWarnings("deprecation")
private Object getValue(Cell raw, String fieldType) throws CellParsingException {
switch (raw.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
return raw.getBooleanCellValue();
case Cell.CELL_TYPE_STRING: {
String s = raw.getStringCellValue();
String boolString = s.toLowerCase();
if (boolString.equals("yes"))
return true;
if (boolString.equals("no"))
return false;
return s;
}
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(raw)) {
Date date = raw.getDateCellValue();
return LocalDate.of(date.getYear() + 1900, date.getMonth() + 1, date.getDate());
}
return (int) raw.getNumericCellValue();
// formula parsing doesnot work
// can be done using FormulaEvaluater
case Cell.CELL_TYPE_FORMULA:
return true;
case Cell.CELL_TYPE_BLANK: {
return getInstance(fieldType);
}
}
return null;
}
示例12: getCellValue
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
/**
* 獲取指定單元格的值
*
* @param rowNumber 行數,從1開始
* @param cellNumber 列數,從1開始
* @return 該單元格的值
*/
public String getCellValue(int rowNumber, int cellNumber) {
String result;
checkRowAndCell(rowNumber, cellNumber);
Sheet sheet = this.workbook.getSheet(this.sheetName);
Row row = sheet.getRow(--rowNumber);
Cell cell = row.getCell(--cellNumber);
switch (cell.getCellTypeEnum()) {
case BLANK:
result = cell.getStringCellValue();
break;
case BOOLEAN:
result = String.valueOf(cell.getBooleanCellValue());
break;
case ERROR:
result = String.valueOf(cell.getErrorCellValue());
break;
case FORMULA:
result = cell.getCellFormula();
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
result = format.format(cell.getDateCellValue());
} else {
result = String.valueOf(cell.getNumericCellValue());
}
break;
case STRING:
result = cell.getRichStringCellValue().getString();
break;
default:
result = cell.getStringCellValue();
break;
}
return result;
}
示例13: getCellFormatValue
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
/**
* 根據HSSFCell類型設置數據(簡單導入使用)
*
* @param cell 單元格
* @return 處理後的值
*/
private static String getCellFormatValue(Cell cell) {
String cellValue;
if (cell != null) {
// 判斷當前Cell的Type
switch (cell.getCellType()) {
// 如果當前Cell的Type為NUMERIC
case Cell.CELL_TYPE_NUMERIC:
case Cell.CELL_TYPE_FORMULA: {
// 判斷當前的cell是否為Date
if (DateUtil.isCellDateFormatted(cell)) {
// 如果是Date類型則,轉化為Data格式
Date date = cell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cellValue = sdf.format(date);
}
// 如果是純數字
else {
//避免科學計數
java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
nf.setGroupingUsed(false);
// 取得當前Cell的數值
cellValue = String.valueOf(nf.format(cell.getNumericCellValue()));
}
break;
}
// 如果當前Cell的Type為STRING
case Cell.CELL_TYPE_STRING:
// 取得當前的Cell字符串
cellValue = cell.getStringCellValue();
break;
// 默認的Cell值
default:
cellValue = " ";
}
} else {
cellValue = "";
}
return cellValue;
}
示例14: getRow
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
private Map<String, String> getRow(Sheet sheet, int index, Map<Integer, String> columnNames, DataFormatter formatter) {
Map<String, String> values = new HashMap<>();
for (Cell cell : sheet.getRow(index)) {
String key = columnNames.get(cell.getColumnIndex());
if (key != null) {
if (cell.getCellTypeEnum() == CellType.NUMERIC && !DateUtil.isCellDateFormatted(cell)) values.put(key, String.valueOf(cell.getNumericCellValue()));
else values.put(key, formatter.formatCellValue(cell));
}
}
return values;
}
示例15: getRow
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
/**
* retrieves a row as a json object.
*
* @param titles the titles of the row.
* @param row the row to read values from.
* @return a jsonobject that maps titles to the column values.
*/
private JsonObject getRow(String[] titles, Row row) {
DataFormatter formatter = new DataFormatter();
JsonObject json = new JsonObject();
int index = 0;
for (int i = 0; i < row.getLastCellNum(); i++) {
Cell cell = row.getCell(i);
Object value = null;
if (cell != null) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = formatter.formatCellValue(cell);
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
value = cell.getDateCellValue().toInstant().toString();
} else {
value = cell.getNumericCellValue();
}
break;
}
// avoid indexing null or empty string, fails to index rows
// when date fields are empty and can lead to mappings being
// set up incorrectly if leading rows has missing data.
if (value != null && !(value.toString().length() == 0)) {
json.put(titles[index], value);
}
}
index++;
}
return json;
}