DataSet does not support System.Nullable<> in Export

前端 未结 4 833
暖寄归人
暖寄归人 2021-01-31 01:34

I was trying to generate a Report using Export to Excell, PDF, TextFile. Well I am doing this in MVC. I have a class which I named SPBatch (which is the exact name of my Stored

4条回答
  •  逝去的感伤
    2021-01-31 02:35

    1) Define This Extensions as Below

    public static class ListExtensions
        {
            public static DataTable ToDataTable(this List list)
            {
                DataTable table = new DataTable(typeof(T).Name);
    
                //Get Properites of List Fiels
                PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    
                //Create Columns as Fields of List
                foreach (PropertyInfo propertyInfo in props)
                {
                    var column = new DataColumn
                    {
                        ColumnName = propertyInfo.Name,
                        DataType = propertyInfo.PropertyType.Name.Contains("Nullable") ? typeof(string) : propertyInfo.PropertyType
                    };
    
                    table.Columns.Add(column);
                }
    
                //Fill DataTable with Rows of List
                foreach (var item in list)
                {
                    var values = new object[props.Length];
    
                    for (var i = 0; i < props.Length; i++)
                    {
                        values[i] = props[i].GetValue(item, null);
                    }
    
                    table.Rows.Add(values);
                }
    
    
                return table;
            }
        }
    

    2) Call Extensions Method as Below where [_lstOperationDetails] is List that we want to convert it from List to DataTable

    DataTable operationDetails = _lstOperationDetails.ToDataTable();
    

提交回复
热议问题