vue項目預覽excel表格功能(file-viewer插件)
file-viewer的插件github地址如下
場景
我沒有直接使用file-viewer,是按照網(wǎng)上搜來的方法,只使用了file-viewer的預覽xlsx的功能,它里面還有預覽ppt,pdf,圖片等功能。
第一步:安裝相關依賴包(exceljs,)
npm install exceljs --save npm install '@handsontable/vue' --save npm install handsontable --save npm install 'handsontable/i18n' --save //這個依賴我沒有下成功,不過也能正常運行
第二步:新建一個xlsxView的文件夾,將file-viewer里相關預覽xlsx的代碼放進去。
第三步:新窗口打開,在預覽組件里引入并寫邏輯 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') { // 預覽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) // 調(diào)用渲染方法進行渲染 return new Promise((resolve, reject) => renderSheet(buffer, child).then(resolve).catch(reject) ) } }
總結
還有一種使用luckysheet和luckyexcel來實現(xiàn)預覽excel的方法,鏈接如下。
vue項目使用luckyexcel預覽excel表格
到此這篇關于vue項目預覽excel表格(file-viewer插件)的文章就介紹到這了,更多相關vue項目預覽excel表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Ant design vue table 單擊行選中 勾選checkbox教程
這篇文章主要介紹了Ant design vue table 單擊行選中 勾選checkbox教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10web前端Vue報錯:Uncaught?(in?promise)?TypeError:Cannot?read?
這篇文章主要給大家介紹了關于web前端Vue報錯:Uncaught?(in?promise)?TypeError:Cannot?read?properties?of?nu的解決方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-01-01vue路由守衛(wèi)及路由守衛(wèi)無限循環(huán)問題詳析
這篇文章主要給大家介紹了關于vue路由守衛(wèi)及路由守衛(wèi)無限循環(huán)問題的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用vue具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-09-09