記錄經驗:
以vue爲例~~~
首先借助大佬的東西:https://github.com/SheetJS/js-xlsx
1.安裝xlsx
npm install xlsx
2.導入爲數組格式
import XLSX from "xlsx";
//拿到excel的file,不同框架的方法不一樣,此處的element的上傳組件拿到的,
<el-upload
ref="upload"
action="/wm/upload"
:show-file-list="false"
:on-change="readExcel"
:auto-upload="false"
style="display: inline-block"
>
轉換數組
</el-upload>
// 讀取excel
readExcel(file){ //此處接受的file,爲文件上傳的file
var reader = new FileReader();
//以二進制方式讀取文件
reader.readAsBinaryString(file.raw);
reader.onload = (e) => {
//獲取文件數據
const data = e.target.result;//e.target.value
//XLSX讀取文件
const wb = XLSX.read(data, { type: "binary" });
//獲取第一張表
const wsname = wb.SheetNames[0];
const ws = wb.Sheets[wsname];
var result = XLSX.utils.sheet_to_json(ws, { header: 1 });
this.dataDetail(result)
return result;
};
},
結果: