Example usage for org.apache.poi.xssf.usermodel XSSFCell getCachedFormulaResultType

List of usage examples for org.apache.poi.xssf.usermodel XSSFCell getCachedFormulaResultType

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFCell getCachedFormulaResultType.

Prototype

@Override
public CellType getCachedFormulaResultType() 

Source Link

Document

Only valid for formula cells

Usage

From source file:com.yanglb.utilitys.codegen.core.reader.BaseReader.java

License:Apache License

/**
 * ?Cell???/*  w  w  w  . ja  v a 2  s.  co  m*/
 * @param cell
 * @return
 */
public String getCellStringValue(XSSFCell cell) {
    String result = null;
    int type = cell.getCellType();
    if (type == Cell.CELL_TYPE_FORMULA)
        type = cell.getCachedFormulaResultType();
    if (type == Cell.CELL_TYPE_BLANK)
        return null;
    if (type == Cell.CELL_TYPE_ERROR) {
        return "#VALUE!";
    }

    switch (type) {
    case Cell.CELL_TYPE_BOOLEAN:
        result = String.valueOf(cell.getBooleanCellValue());
        break;

    case Cell.CELL_TYPE_STRING:
        result = cell.getStringCellValue();
        break;

    case Cell.CELL_TYPE_NUMERIC: {
        if (cell.getCellStyle().getDataFormat() == DataFormatType.FORMAT_DATE) {
            Date date = cell.getDateCellValue();
            String format = "yyyy/MM/dd";//cell.getCellStyle().getDataFormatString();
            if (cell.getCellStyle().getDataFormatString().contains(":")) {
                // 
                format = "yyyy/MM/dd HH:mm:ss";
            }
            SimpleDateFormat df = null;
            try {
                df = new SimpleDateFormat(format);
            } catch (IllegalArgumentException e) {
                //df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
            }
            result = df.format(date);
        } else {
            // ?? 0
            Number number = cell.getNumericCellValue();
            result = number.toString();
            if (result.indexOf('.') != -1) {
                result = result.replaceAll("[0]*$", "");
            }
            if (result.endsWith(".")) {
                result = result.substring(0, result.length() - 1);
            }
        }
    }
        break;
    }
    return result;
}