function fnExcelReport(id, title) {
var tab_text = '<html xmlns:x="urn:schemas-microsoft-com:office:excel">';
tab_text += '<head><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
tab_text += '<xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>'
tab_text += '<x:Name>Test Sheet</x:Name>';
tab_text += '<x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet>';
tab_text += '</x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>';
tab_text += "<table border='1px'>";
var exportTable = $('#' + id).clone();
exportTable.find('input').each(function(index, elem) {
$(elem).remove();
});
tab_text += exportTable.html();
tab_text += '</table></body></html>';
var data_type = 'data:application/vnd.ms-excel';
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
var fileName = title + '.xls';
//Explorer 환경에서 다운로드
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
if (window.navigator.msSaveBlob) {
var blob = new Blob([ tab_text ], {
type : "application/csv;charset=utf-8;"
});
navigator.msSaveBlob(blob, fileName);
}
} else {
var blob2 = new Blob([ tab_text ], {
type : "application/csv;charset=utf-8;"
});
var filename = fileName;
var elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob2);
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
}
함수선언 후
테이블 만들어줌(테스트용)
<table id="table">
<colgroup>
<col width="10%;">
<col width="20%;">
<col width="5%;">
<col width="5%;">
<col width="5%;">
<col width="5%;">
<col width="5%;">
<col width="5%;">
<col width="5%;">
<col width="5%;">
<col width="5%;">
<col width="5%;">
<col width="5%;">
<col width="5%;">
<col width="10%;">
</colgroup>
<thead>
<tr>
<th class="text-center">순번</th>
<th class="text-center">구분</th>
<th class="text-center">1월</th>
<th class="text-center">2월</th>
<th class="text-center">3월</th>
<th class="text-center">4월</th>
<th class="text-center">5월</th>
<th class="text-center">6월</th>
<th class="text-center">7월</th>
<th class="text-center">8월</th>
<th class="text-center">9월</th>
<th class="text-center">10월</th>
<th class="text-center">11월</th>
<th class="text-center">12월</th>
<th class="text-center">합계</th>
</tr>
</thead>
<tbody>
<tr style="cursor: pointer; cursor: hand;">
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center"></td>
<td class="text-center"></td>
</tbody>
</table>
테이블 만든후 버튼 생성
<button type="button" class="btn bg-gradient-success float-right mr-2" onclick="fnExcelReport('table','title');">엑셀다운로드</button>
끝