[HttpPost]
[Authorize]
public async Task<IActionResult> Upload(IFormFile postedFile)
{
if (postedFile == null || postedFile.Length == 0)
{
return RedirectToAction("ImportExcel");
}
//Get file
var newfile = new FileInfo(postedFile.FileName);
var fileExtension = newfile.Extension;
//Check if file is an Excel File
if (fileExtension.Contains(".xls"))
{
using (MemoryStream ms = new MemoryStream())
{
await postedFile.CopyToAsync(ms);
using (ExcelPackage package = new ExcelPackage(ms))
{
ExcelWorksheet workSheet = package.Workbook.Worksheets["Game"];
int totalRows = workSheet.Dimension.Rows;
List<GameBanks> customerList = new List<GameBanks>();
for (int i = 2; i <= totalRows; i++)
{
customerList.Add(new GameBanks
{
ProductDescription = workSheet.Cells[i, 1].Value.ToString(),
ProductCode = workSheet.Cells[i, 2].Value.ToString(),
UnitPrice = Convert.ToDouble(workSheet.Cells[i, 3].Value),
Quantity = Convert.ToInt16(workSheet.Cells[i,4].Value),
Version = workSheet.Cells[i,5].Value.ToString(),
Currency = workSheet.Cells[i,6].Value.ToString(),
TotalPrice = Convert.ToDouble(workSheet.Cells[i,7].Value),
Status = Convert.ToInt16(workSheet.Cells[i,8].Value),
Used = Convert.ToInt16(workSheet.Cells[i,9].Value)
});
}
_context.GameBanks.AddRange(customerList);
await _context.SaveChangesAsync();
}
}
}
return RedirectToAction("Index");
}