Mvc Core read-write excel

All Code

[HttpPost]

public IActionResult ImportExcelOrderAndCargo(IFormFile fileOrderCargo)

{

string path = Path.Combine(“Content/OrderCargo”);

Directory.CreateDirectory(path);

using (FileStream stream = new FileStream(Path.Combine(path, “OrderComplete.xlsx”), FileMode.Create))

{

fileOrderCargo.CopyTo(stream);

stream.Dispose();

}

var streamread = System.IO.File.Open(Path.Combine(path, “OrderComplete.xlsx”), FileMode.Open, FileAccess.Read);

using (var package = new ExcelPackage(streamread))

{

var currentSheet = package.Workbook.Worksheets;

var workSheet = currentSheet.First();

var noOfCol = workSheet.Dimension.End.Column;

var noOfRow = workSheet.Dimension.End.Row;

for (int rowIterator = 2; rowIterator <= noOfRow; rowIterator++)

{

string orderNumber = workSheet.Cells[rowIterator, 1].Value != null ? workSheet.Cells[rowIterator, 1].Value.ToString().Trim() : string.Empty;

Order order = _orderService.GetOrderByNumber(Convert.ToInt32(orderNumber.ToString()));

if (order != null)

{

order.ShippingStatusId = (int)ShippingStatus.Shipped;

order.OrderStatusId = (int)OrderStatus.Complete;

_orderService.UpdateOrder(order);

}

}

}

return RedirectToAction(“List”, “Order”);

}