700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > vue读取xls或xlsx文件

vue读取xls或xlsx文件

时间:2019-09-04 06:41:51

相关推荐

vue读取xls或xlsx文件

vue读取xls或xlsx文件

1.选择一款合适的xls插件,我选择的是xlsx

安装:

npm install xlsx

2.template

<template><div><input type="file"ref="upload"accept=".xls,.xlsx"@change="readExcel"/></div></template>

3.js

<script>import XLSX from 'xlsx'export default {name: 'Excel',data () {return {outputs: []}},methods: {readExcel (e) {let that = thisconst files = e.target.filesif (files.length < 1) {return false} else if (!/\.(xls|xlsx)$/.test(files[0].name.toLowerCase())) {this.$toast('上传文件格式不正确,请上传xls或者xlsx格式')return false}const fileReader = new FileReader()fileReader.onload = (ev) => {try {const data = ev.target.resultconst workbook = XLSX.read(data, {type: 'binary'}) // 读取数据const wsname = workbook.SheetNames[0] // 取第一张表console.log(wsname)const ws = XLSX.utils.sheet_to_json(workbook.Sheets[wsname]) // 生成json表格内容// const ws1 = XLSX.utils.sheet_to_slk(workbook.Sheets[wsname]) // 输出表格对应位置是什么值// const ws2 = XLSX.utils.sheet_to_html(workbook.Sheets[wsname]) // 生成HTML输出// const ws3 = XLSX.utils.sheet_to_csv(workbook.Sheets[wsname]) // 生成分隔符分隔值输出// const ws4 = XLSX.utils.sheet_to_formulae(workbook.Sheets[wsname]) // 生成公式列表(具有值回退)// const ws5 = XLSX.utils.sheet_to_txt(workbook.Sheets[wsname]) // 生成UTF16格式的文本that.outputs = [] // 清空接收数据for (let i = 0; i < ws.length; i++) {let sheetData = ws[i] // 对数据自行操作that.outputs.push(sheetData)}this.$refs.upload.value = ''} catch (e) {console.log(e)return false}}fileReader.readAsBinaryString(files[0])}}}</script>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。