I have created a view in MVC 5 that allows the end user to select the data from dropdownlist. Once the user has selected all the information, upon click i want those selected data to be shown in the excel in respective cell lets say countryname in A2. How can i go about doing this.
Controller Code
public ActionResult Search()
{
var MarkViewModel = new MarkViewModel
{
Countries = _context.Countries.ToList(),
School = _context.Schools.ToList(),
};
return View("Search", MarkViewModel);
}
View
@model SIMS.ViewModels.MarkViewModel
@{
ViewBag.Title = "Search";
}
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.LabelFor(m => m.CountryId, new { @class = "control-label col-md-4" })
@Html.DropDownListFor(m => m.CountryId, new SelectList(Model.Countries, "Id", "Name"), "Select Country", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.CountryId, "", new { @class = "text-danger" })
@Html.HiddenFor(m => m.Id)
@Html.ActionLink("Cancel", "Index", "Mark", null, new { @class = "btn btn-default" })
Excel i have managed to download excel and set the ranges in the excel form. having issue with capturing data from MVC to excel.
Excel code
public ActionResult MarktoExcel()
{
MemoryStream ms = new MemoryStream();
var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("test");
Response.ClearContent();
Response.Buffer = true;
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=mark.xlsx");
Response.Charset = "";
worksheet.Cell(3, 1).Value = "Country:";
worksheet.Cell(3, 1).Style.Font.Bold = true;
worksheet.Range("B3:B4").Style.Border.RightBorder = XLBorderStyleValues.Thin;
worksheet.Range("A3:B3").Merge();
worksheet.Protect();
workbook.SaveAs(ms);
ms.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
Manikandan MurugesanPosted Jul 4, 2017, 8:04 PM
Rukshar KhanPosted Jul 4, 2017, 8:11 PM
Thanks for your response. Yes i am aware of doing that for database, but in this case i need the data selected to be saved on a excel cell.