2021-07-21 Excel预览 :blob文件转为数组,预览表格

话不多说,直接上代码!!

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>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值