由于之前在网上查阅一些资料发现总是不能编译通过,不能正常使用,现把能正常使用的代码贴出:

/// <summary>
   /// excel导入帮助类
   /// </summary>
    public class importexcelutil<t> where t : new()
    {
        //合法文件扩展名
        private static list<string> extname = new list<string>() { ".xls", ".xlsx" };
        /// <summary>
        /// 导入excel内容读取到list<t>中
        /// </summary>
        /// <param name="file">导入execl文件</param>
        /// <param name="sheetname">指定读取excel工作薄sheet的名称</param>
        /// <returns>list<t></returns>
        public static list<t> inputexcel(iformfile file, string sheetname = null)
        {
            //获取文件后缀名
            string type = path.getextension(file.filename);
            //判断是否导入合法文件
            if(!extname.contains(type))
            {
                return null;
            }
            //转成为文件流
            memorystream ms = new memorystream();
            file.copyto(ms);
            ms.seek(0, seekorigin.begin);
            //实例化t数组
            list<t> list = new list<t>();
            //获取数据
            list = inputexcel(ms, sheetname);
            return list;
        }

        /// <summary>
        /// 将excel文件内容读取到list<t>中
        /// </summary>
        /// <param name="filename">文件完整路径名</param>
        /// <param name="sheetname">指定读取excel工作薄sheet的名称</param>
        /// <param name="isfirstrowcolumn">第一行是否是datatable的列名:true=是,false=否</param>
        /// <returns>list<t></returns>
        public static list<t> inputexcel(string filename, string sheetname = null)
        {
            if (!file.exists(filename))
            {
                return null;
            }
            //根据指定路径读取文件
            filestream fs = new filestream(filename, filemode.open, fileaccess.read);
            //实例化t数组
            list<t> list = new list<t>();
            //获取数据
            list = inputexcel(fs, sheetname);

            return list;
        }

        /// <summary>
        /// 将excel文件内容读取到list<t>中
        /// </summary>
        /// <param name="filestream">文件流</param>
        /// <param name="sheetname">指定读取excel工作薄sheet的名称</param>
        /// <returns>list<t></returns>
        private static list<t> inputexcel(stream filestream, string sheetname = null)
        {
            //创建excel数据结构
            iworkbook workbook = workbookfactory.create(filestream);
            //如果有指定工作表名称
            isheet sheet = null;
            if (!string.isnullorempty(sheetname))
            {
                sheet = workbook.getsheet(sheetname);
                //如果没有找到指定的sheetname对应的sheet,则尝试获取第一个sheet
                if (sheet == null)
                {
                    sheet = workbook.getsheetat(0);
                }
            }
            else
            {
                //如果没有指定的sheetname,则尝试获取第一个sheet
                sheet = workbook.getsheetat(0);
            }
            //实例化t数组
            list<t> list = new list<t>();
            if (sheet != null)
            {
                //一行最后一个cell的编号 即总的列数
                irow cellnum = sheet.getrow(0);
                int num = cellnum.lastcellnum;
                //获取泛型对象t的所有属性
                var propertys = typeof(t).getproperties();
                //每行转换为单个t对象
                for (int i = 1; i <= sheet.lastrownum; i++)
                {
                    irow row = sheet.getrow(i);
                    var obj = new t();
                    for (int j = 0; j < num; j++)
                    {
                        //没有数据的单元格都默认是null
                        icell cell = row.getcell(j);
                        if (cell != null)
                        {
                            var value = row.getcell(j).tostring();
                            string str = (propertys[j].propertytype).fullname;
                            if (str == "system.string")
                            {
                                propertys[j].setvalue(obj, value, null);
                            }
                            else if (str == "system.datetime")
                            {
                                datetime pdt = convert.todatetime(value, cultureinfo.invariantculture);
                                propertys[j].setvalue(obj, pdt, null);
                            }
                            else if (str == "system.boolean")
                            {
                                bool pb = convert.toboolean(value);
                                propertys[j].setvalue(obj, pb, null);
                            }
                            else if (str == "system.int16")
                            {
                                short pi16 = convert.toint16(value);
                                propertys[j].setvalue(obj, pi16, null);
                            }
                            else if (str == "system.int32")
                            {
                                int pi32 = convert.toint32(value);
                                propertys[j].setvalue(obj, pi32, null);
                            }
                            else if (str == "system.int64")
                            {
                                long pi64 = convert.toint64(value);
                                propertys[j].setvalue(obj, pi64, null);
                            }
                            else if (str == "system.byte")
                            {
                                byte pb = convert.tobyte(value);
                                propertys[j].setvalue(obj, pb, null);
                            }
                            else
                            {
                                propertys[j].setvalue(obj, null, null);
                            }
                        }
                    }
                    list.add(obj);
                }
            }
            return list;
        }

    }

如使用有问题,请留言。希望能帮到你~~~