Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 470.7k

How to export file to excel in linq sql using asp.net mvc

Apr 26 2021 12:38 PM
Hi Team
 
I have excel spreedsheet, but the problem is formatting of data is wrong and need some help on my sql query when using linq on my MVC application. The reqirement is that first column(year must start with 2021), second column(week must be 9 etc if there is 10 must continue per year week etc), Day column must be 03/02/2021 if there was a schedule on this day. VW250 this model column be 79 if there was a model produce, VW250/2PA this model column must be 574. Below snipping image is my current output and formatting of it is not what is expecting and need help. Second is what is required for this exercise
  1. // Model class     
  2. public class ExtractionViewModel    
  3. {    
  4.     public string Year { getset; }  
  5.     public int Week { getset; }  
  6.     public DateTime Day { getset; }  
  7.     public string VW250 { getset; }  
  8.     public string VW270 { getset; }  
  9.     public string VW2502PA { getset; }  
  10.     public string VW270PA { getset; }    
  11. }  
  1. // Controller    
  2. public void ExportToExcel()    
  3. {  
  4.     var v = new GridView();    
  5.     v.DataSource = this.GetExtractionViewModels();    
  6.     v.DataBind();    
  7.     Response.ClearContent();    
  8.     Response.Buffer = true;    
  9.     Response.AddHeader("content-disposition""attachment; filename=ExtractionRecords.xls");    
  10.     Response.ContentType = "application/ms-excel";    
  11.     Response.Charset = "";    
  12.     StringWriter objStringWriter = new StringWriter();    
  13.     HtmlTextWriter htmlTextWriter = new HtmlTextWriter(objStringWriter);    
  14.     v.RenderControl(htmlTextWriter);    
  15.     Response.Output.Write(objStringWriter.ToString());    
  16.     Response.Flush();    
  17.     Response.End();    
  18.     //return View("DataResult");    
  19. }    
  20.   
  21. public ActionResult DataResult()    
  22. {    
  23.     return View(this.GetExtractionViewModels());    
  24. }    
  25.   
  26. public IList GetExtractionViewModels()    
  27. {    
  28.      var db = new ProductionManagementEntities();    
  29.     var scheduleList = (from p in db.ProductionDays    
  30.                         from m in db.Models    
  31.                         join w in db.Weeks on p.WeekId equals w.WeekId    
  32.                         orderby w.Year ascending    
  33.                         orderby m.Name ascending    
  34.                         where(m.InActive == true)    
  35.                         select new ExtractionViewModel    
  36.                         {  
  37.                             Year = w.Year,    
  38.                             Week = w.WeekNum,    
  39.                             Day = p.ProductionDate,    
  40.                             VW250 = m.Name,    
  41.                             VW270 = m.Name,    
  42.                             VW2502PA = m.Name,    
  43.                             VW270PA = m.Name    
  44.                         }).ToList();       
  45.     return scheduleList;    
  46. }  

Answers (3)