Kendo Grid Export CSV is not working

1 Answer 25 Views
Grid
Anil
Top achievements
Rank 1
Anil asked on 06 Jan 2022, 09:30 AM | edited on 06 Jan 2022, 09:32 AM

I have a kendo grid with custom control. Kendo Grid Export CSV is not working for me. Source code is given below. Can anyone help me.

Code running without error but CSV is not generating ( chrome).

1. kendo grid.
    .Name("PayrollReport")
    .ToolBar(toolbar =>
    {
        toolbar.Excel().HtmlAttributes(new { @class = "toolbar-field" });
        toolbar.Custom()
                        .Text("Export To CSV")
                        .HtmlAttributes(new { id = "exportCSV" });

jquerry

 

  $("#PayrollReport").on("click", "#exportCSV", function (e) {

            var batchid = $("#ddlbatchidamexrpt").data("kendoDropDownList").value();
            var status = $("#ddlpayrollstatus").data("kendoDropDownList").value();
            //debugger;
              $.ajax({
                type: "POST",
                url: "/Home/ExportServer",
                  data: JSON.stringify({ 'Batchid': batchid, 'Status': status }),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    //alert("Saved Successfully");
                },
                failure: function (response) {
                    //alert("Error Occured");
                }
              });
            e.preventDefault();



        }); 

Home controller

 

  [HttpPost]
        public FileContentResult ExportServer(string Batchid,string Status)
        {
            var payrollrpt = svc.GetPayrollReport(Batchid, Status);
            StringBuilder sw = new StringBuilder();          

            sw.AppendLine("\"CompanyID\",\"BatchID\",\"Earning5Code\",\"Earning5Amount\"\"Earning5Code\",\"Earning5Amount\"\"Earning5Code\",\"Earning5Amount\"\"Earning5Code\",\"Earning5Amount\"\"Earning5Code\",\"Earning5Amount\"");
           
            sw.Append("\n");
            foreach (var line in payrollrpt)
            {
                sw.AppendLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\",\"{6}\",\"{7}\",\"{8}\",\"{9}\",\"{10}\",\"{11}\"",
                                           line.CompanyID,
                                           line.BatchID,
                                           line.Earning5CodeTERTaxable,
                                           line.TaxableExpense,
                                           line.Earning5CodeHotel,
                                           line.HOTELTAX,
                                           line.Earning5CodeAir,
                                           line.AIRTAXB,
                                           line.Earning5CodeOther,
                                           line.TRNSRTAX,
                                           line.Earning5CodeMeals,
                                           line.Meals ));
              
                sw.Append("\n");
            }          
           
            return File(new System.Text.UTF8Encoding().GetBytes(sw.ToString()), "text/csv", "PayrollReport.csv");          

        }

 

            

1 Answer, 1 is accepted

Sort by
0
Neli
Telerik team
answered on 11 Jan 2022, 08:11 AM

Hello Anil,

I would suggest you take a look at the threads from StackOverflow and MSDN linked below were exporting to CSV is discussed:

https://stackoverflow.com/questions/26780912/stringbuilder-to-csv

https://stackoverflow.com/a/40780124

https://social.msdn.microsoft.com/Forums/vstudio/en-US/80fa29cf-a56f-45c4-8694-13db45627f99/how-to-create-a-csv-file-in-c-?forum=netfxbcl

I hope this helps. 

Regards,
Neli
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Anil
Top achievements
Rank 1
Answers by
Neli
Telerik team
Share this question
or