需求: 从excel文件中解析里面的内容
html
1、使用插件xlsx.full.min.js,地址:https://unpkg.com/xlsx/dist/xlsx.full.min.js
实例:
js
<script src="https://unpkg.com/xlsx/dist/xlsx.full.min.js"></script>
<input type="file" id="input_btn" />
<div id="excel_show"></div>
<script>
const inputbtn = document.getElementById('input_btn');
const excelshow = document.getElementById('excel_show');
inputbtn.addEventListener('change', function(e) {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = function(e) {
const data = new Uint8Array(e.target.result);
const workbook = XLSX.read(data, { type: 'array' });//表格对象
// workbook.SheetNames; //获取到所有表格
const firstSheetName = workbook.SheetNames[0]; //取第一张表
const worksheet = workbook.Sheets[firstSheetName];
//1、获得 **.xlsx文件 的json格式内容
//const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
//excelshow.innerHTML = JSON.stringify(jsonData);
//2、将表格数据转换为html代码
let html = XLSX.utils.sheet_to_html(worksheet);
console.log('html====',html)
//将html代码渲染到指定容器
excelshow.innerHTML = html;
};
reader.readAsArrayBuffer(file);
});
</script>
例如:
返回展示的结果:
[["序号","名字","id","数据"],[1,"aa",111,11111],[2,"bb",222,22222],[3,"cc",333,33333]]
即: