All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feilong.lib.excel.convertor.IntegerConvertor Maven / Gradle / Ivy

Go to download

feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.

There is a newer version: 4.0.8
Show newest version
package com.feilong.lib.excel.convertor;

import static com.feilong.core.bean.ConvertUtil.toInteger;

import com.feilong.excel.ExcelException;
import com.feilong.excel.definition.ExcelCell;

/**
 * 解决 12344.0 转成 int 的问题
 * 
 * @author feilong
 * @since 1.13.2
 */
public class IntegerConvertor extends AbstractChoiceConvertor{

    @Override
    protected Integer convertValue(int sheetNo,String cellIndex,ExcelCell excelCell,Object value){
        if (value instanceof String){
            String str = (String) value;
            str = str.trim();
            if (str.length() == 0){
                if (!excelCell.isMandatory()){
                    return null;
                }
                throw new ExcelException(WRONG_DATA_NULL, sheetNo, cellIndex, null, excelCell);
            }
            //---------------------------------------------------------------
            try{
                return toInteger(value);
            }catch (Exception e){
                throw new ExcelException(WRONG_DATA_TYPE_NUMBER, sheetNo, cellIndex, value, excelCell);
            }
        }else if (value instanceof Double){
            return (int) Math.rint((Double) value);
        }
        throw new ExcelException(WRONG_DATA_TYPE_NUMBER, sheetNo, cellIndex, value, excelCell);
    }

    //---------------------------------------------------------------

    @Override
    public String getDataTypeAbbr(){
        return "integer";
    }

    @Override
    public Class supportClass(){
        return Integer.class;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy