Export to Excel JQGrid Data in Spring / JSP
I am developing a web application using Spring 3.0, JSP, Tiles. In one of the JSP pages, I have to display some data using a JQgrid and I have to provide a button to make it easier for the user to export the data in the JQgrid to an Excel sheet.
I have successfully created a JQgrid and can display data.
I was wondering how to implement the "Export to Excel" feature with one click of a button. I tried to find several solutions but couldn't find anything specific.
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 I created.
Any help is appreciated!
source to share
You can create a new Excel file using Apache POI and use Spring AbstractExcelView
to make it loadable. Just follow this tutorial to get everything set up.
source to share