分享一個庫ExcelDataReader ,它專注讀取、支持 .xls/.xlsx、內存優化。
首先安裝NuGet 包
dotnet add package ExcelDataReader
dotnet add package System.Text.Encoding.CodePages
編碼
內存優化??:每次僅讀取一行,適合處理百萬級數據。
??類型安全方法??:可用 GetString(0)、GetDouble(1) 等強類型方法(需確保類型匹配)。
??多工作表支持??:reader.NextResult() 切換工作表
public async Task<dynamic> ImportDataAsync(IFormFile file)
{// 注冊編碼Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);// 解決 .NET Core 編碼問題[1,2,6](@ref)using var stream = new MemoryStream();await file.CopyToAsync(stream);stream.Position = 0;//var reader = ExcelReaderFactory.CreateReader(stream, new ExcelReaderConfiguration//{// Password = "your-password" // 支持加密文件[4](@ref)//});int importCount = 0;using var reader = ExcelReaderFactory.CreateReader(stream);var batch = new List<B_BasicInformation>();// 跳過表頭(假設占1行)if (reader.Read()) { }while (reader.Read()){//流式讀取大文件batch.Add(new B_BasicInformation{Name = reader.GetString(0),//可用 GetString(0)、GetDouble(1) 等強類型方法(需確保類型匹配)IdCard = reader.GetString(1),Province = reader.GetString(2),City = reader.GetString(3),Area = reader.GetString(4),Phone = reader.GetConvertString(5),Address = reader.GetString(6),StudyPhase = reader.GetString(7),Grade = reader.GetString(8),Class = reader.GetString(9),School = reader.GetString(10),SchoolCode = reader.GetConvertStringGuid(11),Gender = idCardResult.gender,Birthday = idCardResult.birthday}); if (batch.Count >= 100){//批量插入_repository._Db.Insertable(batch).ExecuteCommand();batch.Clear();}}return new {total=importCount };
}
小文件讀取
public DataSet ReadExcelAsDataSet(string filePath)
{using var stream = File.Open(filePath, FileMode.Open, FileAccess.Read);using var reader = ExcelReaderFactory.CreateReader(stream);// 配置:首行作為列名,忽略空行var result = reader.AsDataSet(new ExcelDataSetConfiguration(){ConfigureDataTable = _ => new ExcelDataTableConfiguration(){UseHeaderRow = true, // 第一行為列名[4,7](@ref)FilterRow = row => row[0]?.ToString() != "" // 跳過空行[4](@ref)}});return result;
}// 使用示例:
var dataSet = ReadExcelAsDataSet("data.xlsx");
foreach (DataTable table in dataSet.Tables)
{Console.WriteLine($"表名: {table.TableName}");foreach (DataRow row in table.Rows){Console.WriteLine($"{row["姓名"]}, 年齡: {row["年齡"]}");}
}