/*
 * @Author: Kane
 * @Date: 2023-10-08 22:12:05
 * @LastEditors: Kane
 * @FilePath: /desktop_archievement_backend/src/main/java/com/cpic/xim/utils/poi/MyPOIUtils.java
 * @Description: POI相关的工具
 * 
 * Copyright (c) ${2023} by Kane, All Rights Reserved. 
 */
package com.cpic.xim.utils.poi;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;

public class MyPOIUtils
{
    public static String getStringCellValue( Cell cell )
    {
        switch ( cell.getCellType())
        {
            case STRING:
                return cell.getStringCellValue();
            default:
                return "";
        }
    }

    public static String getStringCellValue( Row row, int columnIndex )
    {
        Cell cell = row.getCell( columnIndex, MissingCellPolicy.RETURN_NULL_AND_BLANK );

        switch ( cell.getCellType())
        {
            case STRING:
                return cell.getStringCellValue();
            default:
                return "";
        }
    }

    public static double getNumbericCellValue( Cell cell )
    {
        double value = 0.0;

        switch ( cell.getCellType())
        {
            case NUMERIC:
                value = cell.getNumericCellValue();
                break;
            default:
                value = 0.0;
                break;
        }

        return value;
    }

    public static double getNumbericCellValue( Row row, int columnIndex )
    {
        double value = 0.0;
        String strValue;
        Cell cell = row.getCell( columnIndex, MissingCellPolicy.RETURN_NULL_AND_BLANK );
        CellType type = cell.getCellType();
        
        switch ( type )
        {
            case NUMERIC:
                value = cell.getNumericCellValue();
                break;
            case STRING:
                // 字符串类型，尝试转换成数字
                strValue = cell.getStringCellValue();

                try
                {
                    value = Double.parseDouble( strValue );
                }
                // 任何异常都视为转换失败
                catch ( Exception error )
                {
                    value = 0.0;
                }

                break;
            default:
                value = 0.0;
                break;
        }

        return value;
    }
}
