<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title></title> <script src="http://oss.sheetjs.com/js-xlsx/xlsx.full.min.js"></script></head><body> <input type="file" onchange="importf(this)" /> <p id="demo"></p> <script> var rABS = false; //是否將文件讀取為二進(jìn)制字符串 function importf(obj) {//導(dǎo)入 if (!obj.files) { return; } var f = obj.files[0]; { var reader = new FileReader(); var name = f.name; reader.onload = function (e) { var data = e.target.result; var wb; if (rABS) { wb = XLSX.read(data, { type: 'binary' }); } else { var arr = fixdata(data); wb = XLSX.read(btoa(arr), { type: 'base64' }); } document.getElementById("demo").innerHTML = JSON.stringify(XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]])); }; if (rABS) reader.readAsBinaryString(f); else reader.readAsArrayBuffer(f); } } function fixdata(data) { var o = "", l = 0, w = 10240; for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w))); o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w))); return o; } </script></body></html>
2.導(dǎo)出功能的實(shí)現(xiàn)
注意:導(dǎo)出其他格式文件可能會(huì)出現(xiàn)亂碼問題,如有好的解決方案請(qǐng)留言
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title></title></head><body> <button onclick="downloadExl(jsono)">導(dǎo)出</button> <script src="http://oss.sheetjs.com/js-xlsx/xlsx.full.min.js"></script> <!--調(diào)用FileSaver saveAs函數(shù)可以實(shí)現(xiàn)文件下載--> <!--<script src="http://sheetjs.com/demos/Blob.js"></script> <script src="http://sheetjs.com/demos/FileSaver.js"></script>--> <script> //如果使用 FileSaver.js 就不要同時(shí)使用以下函數(shù) function saveAs(obj, fileName) {//當(dāng)然可以自定義簡(jiǎn)單的下載文件實(shí)現(xiàn)方式 var tmpa = document.createElement("a"); tmpa.download = fileName || "下載"; tmpa.href = URL.createObjectURL(obj); //綁定a標(biāo)簽 tmpa.click(); //模擬點(diǎn)擊實(shí)現(xiàn)下載 setTimeout(function () { //延時(shí)釋放 URL.revokeObjectURL(obj); //用URL.revokeObjectURL()來釋放這個(gè)object URL }, 100); } var jsono = [{ //測(cè)試數(shù)據(jù) "保質(zhì)期臨期預(yù)警(天)": "adventLifecycle", "商品標(biāo)題": "title", "建議零售價(jià)": "defaultPrice", "高(cm)": "height", "商品描述": "Description", "保質(zhì)期禁售(天)": "lockupLifecycle", "商品名稱": "skuName", "商品簡(jiǎn)介": "brief", "寬(cm)": "width", "阿達(dá)": "asdz", "貨號(hào)": "goodsNo", "商品條碼": "skuNo", "商品品牌": "brand", "凈容積(cm^3)": "netVolume", "是否保質(zhì)期管理": "isShelfLifeMgmt", "是否串號(hào)管理": "isSNMgmt", "商品顏色": "color", "尺碼": "size", "是否批次管理": "isBatchMgmt", "商品編號(hào)": "skuCode", "商品簡(jiǎn)稱": "shortName", "毛重(g)": "grossWeight", "長(zhǎng)(cm)": "length", "英文名稱": "englishName", "凈重(g)": "netWeight", "商品分類": "categoryId", "這里超過了": 1111.0, "保質(zhì)期(天)": "expDate" }]; const wopts = { bookType: 'xlsx', bookSST: false, type: 'binary' };//這里的數(shù)據(jù)是用來定義導(dǎo)出的格式類型 // const wopts = { bookType: 'csv', bookSST: false, type: 'binary' };//ods格式 // const wopts = { bookType: 'ods', bookSST: false, type: 'binary' };//ods格式 // const wopts = { bookType: 'xlsb', bookSST: false, type: 'binary' };//xlsb格式 // const wopts = { bookType: 'fods', bookSST: false, type: 'binary' };//fods格式 // const wopts = { bookType: 'biff2', bookSST: false, type: 'binary' };//xls格式 function downloadExl(data, type) { const wb = { SheetNames: ['Sheet1'], Sheets: {}, Props: {} }; wb.Sheets['Sheet1'] = XLSX.utils.json_to_sheet(data);//通過json_to_sheet轉(zhuǎn)成單頁(yè)(Sheet)數(shù)據(jù) saveAs(new Blob([s2ab(XLSX.write(wb, wopts))], { type: "application/octet-stream" }), "這里是下載的文件名" + '.' + (wopts.bookType=="biff2"?"xls":wopts.bookType)); } function s2ab(s) { if (typeof ArrayBuffer !== 'undefined') { var buf = new ArrayBuffer(s.length); var view = new Uint8Array(buf); for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; return buf; } else { var buf = new Array(s.length); for (var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF; return buf; } } </script></body></html>
相信看了本文案例你已經(jīng)掌握了方法,更多精彩請(qǐng)關(guān)注Gxl網(wǎng)其它相關(guān)文章!
相關(guān)閱讀:
瀏覽器文件分段斷點(diǎn)上傳
如何使用s-xlsx實(shí)現(xiàn)Excel 文件導(dǎo)入和導(dǎo)出(一)
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com