tarares
tarares

Reputation: 402

Export To Excel JQGrid Data in Spring/JSP

I'm developing a Web application using Spring 3.0, JSP, Tiles. On one of the JSP pages I've to display some data using JQgrid and I've to provide a button to facilitate the user to export the data in JQgrid to an Excel Sheet.

I've successfully created the JQgrid and am able to display the data.

I was wondering how to implement the "Export To Excel" functionality on the click of a button. I tried looking for several solutions but couldn't find anything concrete.

My handler code:

@RequestMapping(method = RequestMethod.POST, value = "/workQueue") 
@ResponseBody   
public JqgridResponse loadXXXXXX(@RequestParam int page, @RequestParam int rows, @RequestParam String sidx, @RequestParam String sord){

    List<ReferralCase> referrals = XXXXService.getReferralCases();

    int endLimit = page * rows;
    int startLimit = endLimit - rows;
    if (endLimit > referrals.size()) {
        endLimit = referrals.size();
    }
    JqgridResponse response = new JqgridResponse();
    response.setRows(referrals.subList(startLimit, endLimit));
    response.setPage(page);
    response.setRecords(referrals.size());
    response.setTotal((referrals.size() / rows) + 1);
    return response;
}

My JSP/JS Code:

    $("#XXXXWorkQueueGrid").jqGrid({
    url:contextRoot+'/dart/workQueue',
    datatype: 'json',
    mtype: 'POST',
    colNames: ['ID','Created','Last Name','First Name','A1','A2','Status','Updated','Workflow'],
    colModel: [
               { name: 'recordId', index: 'recordId', width: 30 },
               { name: 'creationDate', index: 'creationDate', width: 40 },                   
               { name: 'lastName', index: 'lastName', width: 60 },
               { name: 'firstName', index: 'firstName', width: 60 },
               { name: 'A1', index: 'A1', width: 25 },
               { name: 'A2', index: 'A2', width: 25 },
               { name: 'status', index: 'status', width: 40 },
               { name: 'updateDate', index: 'updateDate', width: 40 },
               { name: 'workflow', index: 'workflow', width: 90 }
    ],onPaging: function() {
        $(this).setGridParam({datatype: 'json'}).triggerHandler("reloadGrid");
    },loadComplete: function() {
        $(this).setGridParam({datatype: 'json'}).triggerHandler("reloadGrid");
    },loadError: function(xhr,st,err) {
        alert(err);
    },onSelectRow : function(rowid, status, e) {
        var selRow = $(this).getGridParam("selrow");
        var selRecordId = $(this).getCell(selRow, 'recordId');
        window.location = (contextRoot+"XXXX/referralDetails?recId=" + selRecordId );            
    },
    pager: '#XXXXWorkQueuePager',
    jsonReader: {
        repeatitems: false,
        root: "rows",
        page: "page",
        total: "total",
        records: "records"
    },
    sortorder: "asc",
    sortname: 'recordId',
    gridview: true,
    viewrecords: true,
    hoverrows : false,
    autowidth: true,
    height: 'auto',
    rowNum: 12,
    forceFit: true,
    altRows:true
});
$("#XXXXXWorkQueueGrid").jqGrid('navGrid','#XXXXXWorkQueuePager',{edit:false, add:false, del:false, search:false}); 

});

JqgridResponse is just a custom DAO class created by me.

Any help is appreciated!

Upvotes: 3

Views: 6910

Answers (1)

Benoit Wickramarachi
Benoit Wickramarachi

Reputation: 6226

You can generate a new Excel file using Apache POI and use Spring AbstractExcelView to make it downloadable. Just follow this turorial to setup everything.

Upvotes: 4

Related Questions