vue項目預(yù)覽excel表格功能(file-viewer插件)
file-viewer的插件github地址如下
場景
我沒有直接使用file-viewer,是按照網(wǎng)上搜來的方法,只使用了file-viewer的預(yù)覽xlsx的功能,它里面還有預(yù)覽ppt,pdf,圖片等功能。

第一步:安裝相關(guān)依賴包(exceljs,)
npm install exceljs --save npm install '@handsontable/vue' --save npm install handsontable --save npm install 'handsontable/i18n' --save //這個依賴我沒有下成功,不過也能正常運行
第二步:新建一個xlsxView的文件夾,將file-viewer里相關(guān)預(yù)覽xlsx的代碼放進去。


第三步:新窗口打開,在預(yù)覽組件里引入并寫邏輯 html部分
<template>
<div>
<div v-if="fileType === 'xlsx'" ref="output" />
<div v-if="fileType === 'pptx'" ref="pptRef"></div>
</div>
</template>js部分
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: '文件地址無效,請刷新后重試'
})
}
// 加載文件內(nèi)容
this.uploading(this.fileUrl)
}
// methods方法
methods: {
// 加載文件內(nèi)容
uploading(file) {
// downloadFileXLS是接口,fileKey傳的是文件地址,調(diào)接口獲取文件流
downloadFileXLS({fileKey: file}).then(res => {
if(this.fileType === 'xlsx') {
// 預(yù)覽xlsx
this.displayResult(res)
} else if(this.fileType === 'pptx') {
// 預(yù)覽pptx,可忽略,該篇文章不涉及pptx的預(yù)覽
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)
// 調(diào)用渲染方法進行渲染
return new Promise((resolve, reject) =>
renderSheet(buffer, child).then(resolve).catch(reject)
)
}
}總結(jié)
還有一種使用luckysheet和luckyexcel來實現(xiàn)預(yù)覽excel的方法,鏈接如下。
vue項目使用luckyexcel預(yù)覽excel表格
到此這篇關(guān)于vue項目預(yù)覽excel表格(file-viewer插件)的文章就介紹到這了,更多相關(guān)vue項目預(yù)覽excel表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue2.0 + element UI 中 el-table 數(shù)據(jù)導(dǎo)出Excel的方法
- vue項目使用luckyexcel預(yù)覽excel表格功能(心路歷程)
- Vue3實現(xiàn)動態(tài)導(dǎo)入Excel表格數(shù)據(jù)的方法詳解
- 使用SpringBoot+EasyExcel+Vue實現(xiàn)excel表格的導(dǎo)入和導(dǎo)出詳解
- vue使用axios導(dǎo)出后臺返回的文件流為excel表格詳解
- vue項目中常見的三種文件類型在線預(yù)覽實現(xiàn)(pdf/word/excel表格)
- vue中el-table前端如何導(dǎo)出excel數(shù)據(jù)表格
相關(guān)文章
Vue組織架構(gòu)樹圖組件vue-org-tree的使用解析
這篇文章主要介紹了Vue組織架構(gòu)樹圖組件vue-org-tree的使用解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
Ant design vue table 單擊行選中 勾選checkbox教程
這篇文章主要介紹了Ant design vue table 單擊行選中 勾選checkbox教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
web前端Vue報錯:Uncaught?(in?promise)?TypeError:Cannot?read?
這篇文章主要給大家介紹了關(guān)于web前端Vue報錯:Uncaught?(in?promise)?TypeError:Cannot?read?properties?of?nu的解決方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-01-01
vue路由守衛(wèi)及路由守衛(wèi)無限循環(huán)問題詳析
這篇文章主要給大家介紹了關(guān)于vue路由守衛(wèi)及路由守衛(wèi)無限循環(huán)問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

