zoukankan      html  css  js  c++  java
  • 【NPOI】把Excel文件转List

      public class ExcelTools
        {
            public static List<T> GetList<T>(string path,string sheetName = "Sheet1") where T : new()
            {
    
                var list = new List<T>();
    
                IWorkbook workbook = new XSSFWorkbook(path);
                var sheetAt = workbook.GetSheetIndex(sheetName);
    
                var sheet = workbook.GetSheetAt(sheetAt == -1 ? 0 : sheetAt);
    
                IRow cellNum = sheet.GetRow(0);
    
                var propertys = typeof(T).GetProperties();
                string value = null;
                int num = cellNum.LastCellNum;
    
                for (int i = 1; i <= sheet.LastRowNum; i++)
                {
                    IRow row = sheet.GetRow(i);
                    var obj = new T();
    
    
                    for (int j = 0; j < num; j++)
                    {
                        var head = cellNum.Cells[j].StringCellValue;
                        var property = propertys.FirstOrDefault(e => e.Name == head);
                        value = row.GetCell(j)?.ToString().Trim();
                        if (property == null || string.IsNullOrEmpty(value))
                        {
                            continue;
                        }
                        var type = property.PropertyType;
                        if (type == typeof(string))
                        {
                            property.SetValue(obj, value, null);
                        }
                        else if (type == typeof(DateTime) || type == typeof(DateTime?))
                        {
                            var dt = row.GetCell(j).DateCellValue;
                            DateTime? pdt = Convert.ToDateTime(dt, CultureInfo.InvariantCulture);
                            property.SetValue(obj, pdt, null);
                        }
                        else if (type == typeof(bool) || type == typeof(bool?))
                        {
                            bool? pb;
                            if (value?.Trim() == "" || value?.Trim() == "1")
                            {
                                pb = true;
                            }
                            else if(value?.Trim()=="" || value?.Trim() == "0")
                            {
                                pb = false;
                            }
                            else
                            {
                                pb = row.GetCell(j).BooleanCellValue;
                            }
                            property.SetValue(obj, pb, null);
                        }
                        else if (type == typeof(int) || type == typeof(int?))
                        {
                            int? _int = (int)row.GetCell(j).NumericCellValue;
                            property.SetValue(obj, _int, null);
                        }
                        else if (type == typeof(long) || type == typeof(long?))
                        {
                            long? _long =(long)row.GetCell(j).NumericCellValue;
                            property.SetValue(obj, _long, null);
                        }
                        else if (type == typeof(double) || type == typeof(double?))
                        {
                            double? _long = row.GetCell(j).NumericCellValue;
                            property.SetValue(obj, _long, null);
                        }
                        else
                        {
                            property.SetValue(obj, null, null);
                        }
                    }
                    list.Add(obj);
                }
    
                return list;
            }
        }
  • 相关阅读:
    合数分解为质数的乘积模板
    P2626 斐波那契数列(升级版)(合数的质数分解, 大数为素数的概率十分小的利用)
    1305 Pairwise Sum and Divide
    1344 走格子 (前缀和)
    1347 旋转字符串
    E
    pek (北大oj)3070
    数学中各种矩阵收集(转至其他博主)
    (数论)逆元的线性算法
    洛谷P2627 修剪草坪 题解 单调队列优化DP
  • 原文地址:https://www.cnblogs.com/xuxml/p/14073576.html
Copyright © 2011-2022 走看看