package com.engine.recruit.util;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import weaver.general.Util;

import java.text.SimpleDateFormat;

/**
 * @author:dxfeng
 * @createTime: 2024/01/26
 * @version: 1.0
 */
public class ExcelUtil {
    /**
     * 获取单元格内容
     *
     * @param cell
     * @return
     */
    public static String getCellValue(XSSFCell cell) {
        String cellValue = "";
        if (cell == null) {
            return "";
        }
        switch (cell.getCellType()) {
            case BOOLEAN:
                cellValue = String.valueOf(cell.getBooleanCellValue());
                break;
            case NUMERIC:
                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    // 先看是否是日期格式 读取日期格式
                    SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd");
                    cellValue = sft.format(cell.getDateCellValue());
                } else {
                    // 读取数字
                    cellValue = String.valueOf(new Double(cell.getNumericCellValue()));
                }
                break;
            case FORMULA:
                // 读取公式
                cellValue = cell.getCellFormula();
                break;
            case STRING:
                // 读取String
                cellValue = cell.getStringCellValue();
                break;
            default:
                break;
        }
        cellValue = Util.toHtmlForHrm(cellValue);
        return cellValue;
    }
}
