package org.springblade.business.utils;

import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;

/**
 * @author yangyj
 * @Date 2022/6/8 11:18
 * @description TODO
 */
public class ExcelUtil {
    public static Object getValue(Cell cell) {
        if(cell != null) {
            switch(cell.getCellTypeEnum()){
                case STRING:
                    return cell.getStringCellValue()==null ? null : cell.getStringCellValue().trim();
                case NUMERIC:
                    HSSFDataFormatter dataFormatter = new HSSFDataFormatter();
                    return dataFormatter.formatCellValue(cell);
                case BOOLEAN:
                    return cell.getBooleanCellValue();
                case ERROR:
                    return cell.getErrorCellValue();
                case FORMULA:
                    try {
                        return cell.getStringCellValue();
                    } catch (IllegalStateException e) {
                        return cell.getNumericCellValue();
                    }
                default:
                    cell.setCellType(CellType.STRING);
                    return cell.getStringCellValue()==null ? null : cell.getStringCellValue().trim();
            }
        }
        return null;
    }
}
