Paul Rajs

Paul Rajs

  • NA
  • 641
  • 146.3k

How to Import/Download csv file in Jquery datatable

Dec 11 2020 5:28 AM
Dear Developers,
 
I am creating an application , in that i have to download jquery datatable as csv file.
 
Now am downloading as pdf file using the below code on GenericReportController.cs
 
C# Code
  1. public FileResult ExportCsv(string viewdataName, string fileName) {  
  2.   var data = "";  
  3.   data = TempData[viewdataName].ToString();  
  4.   TempData[viewdataName] = data;  
  5.   var dt = (DataTable) JsonConvert.DeserializeObject(data, (typeof(DataTable)));  
  6.   if (dt.Rows.Count > 0) {  
  7.     dt.TableName = fileName.Replace('/''_');  
  8.     using(XLWorkbook wb = new XLWorkbook()) {  
  9.       wb.Worksheets.Add(dt);  
  10.       using(MemoryStream stream = new MemoryStream()) {  
  11.         wb.SaveAs(stream);  
  12.         return File(stream.ToArray(), "application/octet-stream", fileName + ".csv");  
  13.       }  
  14.     }  
  15.   } else return null;  
  16. }
How can I download to CSV file, kindly suggest me to I am done this task.
 
Regards
Paul.S

Answers (1)