Exemplo n.º 1
0
        /// <summary>Gets the value of a specific Excel cell.
        /// </summary>
        /// <typeparam name="T">The type of the value.</typeparam>
        /// <param name="excelCell">The Excel cell.</param>
        /// <param name="value">The value of the <paramref name="excelCell"/> (output).</param>
        /// <returns>A value indicating whether <paramref name="value"/> contains valid data.</returns>
        /// <exception cref="ArgumentException">Thrown, if <typeparamref name="T"/> represents a enumeration or <paramref name="excelCell"/> can not converted to an object of type <typeparamref name="T"/>.</exception>
        public static bool TryGetCellValue <T>(object excelCell, out T value)
        {
            if (typeof(T).IsEnum) // which enumStringRepresentationUsage? --> call an other method
            {
                throw new ArgumentException("Do not use these method [TryGetCellValue] for enumerations.");
            }

            if ((excelCell == null) || (excelCell is ExcelEmpty) || (excelCell is ExcelMissing))
            {
                value = default(T);
                return(false);
            }
            Type typeofT = typeof(T);

            if (typeofT == typeof(string))
            {
                if (excelCell is String)
                {
                    value = (T)(object)(string)excelCell;
                    return(true);
                }
                else
                {
                    value = (T)(object)excelCell.ToString();
                    return(true);
                }
            }
            else if (typeofT == typeof(Double))
            {
                double doubleValue;
                if (ExcelDataConverter.TryGetDouble(excelCell, out doubleValue) == true)
                {
                    value = (T)(object)doubleValue;
                    return(true);
                }
            }
            else if (typeofT == typeof(int))
            {
                int intValue;
                if (TryGetInteger(excelCell, out intValue) == true)
                {
                    value = (T)(object)(intValue);
                    return(true);
                }
            }
            else if (typeofT == typeof(DateTime))
            {
                DateTime dateTime;
                if (TryGetDateTime(excelCell, out dateTime) == true)
                {
                    value = (T)((object)dateTime);
                    return(true);
                }
            }
            else if (typeofT == typeof(bool))
            {
                if (excelCell is bool)
                {
                    value = (T)(object)(bool)excelCell;
                    return(true);
                }
                if (excelCell is string)  // a fallback solution
                {
                    IdentifierString stringRepresentation = new IdentifierString((string)excelCell);
                    if (IsTrueExcelCell(stringRepresentation))
                    {
                        value = (T)(object)(true);
                        return(true);
                    }
                    else if (IsFalseExcelCell(stringRepresentation))
                    {
                        value = (T)(object)(false);
                        return(true);
                    }
                }
                value = default(T);
                return(false);
            }
            else if (typeofT == typeof(IdentifierString))
            {
                if (excelCell is String)
                {
                    value = (T)(object)new IdentifierString((string)excelCell);
                    return(true);
                }
                else
                {
                    value = (T)(object)new IdentifierString(excelCell.ToString());
                    return(true);
                }
            }
            else
            {
                TryGetExcelCellValue tryGetExcelCellValue;
                if (sm_ExcelCellConverter.TryGetValue(typeofT, out tryGetExcelCellValue) == true)
                {
                    object tempValue;
                    if (tryGetExcelCellValue(excelCell, out tempValue) == true)
                    {
                        value = (T)tempValue;
                        return(true);
                    }
                }
            }
            if (excelCell is T) // a fallback solution
            {
                value = (T)excelCell;
                return(true);
            }
            value = default(T);
            return(false);
        }