NET web api 利用NPOI 读取excel
安装NPOI
[HttpPost("users/upload")]
public async Task<IActionResult> Upload(IFormFile file)
{
if (file == null || file.Length == 0)
return this.BadRequest("文件未来上传");
var api_result = new List<string>();
// 文件的处理逻辑
using (var stream = new MemoryStream())
{
// 将文件复制到MemoryStream.
await file.CopyToAsync(stream);
// 使用MemoryStream来解析Excel文件.
IWorkbook workbook;
stream.Position = 0;
workbook = new XSSFWorkbook(stream);
ISheet sheet = workbook.GetSheetAt(0);
for (int row = 0; row <= sheet.LastRowNum; row++)
{
if (sheet.GetRow(row) != null)
{
//for (int column = 0; column < sheet.GetRow(row).LastCellNum; column++)
//{
// // 用你的代码处理每一个单元格的数据
// Console.WriteLine("Value of cell is: " + sheet.GetRow(row).GetCell(column));
//}
var value = sheet.GetRow(row).GetCell(0);
api_result.Add(value.ToString());
}
}
}
return this.Ok(api_result);
}`
I'm fine, it's ok