我自己做的基于SheetJs做的例子
作者:互联网
可以处理对应列,跳过行头,生成JSON
<html> <head> <title>JS-XLSX Live Demo</title> <style> #drop { border: 2px dashed #bbb; -moz-border-radius: 5px; -webkit-border-radius: 5px; border-radius: 5px; padding: 25px; text-align: center; font: 20pt bold,"Vollkorn"; color: #bbb } #b64data { width: 100%; } a { text-decoration: none } button { } </style> <script type="text/javascript" src="dist/xlsx.full.min.js"></script> <script type="text/javascript" src="dist/xlsx.core.min.js"></script> <script type="text/javascript" src="dist/jquery.min.js"></script> </head> <body> <pre> <input type="file" name="xlfile" id="xlf" onchange="importf(this)"> </pre> <pre id="out"></pre> <br> <div id="demo"> </div> <div> <textarea id='debug' rows="30" cols="100"></textarea> </div> <script> /* FileReader共有4种读取方法: 1.readAsArrayBuffer(file):将文件读取为ArrayBuffer。 2.readAsBinaryString(file):将文件读取为二进制字符串 3.readAsDataURL(file):将文件读取为Data URL 4.readAsText(file, [encoding]):将文件读取为文本,encoding缺省值为'UTF-8' */ var workbook;//读取完成的数据 var rABS = false; //是否将文件读取为二进制字符串 function importf(obj) {//导入 const headers = ["A", "B", "C", "D", "E","F"]; //针对G0107中的表样,做的6列 const ranges = 4; //针对G0107中的表样,跳过的表头 if (!obj.files) { return; } var f = obj.files[0]; var reader = new FileReader(); reader.onload = function (e) { var data = e.target.result; if (rABS) { workbook = XLSX.read(btoa(fixdata(data)), {//手动转化 type: 'base64' }); } else { workbook = XLSX.read(data, { type: 'binary' }); } var out_json; for (var sheetNum = 0; sheetNum < Number(workbook.SheetNames.length); sheetNum++) { var first_sheet_name = workbook.SheetNames[sheetNum]; //var json = XLSX.utils.sheet_to_json(workbook.Sheets[first_sheet_name]); var json = XLSX.utils.sheet_to_json(workbook.Sheets[first_sheet_name], { header: headers, raw: true, defval:''} ); debugger; //console.log(json); out_json = getOutput(json, 'json'); $('#debug').val(out_json); } }; if (rABS) { reader.readAsArrayBuffer(f); } else { reader.readAsBinaryString(f); } } function fixdata(data) { //文件流转BinaryString 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; } function getOutput(obj, type) { switch (type) { case 'json': return JSON.stringify(obj); break; case 'obj': return obj; break; default: return JSON.stringify(obj); } } </script> </body> </html>
标签:基于,obj,读取,SheetJs,workbook,json,例子,var,data 来源: https://www.cnblogs.com/alexgl2008/p/15845736.html