一、nuget
dotnet add package MiniExcel --version 1.31.2
二、新建表及数据
ExampleProducts
三、这里我用了Dapper.Query方法
读取excel
public virtual async Task<IActionResult> Anonymous()
{
try
{
//using (var connection = _dbContext.GetDbConnection())
//{
// var rows = connection.Query(@"select Id, Name from ExampleProducts");
// var path = System.IO.Path.Combine(AppContext.BaseDirectory, "a.xlsx");
// MiniExcel.SaveAs(path, rows);
//}
var path = System.IO.Path.Combine(AppContext.BaseDirectory, "a.xlsx");
using (var stream = System.IO.File.OpenRead(path))
{
var rows = stream.Query<ExampleProduct>();
}
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
}
return Ok(new OkResponse());
}