I have this function to export data from a table that has pagination with Datatables, and it only exports me the view of the table and not all the columns.
function exportGrid(tableid,filename) {
var table= document.getElementById(tableid);
$("table tr td:last-child").replaceWith("");
var html = table.outerHTML;
var a = document.createElement('a');
a.id = 'ExcelDL';
a.href = 'data:application/vnd.ms-excel,' + encodeURIComponent(html);
a.download = filename + ".xls";
document.body.appendChild(a);
a.click();
location.reload();
document.getElementById('ExcelDL').remove();
}