phat lee

phat lee

  • NA
  • 3
  • 972

Export searched result to excel in asp.net Mvc.

May 23 2018 5:46 PM
I have a web application where I have some search fields. I need to export the search result to excel sheet. I have tried storing the results in session but that slowed down the whole application. What is the efficient way to do it?
 
Below is the sample of the codes I tried. Any suggestions, comments, solutions will be highly appreciated. Thank you
  1. public class APPET1Controller : Controller  
  2. {   
  3. private APContext db = new APContext();  
  4. // GET: APPET1  
  5. public ActionResult Index(string search, string cagecode, string sortBy, int? page, string docNumber, string remark, string status)  
  6. {  
  7. APPETViewModel viewModel = new APPETViewModel();  
  8. var aPPET1 = db.APPET1.Include(t =>T.APPETMedia) .Include(t => t.Status) .Include(t => t.APPETCCode) .Include(t => t.APPETDType);  
............some more codes..............
  1. DateTime searchDate;  
  2. if (!String.IsNullOrEmpty(search))  
  3. {  
  4. bool isDateSearch = DateTime.TryParse(search, out searchDate);  
  5. if (isDateSearch)  
  6. {  
  7. aPPET1 = aPPET1.Where(s => s.Date_Received == searchDate);  
  8. else {  
  9. aPPET1 = aPPET1.Where(t.Doc_Number.Contains(search)  
  10. || t.Status.Status1.Contains(search)  
  11. || t.Remark.Contains(search)  
  12. || t.CCode.Contains(search));  
..............some more codes..............
  1. viewModel.Search = search; } }  
  2. var stats = db.Status.Select(s => s.Status1);  
  3. viewModel.Statuses = new SelectList(stats);  
  4. {  
  5. if (!String.IsNullOrEmpty(status))  
  6. {  
  7. aPPET1 = aPPET1.Where(t => t.Status.Status1.Contains(status));  
  8. }  
  9. }  
  10. if (!String.IsNullOrEmpty(docNumber))  
  11. {  
  12. aPPET1 = aPPET1.Where(t => t.Doc_Number.Contains(docNumber));  
  13. }  
  14. if (!String.IsNullOrEmpty(remark))  
  15. {  
  16. aPPET1 = aPPET1.Where(t => t.Remark.Contains(remark));  
  17. }  
............some more codes.........
  1. Session["SearchResults"] = aPPET.ToList<APPET>(); return View(viewModel); }