Estou tentando exportar uma tabela em html para arquivo em Excel. Este plugin jquery funciona quase que perfeitamente pra mim.
Meu problema é com a acentuação, que vem para o excel com caracteres estranhos. Alguma dica de como resolver?
Stack Overflow em Português é um site de perguntas e respostas para programadores profissionais e entusiastas. Leva apenas um minuto para se inscrever.
Inscreva-se para participar desta comunidadeEstou tentando exportar uma tabela em html para arquivo em Excel. Este plugin jquery funciona quase que perfeitamente pra mim.
Meu problema é com a acentuação, que vem para o excel com caracteres estranhos. Alguma dica de como resolver?
No arquivo Excellentexport.js,nesta linha:
var template = {excel: '<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"><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>'}
Adicione <meta charset="UTF-8">
dentro do <head></head>
Ficando assim:
var template = {excel: '<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"><head><meta charset="UTF-8"><!--[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>'}
Se o seu hmtl for UTF-8
, claro
Função simples que fiz com esse intuito.
//adicionar a classe 'remove' em elementos que não devem aparecer no excel
//(inclusive em inputs type='hidden')
function exportaExcel(id,nome){ //id da <table> e nome do arquivo a ser gerado
var hoje = new Date();
var data = ('0'+hoje.getDate()).slice(-2)+"-"+('0'+(hoje.getMonth()+1)).slice(-2)+"-"+hoje.getFullYear();
var nome_arquivo = nome+"_"+data+".xls"; //nome final do arquivo
var meta = '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />';
var html = $("#"+id).clone();
html.find('.remove').remove();
html.find('a').each(function() {
var txt = $(this).text();
$(this).after(txt).remove();
});
html.find('input, textarea').each(function() {
var txt = $(this).val().replace(/\r\n|\r|\n/g,"<br>");
$(this).after(txt).remove();
});
html.find('select').each(function() {
var txt = $(this).find('option:selected').text();
$(this).after(txt).remove();
});
html.find('br').attr('style', "mso-data-placement:same-cell");
html = "<table>"+html.html()+"</table>";
var uri = 'data:application/vnd.ms-excel,'+encodeURIComponent(meta+html);
var a = $("<a>", {href: uri, download: nome_arquivo});
$(a)[0].click();
}
Você pode chamar a função ao clicar em um botão por exemplo:
$("#botao_exportar").click(function(e){
exportaExcel("id_tabela", "nome_arquivo");
});
Algumas observações: