Search code examples
export-to-exceltablesorterjqgrid-asp.net

Jqgrid: Export to excel sorting according to jqgrid table


I have added in my jqgrid to load the content in descending order. But (using OpenXML) after exporting, my data are not in descending order.

Can anyone help to get the solution?

I have added sortname:'Cust_ID' sortorder: "desc" under load jqgrid but this mentioned column Cust_ID is hidden in jqgrid. Now I would like to sort the same as my jqgrid

Below if my export to excel function.

`public string ExportReport(int cp, int noOfOrders)
    {
    string conndb=ConfigurationManager.AppSettings["Db"];
    const string Ids ="0";
    Company pList = _items.GetByCID(cp);
    IQueryable<Product> List

    List=product.SQLDatabase().GetListByCompandIds(cp, Convert.ToInt32(noOfOrders), conndb, Ids)

    XLWorkbook workbook = new XLWorkbook(XLEventTracking.Disabled);
    IXLWorksheet worksheet = workbook.Worksheets.Add("ListReport");

        worksheet.Cell(1, 1).Value = "Date";
        worksheet.Cell(1, 2).Value = "First Name";
        worksheet.Cell(1, 3).Value = "Last Name";
        worksheet.Cell(1, 4).Value = "Email";
        worksheet.Cell(1, 5).Value = "Department";
        worksheet.Cell(1, 6).Value = "Supervisor";

        int i = 2;
        foreach (EList ExpReport in List)
        {
            worksheet.Cell(i, 1).SetValue(ExpReport.Date);
            worksheet.Cell(i, 2).SetValue(ExpReport.FirstName);  
            worksheet.Cell(i, 3).SetValue(ExpReport.LastName);
            worksheet.Cell(i, 4).SetValue(ExpReport.Email);
            worksheet.Cell(i, 5).SetValue(ExpReport.Department);
            worksheet.Cell(i, 6).SetValue(ExpReport.Supervisor);
            ++i;
        }  

        worksheet.Columns().AdjustToContents();
        Session["Workbook"] = workbook;
        string filename;
        filename = "Product-Report";
        return filename;
}`

Solution

  • Client side sorted data in jqGrid CAN NOT be accessed directly from code behind.

    Alternatively, you can get the currently sorted column name from jqGrid in JavaScript, store it in a hidden field (or server control) that can be accessed from code behind, and then apply it to database query in your ExcelExport function.
    For using hidden variable you may follow Persist-JavaScript-variables-and-objects-across-PostBack-in-ASPNet.aspx

    In yet another approach you may get sorted column name from jqGrid in JavaScript and then use AJAX call to pass it to a WebMethod in code behind (or Web API / Script Service / Web Service such as asmx, ashx) which exports data to excel by sorting the data in required order.
    For making ajax post you may refer to using-jquery-to-make-a-post-how-to-properly-supply-data-parameter For AJAX call and WebMethod usage you may follow Send-Pass-multiple-parameters-to-WebMethod-in-jQuery-AJAX-POST-in-ASPNet

    If you need any reference for getting sorted column name you can refer how-to-get-the-current-sort-column-in-a-jqgrid-after-user-clicks-a-column-header