Excel预览
话不多说,直接上代码!!
js
代码片
.
const _this = this;
const reader = new FileReader();
_this.tableData = [];
_this.columns = [];
reader.readAsArrayBuffer(this.uploadFile); //需要传blob类型
reader.onload = function() {
const buffer = reader.result;
const bytes = new Uint8Array(buffer);
const length = bytes.byteLength;
let binary = '';
for (let i = 0; i < length; i++) {
binary += String.fromCharCode(bytes[i]);
}
const XLSX = require('xlsx');
const wb = XLSX.read(binary, {
type: 'binary',
});
const outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
// console.log(outdata),此为取到的excel中内容,然后就可以自己改数据,画页面啦~
//const tableheader = outdata[0]; 你可以看一下这个数组中呢一个是你想要的excel头部
const tableheader = outdata[1];
for (const val in tableheader) {
_this.columns.push({
title: val,
dataIndex: val,
key: val,
});
}
outdata.forEach((v, i) => {
v = { ...v, key: i };
});
_this.tableData = outdata;
_this.preViewVisible = true;
VUE
代码片
.
<a-modal
class="modal"
width="80%"
:title="null"
:centered="true"
:closable="true"
:visible="preViewVisible"
@cancel="preViewVisible = false"
>
<a-table :columns="columns" :data-source="tableData" style="margin-top:45px" :scroll="{ x: 1000 }">
<a slot="name" slot-scope="text">{{ text }}</a>
</a-table>
<template #footer>
<a-button class="w76 cancel-btn" shape="round" @click="preViewVisible = false">
关闭
</a-button>
</template>
</a-modal>