Esempio n. 1
0
        internal List <T> GetData <T>(ISheet sheet)
        {
            var result      = new List <T>();
            int rowIndex    = 0; //用于记录执行的位置,以便错误出现时提示
            int columnIndex = 0; //用于记录执行的位置,以便错误出现时提示

            if (sheet.PhysicalNumberOfRows > 0)
            {
                int limitEmptyRow = 5;                           // 最大允许5个连续空行(超出5行则不循环下面的数据了)
                int emptyRow      = 0;                           // 记录连续空行的个数
                T   t             = default(T);
                int start         = this.IsIgnoreHeader ? 1 : 0; // 表头的目录不需要,从1开始
                for (int i = start; i < sheet.PhysicalNumberOfRows; i++)
                {
                    rowIndex = i;//记录位置,以便提示错误
                    IRow row = sheet.GetRow(i);
                    // 行数
                    if (emptyRow >= limitEmptyRow)
                    {
                        break; // 最大允许连续空行
                    }
                    if (row == null || row.GetCell(0) == null || string.IsNullOrWhiteSpace(row.GetCell(0) + ""))
                    {
                        emptyRow++;
                        continue;
                    }
                    emptyRow = 0;//清空空行记录
                    t        = (T)Activator.CreateInstance(typeof(T));
                    // 开始赋值
                    foreach (PropertyInfo pi in this.PropertyInfos)
                    {
                        if (this.mapColumnIndex.ContainsKey(pi))
                        {
                            int index = this.mapColumnIndex[pi];
                            columnIndex = index;//记录位置,以便提示错误
                            try
                            {
                                // 读取Excel指定index列
                                string cellValue = row.GetCell(index) + "";
                                if (!string.IsNullOrWhiteSpace(cellValue))
                                {
                                    object objValue = null;
                                    if (this.mapValueConverter.ContainsKey(pi))
                                    {
                                        IValueConverter converter = this.mapValueConverter[pi];
                                        objValue = converter.deserialize(cellValue, pi.PropertyType, null);
                                    }
                                    else
                                    {
                                        objValue = this.changeType(cellValue, pi.PropertyType);
                                    }
                                    pi.SetValue(t, objValue);
                                }
                            }
                            catch (Exception ex)
                            {
                                throw new Exception(string.Format("表:{0} -- 第{1}行,第{2}列错误,{3}", this.ClazzDisplayName, rowIndex + 1, columnIndex + 1, ex.Message));
                            }
                        }
                    }
                    result.Add(t);
                }
            }
            return(result);
        }