Search code examples
jqueryjqgrid

Download excel file with jqgrid complete data using jquery


I basically need complete data from JQGrid and populate to excel file.

 <input type="button" id="download" value="DOWNLOAD" />

controller , how to write GetData(); method which has JQGRID data NOTE: Now excel will download but its empty because of no GetData(); please help me out with GetData();

 public ActionResult ExportToExcel()

        {
            var pcmData = object_ProductCategaryMapping.ProductCategaryMappingGet();
            var Jsondata = JsonConvert.SerializeObject(pcmData);

            var grdReport = new System.Web.UI.WebControls.GridView();
            grdReport.DataSource = GetData(Jsondata);
            grdReport.DataBind();

            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

            grdReport.RenderControl(htw);
            byte[] bindata = System.Text.Encoding.ASCII.GetBytes(sw.ToString());

            return File(bindata, "application/ms-excel", "ss.xls");

        }

        public string GetData(string data) {

               // code here
            return data;
        }
    }

JQuery

$('#download').click(function (e) {

        $.ajax({
            type: 'GET',
            url: '/PCM/ExportToExcel',
            success: function (response) {
                var blob = new Blob([response], { type: 'application/ms-excel' })
                var downloadUrl = URL.createObjectURL(blob);
                var a = document.createElement("a")
                a.href = downloadUrl;
                a.download = "ss.xls"
                document.body.appendChild(a)
                a.click()
            }
        })
        //exportGrid();

    })

or Please say some other way to do it . here in this site I found many method but tried and didnt find proper solution

Note: button is normal html button .(not JQGrid pagination button)

enter image description here


Solution

  • pcmdata contain array of data

     public DataTable GetData() {
                DataTable dt = new DataTable();
                var pcmData = object_ProductCategaryMapping.ProductCategaryMappingGet();
                dt.Columns.Add("Product Priority");
                dt.Columns.Add("Product Code");
                dt.Columns.Add("Product Name");
                dt.Columns.Add("Category");
                dt.Columns.Add("SubCategory");
                dt.Columns.Add("Priority 1");
                dt.Columns.Add("Priority1 per");
                dt.Columns.Add("Priority 2");
                dt.Columns.Add("Priority2 per");
                dt.Columns.Add("Priority 3");
                dt.Columns.Add("Priority3 per");
    
                foreach (var obj in pcmData ) {
                    dt.Rows.Add(obj.ProdPriority, obj.ProductCode, obj.ProductName, obj.Category, obj.SubCategory
                   , obj.Priority_1, obj.Priority1per, obj.Priority_2, obj.Priority2per, obj.Priority_3, obj.Priority3per);
                    
                }