How to take table to Excel
function buildHtmlTable(gridOptions) {
var table = document.createElement('table');
table.id = "excelDataTable";
document.body.appendChild(table);
var table$ = $('<table/>');
var columns = addAllColumnHeaders(gridOptions, table$);
for (var i = 0 ; i < gridOptions.data.length ; i++) {
var row$ = $('<tr/>');
for (var colIndex = 0 ; colIndex < columns.length ; colIndex++) {
var cellValue = null;
if (columns[colIndex].indexOf(".") > 0)
{
if (gridOptions.data[i][columns[colIndex].split(".")[0]] !== null){
cellValue = gridOptions.data[i][columns[colIndex].split(".")[0]][columns[colIndex].split(".")[1]];
if (cellValue == false)
cellValue = 0;
else if (cellValue == true)
cellValue = 1;
else if (cellValue != null && cellValue.toString().indexOf("GMT+") != -1 && cellValue.toString().indexOf("Türkiye") != -1 && cellValue.toString().indexOf("Saat") != -1) {
var myDate = new Date(cellValue);
cellValue = myDate.getDate() + "-" + (myDate.getMonth() + 1) + "-" + myDate.getFullYear();
}
}
else
cellValue = "";
} else
{
cellValue = gridOptions.data[i][columns[colIndex]];
if (cellValue == false)
cellValue = "Yok";
else if (cellValue == true)
cellValue = "Var";
else if (cellValue != null && cellValue.toString().indexOf("GMT+") != -1 && cellValue.toString().indexOf("Türkiye") != -1 && cellValue.toString().indexOf("Saat") != -1){
var myDate = new Date(cellValue);
cellValue = myDate.getDate() + "-" + (myDate.getMonth() + 1) + "-" + myDate.getFullYear();
} else if (cellValue != null && cellValue.toString().indexOf("/") != -1) {
cellValue = "'" + cellValue;
}
}
if (cellValue == null) { cellValue = ""; }
row$.append($('<td/>').html(cellValue.toString()));
// row$.append($('<td/>').html("'"+cellValue));
}
table$.append(row$);
//$("#excelDataTable").append(row$);
}
tableToExcel(table$.get(0), 'rapor');
};
function addAllColumnHeaders(gridOptions, tbl) {
var columnSet = [];
var headerTr$ = $('<tr/>');
for (var j = 0; j < gridOptions.columnDefs.length; j++) {
if (gridOptions.columnDefs[j].visible == undefined) {
columnSet.push(gridOptions.columnDefs[j].name);
headerTr$.append($('<th/>').html(gridOptions.columnDefs[j].displayName));
}
}
tbl.append(headerTr$);
return columnSet;
};
var tableToExcel = (function () {
var uri = 'data:application/vnd.ms-excel; base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><meta http-equiv="content-type" content="text/html" charset="UTF-8"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>'
, base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
return function (table, name) {
if (!table.nodeType) table = document.getElementById(table)
var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
window.location.href = uri + base64(format(template, ctx))
}
})()