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;
            }
        }
  • 相关阅读:
    angularjs 学习笔记(一)
    iconfont项目成员添加不进去的问题
    服务器webapi集成极光推送学习笔记
    winform closing事件注册
    asp.net core webapi 似乎未安装在 IIS 中承载 .NET Core 项目所需的 AspNetCoreModule。请尝试修复 Visual Studio 以纠正该问题。
    css 行内元素设置宽高
    透过浏览器看HTTP缓存[转载]
    矢量字体图标
    HTML5新特性之WebRTC[转]
    一个前后端分离方案[转载]
  • 原文地址:https://www.cnblogs.com/xuxml/p/14073576.html
Copyright © 2011-2022 走看看