依赖的插件:xlsx,地址如下:
https://github.com/SheetJS/js-xlsx/blob/master/dist/xlsx.core.min.js
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>读取Excel表格的内容</title></head><body>
<input type="file" id="excel-file">
<div class="content"></div>
<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript" class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="xlsx.core.min.js"></script>
<script type="text/javascript">
$('#excel-file').change(function(e) { var files = e.target.files; var fileReader = new FileReader();
fileReader. = function(ev) { try { var data = ev.target.result,
workbook = XLSX.read(data, { type: 'binary'
}), // 以二进制流方式读取得到整份excel表格对象
persons = []; // 存储获取到的数据
} catch (e) { console.log('文件类型不正确'); return;
} // 表格的表格范围,可用于判断表头是否数量是否正确
var fromTo = ''; // 遍历每张表读取
for (var sheet in workbook.Sheets) { if (workbook.Sheets.hasOwnProperty(sheet)) {
fromTo = workbook.Sheets[sheet]['!ref']; console.log(fromTo);
persons = persons.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet])); // break; // 如果只取第一张表,就取消注释这行
$('.content').text(JSON.stringify(persons))
}; // 以二进制方式打开文件
fileReader.readAsBinaryString(files[0]);
}); </script></body></html>
作者:椰果粒
链接:https://www.jianshu.com/p/37b163bea324
点击查看更多内容