您好我试图上传并读取我的asp.net项目中的excel文件,但我找到的所有文档都是针对ASP MVC 5.我的目标是读取excel表并将值传递给对象列表.
这是我的控制器,它适用于将文件上传到我的wwwroot/uploads
public class HomeController : Controller
{
private IHostingEnvironment _environment;
public HomeController(IHostingEnvironment environment)
{
_environment = environment;
}
public IActionResult index()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Index(ICollection<IFormFile> files)
{
var uploads = Path.Combine(_environment.WebRootPath, "uploads");
foreach (var file in files)
{
if (file.Length > 0)
{
using (var fileStream = new FileStream(Path.Combine(uploads, file.FileName), FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
}
}
return View();
}
Run Code Online (Sandbox Code Playgroud)