Beispiel #1
0
        public static Object GetValueTrueType(object obj, String reultType)
        {
            if (obj == null)
            {
                return(null);
            }
            Object ob       = null;
            String strValue = obj.ToString();

            if (strValue.Equals(""))
            {
                return(null);
            }
            var v = obj.GetType().ToString();

            if (!obj.GetType().ToString().Equals(reultType))
            {
                switch (reultType)
                {
                case "String":
                    ob = strValue;
                    break;

                case "DateTime":


                    if (!strValue.Equals(""))
                    {
                        //日期格式化  - - -
                        try
                        {
                            DateTime dt = Utils.FormatDate(strValue);
                            ob = dt;
                        }
                        catch
                        {
                            //报出错误
                            // throw new Exception(cell.Sheet.SheetName + "数据第:" + (cell.RowIndex + 1) + " 行,第:" + (cell.ColumnIndex + 1) + " 列," + "数据格式不正确,值是:" + cellValue);
                        }
                    }


                    break;

                case "Int32":


                    if (Regex.Match(strValue, @"^-?\d+$").Success)
                    {
                        ob = int.Parse(strValue);
                    }
                    else
                    {
                        //报出错误
                        //throw new Exception("数据第:" + (cell.RowIndex + 1) + " 行,第:" + (cell.ColumnIndex + 1) + " 列," + "数据格式不正确,值是:" + cellValue);
                    }


                    break;

                case "Single":



                    if (Regex.Match(strValue, @"^(-?\d+)(\.\d+)?$").Success)
                    {
                        ob = float.Parse(strValue);
                    }
                    else
                    {
                        if (strValue.StartsWith("."))
                        {
                            ob = float.Parse("0" + strValue);
                        }
                        else
                        {
                            //报出错误
                            // throw new Exception("数据第:" + (cell.RowIndex + 1) + " 行,第:" + (cell.ColumnIndex + 1) + " 列," + "数据格式不正确,值是:" + cellValue);
                        }
                    }

                    break;

                case "Double":

                    if (Regex.Match(strValue, @"^(-?\d+)(\.\d+)?$").Success)
                    {
                        ob = double.Parse(strValue);
                    }
                    else
                    {
                        //报出错误
                        // throw new Exception("数据第:" + (cell.RowIndex + 1) + " 行,第:" + (cell.ColumnIndex + 1) + " 列," + "数据格式不正确,值是:" + cellValue);
                    }

                    break;

                default:
                    //有无法识别的类型
                    break;
                }
            }
            else
            {
                return(obj);
            }
            return(ob);
        }