js-xlsx 中文乱码
想利用vue读取xlsx文件内容,通过度娘处得到答案,如下:
<template>
<span>
<input
class="input-file"
type="file"
@change="exportData"
accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
/>
</span>
</template>
<script>
import XLSX from 'xlsx'
export default {
methods: {
exportData (event) {
if (!event.currentTarget.files.length) {
return
}
// const that = this
// 拿取文件对象
var f = event.currentTarget.files[0]
// 用FileReader来读取
var reader = new FileReader()
// 重写FileReader上的readAsBinaryString方法
FileReader.prototype.readAsBinaryString = function (f) {
var binary = ''
var wb // 读取完成的数据
var outdata // 你需要的数据
var reader = new FileReader()
reader.onload = () => {
// 读取成Uint8Array,再转换为Unicode编码(Unicode占两个字节)
var bytes = new Uint8Array(reader.result)
console.log('bytes=', bytes)
var length = bytes.byteLength
for (var i = 0; i < length; i++) {
binary += String.fromCharCode(bytes[i])
}
// 接下来就是xlsx了,具体可看api
wb = XLSX.read(binary, {
type: 'binary'
})
console.log('wb = ' + wb)
outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]])
// 自定义方法向父组件传递数据
console.log('outdata = ' + JSON.stringify(outdata))
}
reader.readAsArrayBuffer(f)
}
reader.readAsBinaryString(f)
}
}
}
</script>
但是经过测试,调试器得到的结果中文却又乱码,如图:
bytes= Uint8Array(869) [206, 202, 204, 226, 183, 195, 206, 202, 205, 248, 214, 183, 44, 188, 198, 187, 174, 44, 181, 165, 212, 170, 44, 185, 216, 188, 252, 180, 202, 44, 194, 228, 181, 216, 210, 179, 214, 202, 193, 191, 44, 184, 252, 208, 194, 202, 177, 188, 228, 44, 215, 180, 204, 172, 44, 212, 164, 185, 192, 191, 201, 196, 220, 208, 194, 212, 246, 214, 220, 190, 249, 213, 185, 207, 214, 193, 191, 44, 214, 184, 181, 188, 189, 168, 210, 233, 44, 185, 216, 188, 252, 180, 202, 210, 198, 182, 175, 183, 195, 206, …]
App.vue?234e:44 wb = [object Object]
App.vue?234e:47 outdata = [{"ÎÊÌâ·ÃÎÊÍøÖ·":"https://aisite.wejianzhan.com/site/shengtingcq.com/d01b9323-781a-42ae-987e-aec0312a6fdf","¼Æ»®":"033-²ðǨ-µØÇø","µ¥Ôª":"µØÇø-·¿ÎݲðǨ²¹³¥","¹Ø¼ü´Ê":"ÖØÇì·¿ÎݲðǨ²¹³¥","ÂäµØÒ³ÖÊÁ¿":"ÎÞ·¨Ê¶±ð","¸üÐÂʱ¼ä":44130.472025462965,"״̬":"Õï¶ÏÍê³É","Ô¤¹À¿ÉÄÜÐÂÔöÖܾùÕ¹ÏÖÁ¿":"10~20","Ö¸µ¼½¨Òé":"##µ±Ç°¹Ø¼ü´ÊÓ롾·¿ÎݲðǨÓ뻧¿Ú¡¢Ð²ðǨ¹æ¶¨¡¢²ðǨÎļþ¡¿µÈ´Ê¹²ÓôËÂäµØÒ³£¬Îª¼õÉÙ¶ÔÆäËü´ÊÓ°ÏìÇë½÷É÷Ð޸ģ¬½¨ÒéÐÂÔöÂäµØÒ³##\n","¹Ø¼ü´ÊÒƶ¯·ÃÎÊÍøÖ·":"https://aisite.wejianzhan.com/site/shengtingcq.com/d01b9323-781a-42ae-987e-aec0312a6fdf"},{"ÎÊÌâ·ÃÎÊÍøÖ·":"https://aisite.wejianzhan.com/site/shengtingcq.com/d01b9323-781a-42ae-987e-aec0312a6fdf","¼Æ»®":"031-²ðǨ-ÔÓÁ÷Á¿","µ¥Ôª":666,"¹Ø¼ü´Ê":"ºþ±±Î人²ðǨ","ÂäµØÒ³ÖÊÁ¿":"ÎÞ·¨Ê¶±ð","¸üÐÂʱ¼ä":44130.472025462965,"״̬":"Õï¶ÏÍê³É","Ô¤¹À¿ÉÄÜÐÂÔöÖܾùÕ¹ÏÖÁ¿":"10~20","Ö¸µ¼½¨Òé":"##µ±Ç°¹Ø¼ü´ÊÓ롾·¿ÎݲðǨÓ뻧¿Ú¡¢Ð²ðǨ¹æ¶¨¡¢²ðǨÎļþ¡¿µÈ´Ê¹²ÓôËÂäµØÒ³£¬Îª¼õÉÙ¶ÔÆäËü´ÊÓ°ÏìÇë½÷É÷Ð޸ģ¬½¨ÒéÐÂÔöÂäµØÒ³##\n","¹Ø¼ü´ÊÒƶ¯·ÃÎÊÍøÖ·":"https://aisite.wejianzhan.com/site/shengtingcq.com/d01b9323-781a-42ae-987e-aec0312a6fdf"}]
请问如何解决呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
改为
即可解决