Hi Apeksha,
Check this example. Now please take its reference and correct your code.
Note: For condition you have to add your switch case.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string id)
{
NorthwindEntities db = new NorthwindEntities();
if (!Directory.Exists(Server.MapPath("~/" + id)))
{
Directory.CreateDirectory(Server.MapPath("~/" + id));
}
List<int?> jobNumList = db.Orders.Where(z => z.CustomerID == id).Select(z => z.EmployeeID).Distinct().ToList();
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddDirectoryByName("Files");
for (int i = 0; i < jobNumList.Count; i++)
{
var ii = jobNumList[i];
string filename = ii + ".xls";
string folderpath = Server.MapPath("~/" + id);
string filepath = Path.Combine(folderpath, filename);
if (db.Products.Any(z => z.CategoryID == ii))
{
var catagory = db.Products.Where(z => z.CategoryID == ii).FirstOrDefault().ProductName;
var model = db.Products.Where(z => z.CategoryID == ii).ToList();
string htmlresult = RenderRazorViewToString(ControllerContext, "~/Views/Home/ExcelTemplate.cshtml", model);
byte[] excelbytes = Encoding.ASCII.GetBytes(htmlresult);
using (Stream file = System.IO.File.Open(filepath, FileMode.OpenOrCreate))
{
file.Write(excelbytes, 0, excelbytes.Length);
file.Flush();
file.Close();
file.Dispose();
}
System.IO.File.WriteAllBytes(filepath, excelbytes);
zip.AddFile(filepath, "Files");
}
}
byte[] bytes = null;
string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd"));
using (MemoryStream memoryStream = new MemoryStream())
{
zip.Save(memoryStream);
bytes = memoryStream.ToArray();
memoryStream.Close();
memoryStream.Dispose();
}
Directory.Delete(Server.MapPath("~/" + id), true);
return File(bytes, "application/zip", zipName);
}
}
private string RenderRazorViewToString(ControllerContext context, string viewPath, object model = null, bool partial = false)
{
ViewEngineResult viewEngineResult = null;
if (partial)
{
viewEngineResult = ViewEngines.Engines.FindPartialView(context, viewPath);
}
else
{
viewEngineResult = ViewEngines.Engines.FindView(context, viewPath, null);
}
var view = viewEngineResult.View;
context.Controller.ViewData.Model = model;
string result = null;
using (var sw = new StringWriter())
{
var ctx = new ViewContext(context, view, context.Controller.ViewData, context.Controller.TempData, sw);
view.Render(ctx, sw);
result = sw.ToString();
}
return result;
}
}
View
Index
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<input type="text" name="id" />
<input type="submit" value="Submit" />
}
</body>
</html>
ExcelTemplate
@model IEnumerable<Multiple_Excel_Zip_MVC.Product>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>ExcelTemplate</title>
</head>
<body>
<table class="table">
<tr>
<th>@Html.DisplayNameFor(model => model.ProductName)</th>
<th>@Html.DisplayNameFor(model => model.CategoryID)</th>
<th>@Html.DisplayNameFor(model => model.UnitPrice)</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.ProductName)</td>
<td>@Html.DisplayFor(modelItem => item.CategoryID)</td>
<td>@Html.DisplayFor(modelItem => item.UnitPrice)</td>
</tr>
}
</table>
</body>
</html>
Screenshot