
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
/**
* Table 정보를 Excel 다운로드 한다.
* $.tableExcelExport(테이블ID, 시트명, xls 파일명)
*/
(function($) {
$.tableExcelExport = function(tableId, sheet, fileName) {
var worksheet = null;
var table = get(tableId);
const 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 name=ProgId content=Excel.Sheet> <meta name=Generator content="Microsoft Excel 11"><meta http-equiv="Content-Type" content="text/html; 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>'};
const ctx = {worksheet: sheet || 'Worksheet', table: table.innerHTML};
const b64 = base64(format(template.excel, ctx));
let today = new Date();
let year = today.getFullYear(); // 년도
let month = today.getMonth() + 1; // 월
let date = today.getDate(); // 날짜
var yyyymmdd = year + "." + month + "." + date;
fileName = fileName.substr(0, fileName.lastIndexOf(".xls")) + "_" + yyyymmdd + ".xls";
return createDownloadLink(b64, 'application/vnd.ms-excel', fileName);
};
const createDownloadLink = function(base64data, exporttype, filename) {
if (window.navigator.msSaveBlob) {
const blob = b64toBlob(base64data, exporttype);
window.navigator.msSaveBlob(blob, filename);
return false;
} else if(window.URL.createObjectURL) {
const blob = b64toBlob(base64data, exporttype);
var a = $("<a style='display: none;'/>");
var url = window.URL.createObjectURL(blob);
a.attr("href", url);
a.attr("download", filename);
$("body").append(a);
a[0].click();
window.URL.revokeObjectURL(url);
a.remove();
} else {
anchor.download = filename;
anchor.href = "data:" + exporttype + ";base64," + base64data;
}
return true;
};
const format = function(s, c) {
return s.replace(new RegExp("{(\\w+)}", "g"), function(m, p) {
return c[p];
});
};
const get = function(element) {
if (!element.nodeType) {
return document.getElementById(element);
}
return element;
};
const base64 = function(s) {
return window.btoa(unescape(encodeURIComponent(s)));
};
const b64toBlob = function (b64Data, contentType, sliceSize) {
contentType = contentType || '';
sliceSize = sliceSize || 512;
const byteCharacters = window.atob(b64Data);
const byteArrays = [];
let offset;
for (offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
let i;
for (i = 0; i < slice.length; i = i + 1) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new window.Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
return new window.Blob(byteArrays, {
type: contentType
});
};
})(jQuery);