JS 浣跨敤xlsx.core.js 鏁版嵁瀵煎嚭鍒癳xcel
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS 浣跨敤xlsx.core.js 鏁版嵁瀵煎嚭鍒癳xcel相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/res' title='res'>res
load col 瀵煎嚭 tle 琛ㄦ牸 date app moment/* 閫氱敤瀵煎嚭鏁版嵁 闇€瑕佸紩鍏?xlsx.core.js * data锛氭暟鎹? * th锛氳〃澶? * filename锛氬鍑鸿〃鏍煎悕绉? */ var data = []; var th = [ ["js瀵煎嚭excel琛?, "", "", "", "", ""], ["鏍囬1", "鏍囬2", "鏍囬3", "鏍囬4", "鏍囬5", "鏍囬6"], ]; var filename = "瀵煎嚭琛ㄦ牸鍚嶇О"锛?br>銆€銆€exportData(data, th, filename); function exportData(data, th, filename) { var deferred = $.Deferred(); data = data || []; th = th || []; var merges = []; merges.push({ s: { //s涓哄紑濮? c: 0, //寮€濮嬪垪 r: 0 //寮€濮嬭 }, e: { //e缁撴潫 c: 5, //缁撴潫鍒?/span> r: 0 //缁撴潫琛?/span> } }); if (data.length === 0 || th.length === 0) {
銆€銆€銆€銆€銆€銆€銆€console.log("缂哄皯鏁版嵁"); deferred.reject(); return; } filename = filename || 鈥樻暟鎹鍑衡€?span style="color: rgba(0, 0, 0, 1)">; filename += 鈥榑鈥?+ moment(new Date()).format(鈥榊YYYMMDDHHmmss鈥? + 鈥?xlsx鈥? // moment 鏃堕棿鎻掍欢锛堝涓嶉渶瑕侊紝鍙互鍘绘帀锛?/span> var loadedData = []; var i = 0; loadedData = loadedData.concat(th); for (i = 0; i < data.length; ++i) { var item = data[i]; var obj = []; obj.push(item.title1 || "-"); obj.push(item.title2 || "-"); obj.push(item.title3 || "-"); obj.push(item.title4 || "-"); obj.push(item.title5 || "-"); obj.push(item.title6 || "-"); loadedData.push(obj); } initFile(); function initFile() { var wb = XLSX.utils.book_new(), ws = XLSX.utils.aoa_to_sheet(loadedData); ws[鈥?merges鈥榏 = merges; // 鍚堝苟鍗曞厓鏍?/span> XLSX.utils.book_append_sheet(wb, ws, "sheet1"); XLSX.writeFile(wb, filename, { compression: true }); deferred.resolve(); } return deferred.promise(); }