using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using ExcelDataReader; namespace Common.Helper.Excel { public class ExcelHelper { static ExcelHelper() { } private ExcelHelper() { } public static ExcelHelper Instance { get; } = new ExcelHelper(); /// /// 读取excel的帮助类 必须传该excel有几列 /// /// /// /// public List> Read(string filePath,int columnCount) { var excelContent = new List>(); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); using var stream = System.IO.File.Open(filePath, FileMode.Open, FileAccess.Read); using var reader = ExcelReaderFactory.CreateReader(stream); do { while (reader.Read()) { var cellContent = new List(); for (var i = 0; i < columnCount; i++) { cellContent.Add(reader[i]==null?"": reader[i].ToString()); } if(!cellContent.All(string.IsNullOrEmpty)) excelContent.Add(cellContent); } } while (reader.NextResult()); return excelContent; } } }