111 lines
3.3 KiB
C#
111 lines
3.3 KiB
C#
using DevExpress.Spreadsheet;
|
|
using DeviceRepairAndOptimization.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace DeviceRepairAndOptimization.Utils
|
|
{
|
|
public static class PublicExtended
|
|
{
|
|
/// <summary>
|
|
/// 判断DataSet总是否有数据
|
|
/// </summary>
|
|
/// <param name="table"></param>
|
|
/// <returns></returns>
|
|
public static bool HasData(this DataSet ds)
|
|
{
|
|
if (ds != null && ds.Tables.Count > 0)
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断DataTable总是否有数据
|
|
/// </summary>
|
|
/// <param name="table"></param>
|
|
/// <returns></returns>
|
|
public static bool HasData(this DataTable table)
|
|
{
|
|
if (table != null && table.Rows.Count > 0)
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 判断集合是否为空
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="lst"></param>
|
|
/// <returns></returns>
|
|
public static bool IsNullOrZero<T>(this List<T> lst) where T : class
|
|
{
|
|
if (lst == null || lst.Count == 0)
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从集合中随机取出一个对象
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="lst"></param>
|
|
/// <returns></returns>
|
|
public static T RandomOne<T>(this List<T> lst) where T : class
|
|
{
|
|
if (lst != null && lst.Count > 0)
|
|
{
|
|
return lst.OrderBy(x => Guid.NewGuid()).First();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static DataTable toDataTable<T>(this List<T> lst, string TableName = null) where T : class
|
|
{
|
|
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
|
|
DataTable table = new DataTable();
|
|
if (!string.IsNullOrEmpty(TableName))
|
|
table.TableName = TableName;
|
|
|
|
foreach (PropertyDescriptor prop in properties)
|
|
{
|
|
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
|
|
}
|
|
foreach (T item in lst)
|
|
{
|
|
DataRow row = table.NewRow();
|
|
foreach (PropertyDescriptor prop in properties)
|
|
{
|
|
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
|
|
}
|
|
table.Rows.Add(row);
|
|
}
|
|
return table;
|
|
}
|
|
|
|
|
|
public static void WriteValue(this List<SheetDataItem> lst, Cell cell)
|
|
{
|
|
try
|
|
{
|
|
SheetDataItem item = lst.First(x => x.ColumnIndex == cell.ColumnIndex && x.RowIndex == cell.RowIndex);
|
|
if (item != null)
|
|
item.Value = cell.Value.TextValue;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
XtraMessageBoxHelper.Error(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|