npm i xlsx
// HTML
<el-form-item>
<el-upload
class="upload-demo"
action=""
:on-change="handleChange"
:show-file-list="false"
:on-remove="handleRemove"
accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
:auto-upload="false"
>
<el-button
v-permission="'PA.mallassist.orderManage.batchExcel'"
type="primary"
size="mini"
>批量(导入)发货单</el-button
>
</el-upload>
<el-button
v-permission="'PA.mallassist.orderManage.batchDeliver'"
type="primary"
size="mini"
@click="bulkDelivery"
>批量发货</el-button
>
</el-form-item>
<script src="http://oss.sheetjs.com/js-xlsx/xlsx.full.min.js"></script>
import XLSX from "xlsx";
importfxx(obj) {
this.file = obj;
const _this = this;
const rABS = false; //是否将文件读取为二进制字符串
const f = this.file;
const reader = new FileReader();
FileReader.prototype.readAsBinaryString = function(f) {
let binary = "";
const rABS = false; //是否将文件读取为二进制字符串
// const pt = this;
let wb; //读取完成的数据
let outdata;
const reader = new FileReader();
reader.onload = function(e) {
const bytes = new Uint8Array(reader.result);
const length = bytes.byteLength;
for (let i = 0; i < length; i++) {
binary += String.fromCharCode(bytes[i]);
}
if (rABS) {
wb = XLSX.read(btoa(_this.fixdata(binary)), {
//手动转化
type: "base64",
});
} else {
wb = XLSX.read(binary, {
type: "binary",
});
}
outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]); //outdata就是读取excel内容之后输出的东西
_this.da = [...outdata];
const arr = [];
_this.da.map((v) => {
const obj = {};
obj.rownum = v["rownum"]; //行号
obj.expressCompany = v["expressCompany"]; //快递公司
obj.expressNum = v["expressNum"]; //快递单号
obj.orderbatchcode = v["orderbatchcode"]; //订单号
obj.productnumber = v["productnumber"]; //物料编号
obj.pnum = v["pnum"]; //数量
arr.push(obj);
});
console.log("arr", arr);
_this.tableData2 = arr;
};
reader.readAsArrayBuffer(f);
};
if (rABS) {
reader.readAsArrayBuffer(f);
} else {
reader.readAsBinaryString(f);
}
},
fixdata(data) {
//文件流转BinaryString
let o = "";
let l = 0;
const 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;
},
handleChange(file) {
this.fileTemp = file.raw;
if (this.fileTemp) {
if (
this.fileTemp.type ==
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
this.fileTemp.type == "application/vnd.ms-excel"
) {
this.importfxx(this.fileTemp);
} else {
this.$message({
type: "warning",
message: "附件格式错误,请删除后重新上传!",
});
}
} else {
this.$message({
type: "warning",
message: "请上传附件!",
});
}
},
复制粘贴就可以用了。
附上excel中内容