chooseFile() {
let _this = this;
let sn = [];
uni.chooseFile({
count: 6, //默认100
extension: ['.xlsx','.xls'],
success: (res) => {
console.log(res);
console.log(res.tempFiles[0].name);
this.name = res.tempFiles[0].name;
// this.uploadFile(JSON.stringify(res.tempFilePaths))
let fileName = res.tempFiles[0]
let reader = new FileReader()
//启动函数
reader.readAsBinaryString(fileName)
// //onload在文件被读取时自动触发
reader.onload = function(e) {
// //workbook存放excel的所有基本信息
let workbook = XLSX.read(e.target.result, {
type: 'binary'
})
// //定义sheetList中存放excel表格的sheet表,就是最下方的tab
let sheetList = workbook.SheetNames
// //存放json数组格式的表格数据
let resultJson = []
// //存放字符串数组格式的表格数据
let resultArr = []
let sheet2JSONOpts = {
/** Default value for null/undefined values */
defval: '' //给defval赋值为空的字符串
}
sheetList.forEach((y) => {
let worksheet = workbook.Sheets[y]
//多传一个sheet2JSONOpts参数是想为空的列表默认传参为''
let excelData = XLSX.utils.sheet_to_json(workbook.Sheets[y],
sheet2JSONOpts) //每行以json数组形式输出
let excelL = XLSX.utils.sheet_to_formulae(workbook.Sheets[
y]) //生成一个excel的公式列表,可以获取到表格所有信息
let arr = []; //为了实现多表
console.log("每行以json数组形式输出", excelData)
excelData.map((item, index) => {
console.log(_this.sn, '9999')
if (item.sn) {
sn.push(item.sn)
}
if (item.isbn && index == 0) {
_this.isbn = item.isbn
}
})
if (sn.length > 0) {
_this.isshowbnt = true;
_this.sn = sn;
}else{
uni.showToast({
title: '请确认sn号或isbn是否正确',
icon: 'none',
duration: 1000
});
}
console.log('第一列结果', _this.sn, _this.isbn);
});
};
}
});
},
前端实现导入Excel并解析
最新推荐文章于 2024-02-25 14:17:25 发布