コード例 #1
0
    /// <summary>
    /// Convert type of cell value to its predefined type which is specified in the sheet's ScriptMachine setting file.
    /// </summary>
    protected object ConvertFrom(ICell cell, Type t)
    {
        object value = null;

        if (t == typeof(float) || t == typeof(double) || t == typeof(short) || t == typeof(int) || t == typeof(long))
        {
            if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
            {
                value = cell.NumericCellValue;
            }
            else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
            {
                //Get correct numeric value even the cell is string type but defined with a numeric type in a data class.
                if (t == typeof(float))
                {
                    value = Convert.ToSingle(cell.StringCellValue);
                }
                if (t == typeof(double))
                {
                    value = Convert.ToDouble(cell.StringCellValue);
                }
                if (t == typeof(short))
                {
                    value = Convert.ToInt16(cell.StringCellValue);
                }
                if (t == typeof(int))
                {
                    value = Convert.ToInt32(cell.StringCellValue);
                }
                if (t == typeof(long))
                {
                    value = Convert.ToInt64(cell.StringCellValue);
                }
            }
            else if (cell.CellType == NPOI.SS.UserModel.CellType.Formula)
            {
                // Get value even if cell is a formula
                if (t == typeof(float))
                {
                    value = Convert.ToSingle(cell.NumericCellValue);
                }
                if (t == typeof(double))
                {
                    value = Convert.ToDouble(cell.NumericCellValue);
                }
                if (t == typeof(short))
                {
                    value = Convert.ToInt16(cell.NumericCellValue);
                }
                if (t == typeof(int))
                {
                    value = Convert.ToInt32(cell.NumericCellValue);
                }
                if (t == typeof(long))
                {
                    value = Convert.ToInt64(cell.NumericCellValue);
                }
            }
        }
        else if (t == typeof(string) || t.IsArray)
        {
            // HACK: handles the case that a cell contains numeric value
            //       but a member field in a data class is defined as string type.
            //       e.g. string s = "123"
            if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
            {
                value = cell.NumericCellValue;
            }
            else
            {
                value = cell.StringCellValue;
            }
        }
        else if (t == typeof(bool))
        {
            value = cell.BooleanCellValue;
        }
        else if (t.IsGenericType)
        {
            if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
            {
                value = cell.NumericCellValue;
            }
            else
            {
                value = cell.StringCellValue;
            }
        }

        if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable <>)))
        {
            var nc = new NullableConverter(t);
            return(nc.ConvertFrom(value));
        }

        if (t.IsEnum)
        {
            // for enum type, first get value by string then convert it to enum.
            value = cell.StringCellValue;
            return(Enum.Parse(t, value.ToString(), true));
        }
        else if (t.IsArray)
        {
            string strValue = value.ToString();

            if (t.GetElementType() == typeof(float))
            {
                return(ConvertExt.ToSingleArray(strValue));
            }

            if (t.GetElementType() == typeof(double))
            {
                return(ConvertExt.ToDoubleArray(strValue));
            }

            if (t.GetElementType() == typeof(short))
            {
                return(ConvertExt.ToInt16Array(strValue));
            }

            if (t.GetElementType() == typeof(int))
            {
                return(ConvertExt.ToInt32Array(strValue));
            }

            if (t.GetElementType() == typeof(long))
            {
                return(ConvertExt.ToInt64Array(strValue));
            }

            if (t.GetElementType() == typeof(string))
            {
                return(ConvertExt.ToStringArray(strValue));
            }
        }
        else if (t.IsGenericType)
        {
            string strValue = value.ToString();

            if (t == typeof(List <float>))
            {
                return(ConvertExt.ToSingleList(strValue));
            }

            if (t == typeof(List <double>))
            {
                return(ConvertExt.ToDoubleList(strValue));
            }

            if (t == typeof(List <short>))
            {
                return(ConvertExt.ToInt16List(strValue));
            }

            if (t == typeof(List <int>))
            {
                return(ConvertExt.ToInt32List(strValue));
            }

            if (t == typeof(List <long>))
            {
                return(ConvertExt.ToInt64List(strValue));
            }

            if (t == typeof(List <string>))
            {
                return(ConvertExt.ToStringList(strValue));
            }
        }

        // for all other types, convert its corresponding type.
        if (cell.CellType == NPOI.SS.UserModel.CellType.String)
        {
            string str = (string)value;
            str   = str.Replace("\\n", "\n");
            value = str;
        }

        return(Convert.ChangeType(value, t));
    }