The github address of the file-viewer plugin is as follows
Scenes
I didn't use file-viewer directly, but according to the method I searched online, I only used file-viewer's function of previewing xlsx, and it also has functions such as previewing ppt, pdf, and pictures.
Step 1: Install related dependencies (exceljs,)
npm install exceljs --save
npm install '@handsontable/vue' --save
npm install handsontable --save
npm install 'handsontable/i18n' --save //这个依赖我没有下成功,不过也能正常运行
Step 2: Create a new xlsxView folder, and put the relevant preview xlsx code in file-viewer into it.
Step 3: Open a new window, introduce and write logic in the preview component
html part
<template>
<div>
<div v-if="fileType === 'xlsx'" ref="output" />
<div v-if="fileType === 'pptx'" ref="pptRef"></div>
</div>
</template>
js part
import renderSheet from '../xlsxView'; // 引入
// mounted生命周期
mounted() {
// 从路由地址中获取fileUrl,fileType
this.fileUrl = this.$route.query.fileUrl ? this.$route.query.fileUrl : null
this.fileType = this.$route.query.fileType ? this.$route.query.fileType : null
if (this.fileUrl == null) {
this.$message({
type: 'error',
message: '文件地址无效,请刷新后重试'
})
}
// 加载文件内容
this.uploading(this.fileUrl)
}
// methods方法
methods: {
// 加载文件内容
uploading(file) {
// downloadFileXLS是接口,fileKey传的是文件地址,调接口获取文件流
downloadFileXLS({
fileKey: file}).then(res => {
if(this.fileType === 'xlsx') {
// 预览xlsx
this.displayResult(res)
} else if(this.fileType === 'pptx') {
// 预览pptx,可忽略,该篇文章不涉及pptx的预览
this.previewPptx(res)
}
})
},
displayResult(buffer) {
// 生成新的dom
const node = document.createElement('div')
// 添加孩子,防止vue实例替换dom元素
if (this.last) {
this.$refs.output.removeChild(this.last.$el)
this.last.$destroy()
}
const child = this.$refs.output.appendChild(node)
// 调用渲染方法进行渲染
return new Promise((resolve, reject) =>
renderSheet(buffer, child).then(resolve).catch(reject)
)
}
}
Summarize
There is also a way to use luckysheet and luckyexcel to achieve preview excel, the link is as follows.
Vue project uses luckyexcel to preview excel form