[Javascript] table 의 내용을 엑셀로 다운로드하기

Posted by 대혀니_
2017. 7. 29. 18:23 IT/잡담

<table id="test" /> 

<button type="button" onclick="tableToExcel('test')" /> 


페이징이 없는 테이블 데이터를 받을 때 사용

한글 처리 핵심은 %EF%BB%BF (유니코드)


function tableToExcel(id) {
    var data_type = 'data:application/vnd.ms-excel;charset=utf-8';
    var table_html = encodeURIComponent(document.getElementById(id).outerHTML);
 
    var a = document.createElement('a');
    a.href = data_type + ',%EF%BB%BF' + table_html;
    a.download = id+'_excel'+'.xls';
    a.click();
}