Gcobani54
Member
- Joined
- Apr 26, 2021
- Messages
- 7
- Programming Experience
- 3-5
Hi Team
I am struggling to make my query dynamic, what i mean here the record list must be formatted. Currently now i will attached the output of my excel and columns are not formatted for data be exposed. The first attachment is my current data. The out output must be second attachment, will attached it here for clarity. Here is my logic below for more information.
I am struggling to make my query dynamic, what i mean here the record list must be formatted. Currently now i will attached the output of my excel and columns are not formatted for data be exposed. The first attachment is my current data. The out output must be second attachment, will attached it here for clarity. Here is my logic below for more information.
exporting record list as excel using asp.net mvc 5:
public IList<ExtractionViewModel> GetExtractionViewModels()
{
var db = new ProductionManagementEntities();
var scheduleList = (from p in db.ProductionDays
from m in db.Models
join w in db.Weeks on p.WeekId equals w.WeekId
orderby w.Year ascending
orderby m.Name descending
where(m.InActive == true)
select new ExtractionViewModel
{
Year = w.Year,
Week = w.WeekNum,
Day = p.ProductionDate,
VW250 = m.Name,
VW270 = m.Name,
VW2502PA = m.Name,
VW270PA = m.Name
}).ToList();
return scheduleList;
}
public class ExtractionViewModel
{
public string Year { get; set; }
public int Week { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0;yyyy-MM-dd}", ApplyFormatInEditMode =false)]
public DateTime Day { get; set; }
public string VW250 { get; set; }
public string VW270 { get; set; }
public string VW2502PA { get; set; }
public string VW270PA { get; set; }
}
public ActionResult DataResult()
{
return View(this.GetExtractionViewModels());
}
public void ExportToExcel()
{
var v = new GridView();
v.DataSource = this.GetExtractionViewModels();
v.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=ExtractionRecords.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter objStringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(objStringWriter);
v.RenderControl(htmlTextWriter);
Response.Output.Write(objStringWriter.ToString());
Response.Flush();
Response.End();
//return View("DataResult");
}