In jqgrid there is perfect function: exportToExcel. It works fine. I wonder if there are some options to add to exported file additional columns or data not from table, but my custom data.
First of all thank you for the question it is very useful.
In order to do the requested job you will need first to download the latest build of Guriddo jqGrid from GitHub
To do the job you will need to use the onBeforeExport event in exportToExcel method with the code like this:
$("#export").on("click", function(){
$("#jqGrid").jqGrid("exportToExcel",{
...
onBeforeExport : function ( xlsx, rowPos ) {
// rowPos is the last inserted index with data in the excel sheet
var sheet = xlsx.xl.worksheets['sheet1.xml'];
// function to add a row
function Addrow(index, data) {
var row = sheet.createElement('row');
row.setAttribute("r", index);
for (i = 0; i < data.length; i++) {
var key = data[i].key;
var value = data[i].value;
var c = sheet.createElement('c');
c.setAttribute("t", "inlineStr");
//c.setAttribute("s", "2"); This make it bold
c.setAttribute("r", key + index);
var is = sheet.createElement('is');
var t = sheet.createElement('t');
var text = sheet.createTextNode(value)
t.appendChild(text);
is.appendChild(t);
c.appendChild(is);
row.appendChild(c);
}
return row;
}
var r1 = Addrow(++rowPos, [{ key: 'A', value: rowPos }, { key: 'B', value: 'Product1' }]);
var r2 = Addrow(++rowPos, [{ key: 'A', value: rowPos }, { key: 'B', value: 'Product2' }]);
var r3 = Addrow(++rowPos, [{ key: 'A', value: rowPos }, { key: 'B', value: 'Product3' }]);
var r4 = Addrow(++rowPos, [{ key: 'A', value: rowPos }, { key: 'B', value: 'Product4' }]);
// get sheetData
var sheetData = sheet.getElementsByTagName('sheetData')[0];
sheetData.insertBefore(r4,sheetData.childNodes[0]);
sheetData.insertBefore(r3,sheetData.childNodes[0]);
sheetData.insertBefore(r2,sheetData.childNodes[0]);
sheetData.insertBefore(r1,sheetData.childNodes[0]);
}
});
});