NPOI 读取Excel 数据中 DataTable 和List互转

本文介绍了如何利用NPOI库读取Excel文件,并将数据转换为DataTable和List对象。通过C#编程,结合LINQ查询,实现了高效的数据操作与转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;

namespace Ecolab_CST.Utils
{
    /// <summary>
    /// DataTable扩展方法类
    /// </summary>
    public static class DataTableExtend
    {
        /// <summary>
        /// DataTable转成List
        /// </summary>
        public static List<T> ToDataList<T>(this DataTable dt)
        {
            var list = new List<T>();
            var plist = new List<PropertyInfo>(typeof(T).GetProperties());

            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }

            foreach (DataRow row in dt.Rows)
            {
                T s = Activator.CreateInstance<T>();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    PropertyInfo prop = plist.Find(p => p.Name == dt.Columns[i].ColumnName);
                    if (prop != null&& prop.CanWrite)
                    {
                        try
                        {
                            if (!Convert.IsDBNull(row[i]))
                            {
                                object v = null;
                              
                                if (prop.PropertyType == typeof(bool)||Nullable.GetUnderlyingType(prop.PropertyType) == typeof(bool))
                                {
                                    v = row[i].ToString() == "1" ? true : false;
                                }
                                else
                                {
                                    // 泛型属性可空类型
                                    if ((prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) || prop.PropertyType.ToString().Contains("System.Nullable"))
                                    {
                                        v = Convert.ChangeType(row[i], Nullable.GetUnderlyingType(prop.PropertyType));
                                        
                                    }
                                    else
                                    {
                                        v = Convert.ChangeType(row[i], prop.PropertyType);
                                      
                                    }
                                }


                                prop.SetValue(s, v, null);
                            }
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("字段[" + prop.Name + "]转换出错," + ex.Message);
                        }
                    }
                }
                list.Add(s);
            }
            return list;
        }
        public static bool BoolParse(string str)
        {
            if (str == "0" || str == "")
            {
                return false;
            }
            else if (str == "1")
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        /// <summary>
        /// DataTable转成实体对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static T ToDataEntity<T>(this DataTable dt)
        {
            T s = Activator.CreateInstance<T>();
            if (dt == null || dt.Rows.Count == 0)
            {
                return default(T);
            }
            var plist = new List<PropertyInfo>(typeof(T).GetProperties());
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                PropertyInfo info = plist.Find(p => p.Name == dt.Columns[i].ColumnName);
                if (info != null)
                {
                    try
                    {
                        if (!Convert.IsDBNull(dt.Rows[0][i]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值