我尝试使用 vue.js 读取 excel 文件,但是一旦我读取文件,您就可以像 5 gb ram 一样开始使用内存,并且我的 excel 文件很小,请帮助需要将文件转换为 json vue 方法处理 excel 文件我尝试了我在文档中看到的所有类型选项,但都向我发送了不同的错误我在这里看到了类似的问题,但尝试时仍然无法解决这个问题
- base64:“TypeError:input.replace 不是函数”
- 二进制:“TypeError:x.charCodeAt 不是函数”
- 字符串:“类型错误:data.match 不是函数”
- 数组:是导致内存进入 5gb 的数组
此外,当创建 reader.onload 时尝试使用文档中提供的新文件阅读器时,该函数从未运行。实际的模板我尝试了两件事。当我使用缓冲区时,它似乎可以工作,但所有函数都返回空数组。就像文件是空的,但它不是
两种方式都做了同样的事情
<v-file-input
v-on:change="displayFile($event)"
v-model="file">
</v-file-input>
<input type="file" name="xlfile" id="xlf" v-on:change="displayFile($event)" />
displayFile: function (event) {
// console.log(event.target.files[0])
// const file = event.target.files[0]
// const workbook = XLSX.read(file, {
// type: 'string'
// })
// console.log(workbook, workbook.SheetNames)
// const res = XLSX.read(file)
// console.log(res)
// const res = XLSX.read(this.file)
// console.log(res)
console.log(this.file)
this.file.text().then(text => {
const fileType = this.file.type
console.log(fileType)
// this.PropceseMethod(this.file, fileType)
})
const reader = new FileReader()
reader.onload = (data) => {
console.log('HERE')
console.log(data)
const workbook = XLSX.read(data, {
type: 'buffer'
})
console.log(workbook)
workbook.SheetNames.forEach(function (sheetName) {
console.log(sheetName)
console.log(workbook.Sheets[sheetName])
// Here is your object
const XLRowObject = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName])
console.log(XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]))
console.log(XLRowObject)
const jsonObject = JSON.stringify(XLRowObject)
console.log(jsonObject)
})
}
reader.onerror = function (ex) {
console.log(ex)
}
reader.readAsText(this.file)
}