分享 java 正文

解决POI(3.15后)读取excel的值过时问题

じ辰╆紫天せ
じ辰╆紫天せ 2017-11-16 13:21:46
32982 0 修改博文
我使用的版本信息如下:
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
以前读取值如下,当升级版本后,提示过时了(以前使用的接口定义的,现在使用枚举了,也可以如:CellType.STRING.getCode())
public static String getCellValue(Cell cell)
{
    if (cell == null)
    {
        return null;
    }
    String ret;
    switch (cell.getCellType())
    {
        case Cell.CELL_TYPE_BLANK:
            ret = null;
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            ret = String.valueOf(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_ERROR:
            ret = null;
            break;
        case Cell.CELL_TYPE_FORMULA:
            Workbook wb = cell.getSheet().getWorkbook();
            CreationHelper crateHelper = wb.getCreationHelper();
            FormulaEvaluator evaluator = crateHelper.createFormulaEvaluator();
            ret = getCellValue(evaluator.evaluateInCell(cell));
            break;
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell))
            {
                Date theDate = cell.getDateCellValue();
                ret = new SimpleDateFormat(DATE_OUTPUT_PATTERNS).format(theDate);
            } else
            {
                ret = NumberToTextConverter.toText(cell.getNumericCellValue());
            }
            break;
        case Cell.CELL_TYPE_STRING:
            ret = cell.getRichStringCellValue().getString();
            break;
        default:
            ret = null;
    }
    return ret == null ? null : ret.trim(); // 有必要自行trim
}

CellTypeEnum

/**
 * Unknown type, used to represent a state prior to initialization or the
 * lack of a concrete type.
 * For internal use only.
 */
@Internal(since="POI 3.15 beta 3")
_NONE(-1),

/**
 * Numeric cell type (whole numbers, fractional numbers, dates)
 */
NUMERIC(0),

/** String (text) cell type */
STRING(1),

/**
 * Formula cell type
 * @see FormulaType
 */
FORMULA(2),

/**
 * Blank cell type
 */
BLANK(3),

/**
 * Boolean cell type
 */
BOOLEAN(4),

/**
 * Error cell type
 * @see FormulaError
 */
ERROR(5);
解决方案
public static String getCellValue(Cell cell)
{
    if (cell == null)
    {
        return null;
    }
    String ret;
    switch (cell.getCellTypeEnum())
    {
        case BLANK:
            ret = null;
            break;
        case BOOLEAN:
            ret = String.valueOf(cell.getBooleanCellValue());
            break;
        case ERROR:
            ret = null;
            break;
        case FORMULA:
            Workbook wb = cell.getSheet().getWorkbook();
            CreationHelper crateHelper = wb.getCreationHelper();
            FormulaEvaluator evaluator = crateHelper.createFormulaEvaluator();
            ret = getCellValue(evaluator.evaluateInCell(cell));
            break;
        case NUMERIC:
            if (DateUtil.isCellDateFormatted(cell))
            {
                Date theDate = cell.getDateCellValue();
                ret = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(theDate);
            } else
            {
                ret = NumberToTextConverter.toText(cell.getNumericCellValue());
            }
            break;
        case STRING:
            ret = cell.getRichStringCellValue().getString();
            break;
        default:
            ret = null;
    }
    return ret == null ? null : ret.trim(); // 有必要自行trim
}

欢迎打赏投稿

我要投稿

分享分类

推荐博文