complementing Randrade's answer
You can change the lines of
$("#btnExport").click(function(e) {
var a = document.createElement('a');
var data_type = 'data:application/vnd.ms-excel';
var table_div = document.getElementById('dvData');
var table_html = table_div.outerHTML.replace(/ /g, '%20');
a.href = data_type + ', ' + table_html;
a.download = 'filename.xls';
a.click();
e.preventDefault();
});
To:
$(document).ready(function () {
$("#btnExport").click(function (e) {
e.preventDefault();
var table_div = document.getElementById('divTabela');
// esse "\ufeff" é importante para manter os acentos
var blobData = new Blob(['\ufeff'+table_div.outerHTML], { type: 'application/vnd.ms-excel' });
var url = window.URL.createObjectURL(blobData);
var a = document.createElement('a');
a.href = url;
a.download = 'Meu arquivo Excel'
a.click();
});
});
so the accents will remain correctly, and we can export larger tables using bloob
remembering that you should have the script below and a button on the page for export
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script><buttonid="btnExport">Exportar para Excel</button>