The demo Link - "https://jsfiddle.net/OlegKi/ovq05x0c/6/" uses the code
.jqGrid("navButtonAdd", {
caption: "",
title: "Export to Excel(.XLSX)",
onClickButton: function () {
var filename = "jqGrid.xlsx",
data = $(this).jqGrid("getGridParam", "lastSelectedData"), i, item,
dataAsArray = [["Client", "Date", "Amount", "Tax", "Total",
"Closed", "Shipped via"]];
for (i = 0; i < data.length; i++) {
item = data[i];
dataAsArray.push([
item.name,
new Date(item.invdate),
item.amount, item.tax, item.total,
item.closed, item.ship_via]);
}
var ws_name = "SheetJS";
var wb = XLSX.utils.book_new(),
ws = XLSX.utils.aoa_to_sheet(dataAsArray);
XLSX.utils.book_append_sheet(wb, ws, ws_name);
XLSX.writeFile(wb, filename);
}
});
in this demo- click on export button export value of jqgrid select formatter column instead of text of formatter select column(Shipped via). And my requirement is text should export in excel. Can anyone help to solve this issue.
One can fix the code by calling formatter
function and removing <span class='ui-jqgrid-cell-wrapper'>...</span>
wrapper in case of usage autoResizable: true
property. The modified code could looks like
.jqGrid("navButtonAdd", {
caption: "",
title: "Export to Excel(.XLSX)",
onClickButton: function () {
var filename = "jqGrid.xlsx",
self = this,
data = $(this).jqGrid("getGridParam", "lastSelectedData"), i, item,
iColByName = $(this).jqGrid("getGridParam", "iColByName"),
dataAsArray = [["Client", "Date", "Amount", "Tax", "Total", "Closed", "Shipped via"]];
function getFormattedValue (item, cmName) {
var value = self.formatter(item.id, item[cmName], iColByName[cmName], item, "add", item),
ex = /^<span class='ui-jqgrid-cell-wrapper'>([^<]+?)<\/span>$/i.exec(value);
return Array.isArray(ex) && ex.length === 2 ? ex[1] :value;
}
for (i = 0; i < data.length; i++) {
item = data[i];
dataAsArray.push([
item.name,
getFormattedValue(item, "invdate"),
getFormattedValue(item, "amount"),
getFormattedValue(item, "tax"),
getFormattedValue(item, "total"),
item.closed,
getFormattedValue(item, "ship_via")]);
}
var ws_name = "SheetJS";
var wb = XLSX.utils.book_new(),
ws = XLSX.utils.aoa_to_sheet(dataAsArray);
XLSX.utils.book_append_sheet(wb, ws, ws_name);
XLSX.writeFile(wb, filename);
}
});