/// <summary>
 ///     GetCellValue
 /// </summary>
 /// <param name="cell">cell</param>
 /// <param name="propertyType">propertyType</param>
 /// <returns>cellValue</returns>
 public static object GetCellValue([CanBeNull] this ICell cell, Type propertyType)
 {
     if (cell == null || cell.CellType == CellType.Blank || cell.CellType == CellType.Error)
     {
         return(propertyType.GetDefaultValue());
     }
     return(cell.Value.ToOrDefault(propertyType));
 }
Example #2
0
        /// <summary>
        ///     SetCellValue
        /// </summary>
        /// <param name="cell">ICell</param>
        /// <param name="value">value</param>
        /// <param name="formatter">formatter</param>
        public static void SetCellValue(this ICell cell, object?value, string?formatter)
        {
            if (value is null)
            {
                cell.CellType = CellType.Blank;
                return;
            }

            if (value is DateTime time)
            {
                cell.Value = string.IsNullOrWhiteSpace(formatter)
                    ? time.Date == time?time.ToStandardDateString() : time.ToStandardTimeString()
                                 : time.ToString(formatter);

                cell.CellType = CellType.String;
            }
            else
            {
                var type = value.GetType();
                if (
                    type == typeof(double) ||
                    type == typeof(int) ||
                    type == typeof(long) ||
                    type == typeof(float) ||
                    type == typeof(decimal)
                    )
                {
                    cell.Value    = Convert.ToDouble(value);
                    cell.CellType = CellType.Numeric;
                }
                else if (type == typeof(bool))
                {
                    cell.Value    = value;
                    cell.CellType = CellType.Boolean;
                }
                else
                {
                    cell.Value = value is IFormattable val && formatter.IsNotNullOrWhiteSpace()
                        ? val.ToString(formatter, CultureInfo.CurrentCulture)
                        : value.ToString();
                    cell.CellType = CellType.String;
                }
            }
        }
 /// <summary>
 ///     SetCellValue
 /// </summary>
 /// <param name="cell">ICell</param>
 /// <param name="value">value</param>
 public static void SetCellValue([NotNull] this ICell cell, object value) => cell.SetCellValue(value, null);
 /// <summary>
 ///     GetCellValue
 /// </summary>
 /// <typeparam name="T">Type</typeparam>
 /// <param name="cell">cell</param>
 /// <returns></returns>
 public static T GetCellValue <T>([CanBeNull] this ICell cell) => (cell?.Value).ToOrDefault <T>();