我是借鉴使用h5的示例代码,完成这个功能的,先将h5代码奉上:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="http://oss.sheetjs.com/js-xlsx/xlsx.full.min.js"></script>
</head>
<body>
<input type="file"onchange="importf(this)" />
<script>
var wb;//读取完成的数据
var rABS = false; //是否将文件读取为二进制字符串
function importf(obj) {//导入
// console.log(obj)
if(!obj.files) {
return;
}
var f = obj.files[0];
// console.log(obj)
console.log(f)
var reader = new FileReader();
reader.onload = function(e) {
var data = e.target.result;
if(rABS) {
wb = XLSX.read(btoa(fixdata(data)), {//手动转化
type: 'base64'
});
} else {
wb = XLSX.read(data, {
type: 'binary'
});
}
//wb.SheetNames[0]是获取Sheets中第一个Sheet的名字
//wb.Sheets[Sheet名]获取第一个Sheet的数据
// document.getElementById("demo").innerHTML= JSON.stringify( XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]) );
let data1 = JSON.stringify( XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]) );
console.log(data1)
};
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;
}
</script>
</body>
</html>
复制该代码,即可将上传的Excel文件中的数据打印在控制台中,如果将rABS 改为true,则将文件读取为二进制字符串,关键点:
使用XLSX.read(data, {type: 'binary'});
方法读取fileReader中的结果,然后再使用XLSX.utils.sheet_to_json()方法将数据转换成数组字符串
以下为vue代码:
<el-upload
class="avatar-uploader"
action=""
:show-file-list="false"
:on-success="handleAvatarSuccessA"
:on-change="importf"
>
<n-button dark title="导入" @click="submitUpload" src="static/export.png"></n-button>
</el-upload>
data(){
return{
rABS:false,
wb:''
}
}
importf(file){
this.file = file;
const isLt5M = file.size/1024/1024 < 5;
const extention = file.name.split('.')[1] === 'xls';
const extention2 = file.name.split('.')[1] === 'xlsx';
// console.log(extention,extention2,isLt5M)
if(!extention&&!extention2){
this.$message.warning('长传的文件只能是Excel')
return false
}
if(!isLt5M){
this.$message.warning('文件大小不能超过5MB')
return false
}
let data1 = XLSX.utils.sheet_to_json(file.name)
var f = file.raw;
var that = this;
var reader = new FileReader();
reader.onload = function(e) {
// console.log(e)
let wb = that.wb
var data = e.target.result;
if(that.rABS) {
wb = XLSX.read(btoa(that.fixdata(data)), {//手动转化
type: 'base64'
});
} else {
wb = XLSX.read(data, {
type: 'binary'
});
}
// wb.SheetNames[0]是获取Sheets中第一个Sheet的名字
// wb.Sheets[Sheet名]获取第一个Sheet的数据
// document.getElementById("demo").innerHTML= JSON.stringify( XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]) );
let data1 = JSON.stringify( XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]) );
console.log(data1)
};
if(this.rABS) {
reader.readAsArrayBuffer(f);
} else {
reader.readAsBinaryString(f);
}
},
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;
},
网友评论