Hi rani,
You need to add another model class and set the color.
Check this example. Now please take its reference and correct your code.
Excel file
Model
Customer
public class CustomerModel
{
public int Id { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
Detail
public class Details
{
public CustomerModel Customer { get; set; }
public string Color { get; set; }
}
Controller
public class HomeController : Controller
{
private IHostingEnvironment Environment;
public HomeController(IHostingEnvironment _environment)
{
Environment = _environment;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(IFormFile postedFile)
{
string path = Path.Combine(this.Environment.WebRootPath, "Uploads");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string fileName = Path.GetFileName(postedFile.FileName);
string filePath = Path.Combine(path, fileName);
using (FileStream stream = new FileStream(filePath, FileMode.Create))
{
postedFile.CopyTo(stream);
}
DataTable dt = new DataTable();
using (XLWorkbook workBook = new XLWorkbook(filePath))
{
IXLWorksheet workSheet = workBook.Worksheet(1);
bool firstRow = true;
foreach (IXLRow row in workSheet.Rows())
{
if (firstRow)
{
foreach (IXLCell cell in row.Cells())
{
dt.Columns.Add(cell.Value.ToString());
}
firstRow = false;
}
else
{
dt.Rows.Add();
int i = 0;
foreach (IXLCell cell in row.Cells())
{
dt.Rows[dt.Rows.Count - 1][i] = cell.Value.ToString();
i++;
}
}
}
}
List<Details> customers = new List<Details>();
for (int i = 0; i < dt.Rows.Count; i++)
{
CustomerModel customer = new CustomerModel();
customer.Id = Convert.ToInt32(dt.Rows[i]["Id"]);
customer.Name = dt.Rows[i]["Name"].ToString();
customer.Country = dt.Rows[i]["Country"].ToString();
Details detail = new Details();
detail.Customer = customer;
switch (customer.Country.ToLower())
{
case "united states":
detail.Color = "Red";
break;
case "india":
detail.Color = "Orange";
break;
case "russia":
detail.Color = "Green";
break;
default:
detail.Color = "White";
break;
}
customers.Add(detail);
}
ViewBag.Data = customers;
System.IO.File.Delete(filePath);
return View();
}
}
View
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<link rel="stylesheet" media="screen" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" />
</head>
<body class="container">
<form method="post" asp-controller="Home" asp-action="Index" enctype="multipart/form-data">
<input type="file" name="postedFile" class="form-control" />
<input type="submit" value="Upload" class="btn btn-prinary" />
</form>
@if (ViewBag.Data != null)
{
<table class="table">
<tr>
<th>ID</th>
<th>Name</th>
<th>Country</th>
</tr>
@foreach (Details customer in ViewBag.Data)
{
<tr>
<td>@customer.Customer.Id</td>
<td>@customer.Customer.Name</td>
<td style="background-color:@customer.Color">@customer.Customer.Country</td>
</tr>
}
</table>
}
</body>
</html>
Screenshot