Vue移動端實現(xiàn)pdf/excel/圖片在線預(yù)覽
vue移動端實現(xiàn)pdf、excel、圖片在線預(yù)覽
一、首先預(yù)覽pdf
安裝vue-pdf,預(yù)覽excel安裝xlsx,預(yù)覽圖片是用的vant
npm install --save vue-pdf npm install xlsx --save npm install vant --save
二、父頁面代碼
<template> <button @click="seeFile"> </button> </template> //引入子頁面,也就是預(yù)覽的界面 import Views from './previewFile' export default { name: 'FileList', components: { Views }, data() { return { url:'',//要預(yù)覽的文件地址 src: '', // 傳參 type: '', // 附件類型 } }, methods: { //item參數(shù)為文件地址 seeFile(this.url) { //文件預(yù)覽 if (item.fileName.toLowerCase().includes('jpg') || item.fileName.toLowerCase().includes('png')|| item.fileName.toLowerCase().includes('jpeg')) { this.src = fileAbsPath this.type = 'img' } else if (item.fileName.toLowerCase().includes('pdf')) { this.src = fileAbsPath this.type = 'pdf' this.$refs.vViews.pageNum = 1 } else if (item.fileName.toLowerCase().includes('xls') || item.fileName.toLowerCase().includes('xlsx')) { this.src = fileAbsPath this.type = 'excel' } this.$nextTick(() => { this.$refs.Views.isShow = true this.$refs.Views.showFile(this.src) }) }, } }
三、子頁面previewFile(預(yù)覽頁面)代碼
<template> <div class="viewBox" v-if="isShow"> <!-- pdf預(yù)覽 --> <div class="wrap-pdf" v-if="type === 'pdf'"> <div class="pdf"> <div class="pdf-tab"> <div class="btn-def" @click.stop="prePage"><span>上一頁</span> </div> <div class="btn-def" @click.stop="nextPage"><span>下一頁</span> </div> </div> <div style="text-align: center;">{{pageNum}}/{{pageTotalNum}}</div> <div class="any-scroll-view"> <div ref="body"> <pdf id="pdfPreview" ref="pdf" :src="pdfSrc" :page="pageNum" :rotate="pageRotate" @password="password" @progress="loadedRatio = $event" @page-loaded="pageLoaded($event)" @num-pages="pageTotalNum=$event" @error="pdfError($event)" @link-clicked="page = $event"> </pdf> </div> </div> </div> <!-- <pdf v-for="item in numPages" :key="item" :src="pdfSrc" :page="item"/> --> </div> <!-- 表格組件 --> <div class="table-box" v-if="type === 'excel'"> <van-tabs class="table-tab" v-if="sheetNames.length" title-active-color="#07AC7F" color="#07AC7F" @click="clickTab"> <van-tab v-for="(item, index) in sheetNames" :key="index" :name="item" :title="item"></van-tab> </van-tabs> <div class="tableBox" ref="excPreview"></div> </div> <!-- 關(guān)閉按鈕 --> <van-icon class="closeBtn" name="cross" @click="isShow = false" /> </div> </template> <script> import { ImagePreview } from 'vant' import pdf from 'vue-pdf' import XLSX from 'xlsx' export default { name: 'PreviewFile', components: { pdf }, props: { datas: {}, type: {} }, data() { return { isShow: false, numPages: 1, pdfSrc: '', sheetNames: [], pageNum: 1, wsObj: {}, pageTotalNum:1, pageRotate:0, // 加載進(jìn)度 loadedRatio:0, curPageNum:0, } }, methods: { showFile(newVal) { console.log('----', newVal) if (this.type === 'img') { const that = this ImagePreview({ images: [newVal], onClose() { that.isShow = false } }) } else if (this.type === 'pdf') { this.pdfSrc = pdf.createLoadingTask(newVal) } else if (this.type === 'excel') { var xhr = new XMLHttpRequest() xhr.open('get', newVal, true) xhr.responseType = 'arraybuffer' let _this = this xhr.onload = function (e) { let that=this; var binary = ""; if (xhr.status === 200) { var bytes = new Uint8Array(xhr.response) var length = bytes.byteLength; for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); } var wb = XLSX.read(binary, { type: "binary" }); var wsname = wb.SheetNames[0]; var ws = wb.Sheets[wsname]; _this.sheetNames = [...wb.SheetNames] // 數(shù)組 _this.wsObj = { ...wb.Sheets } _this.changeExcel(_this.sheetNames[0]) } } xhr.send() } }, clickTab(name) { this.changeExcel(name) }, prePage(){ var p = this.pageNum p = p>1?p-1:this.pageTotalNum this.pageNum = p }, nextPage(){ var p = this.pageNum p = p<this.pageTotalNum?p+1:1 this.pageNum = p }, password(updatePassword, reason) { updatePassword(prompt('password is "123456"')) }, pageLoaded(e){ this.curPageNum = e }, pdfError(error){ console.error(error) }, changeExcel(item) { // 獲取當(dāng)前選中表格對象 const ws = this.wsObj[item] console.log(ws); const keyArr = Object.keys(ws) || [] const HTML = keyArr.length > 1 ? XLSX.utils.sheet_to_html(ws) : '<html><head><meta charset="utf-8"/>' + '<title>SheetJS Table Export</title></head><body><div class="myTable">暫無數(shù)據(jù)</div></body>' + '</html>' this.$nextTick(() => { this.$refs.excPreview.innerHTML = HTML // 獲取表格dom元素 const tables = this.$refs.excPreview.children[2] // 添加完畢后 通過空格將數(shù)組組裝為字符串 tables.className = 'myTable' }) } } } </script> <style lang="scss" scoped> .excel-container { width: 100%; } table { display: table; border-collapse: collapse; box-sizing: border-box; border: 1px solid #929292; width: auto;height: auto;color: #333333;// 合并邊框 th,tr{ white-space: nowrap;overflow: hidden;text-overflow: ellipsis;background: #ffffff;padding: 10px;border:1px solid #929292; td{ font-weight: normal; text-align: center; border:1px solid #929292; } } } .tableBox {width: 100vw;height: calc(100vh - 44px);overflow: auto; }// 表格邊框 .pdf-tab { display: -ms-flexbox; display: flex; -ms-flex-wrap: wrap; flex-wrap: wrap; padding: 0 .4rem; -ms-flex-pack: justify; justify-content: space-between; } .pdf-tab .btn-def { border-radius: .2rem; font-size: 0.5rem; // height: 40px; margin-top: 40px; // width: 6.4rem; text-align: center; // line-height: 1.93333rem; background: #409eff; color: #fff; // margin-bottom: 1.26667rem; } .pdf-total { text-align: center; font-size: 1.45333rem; } .pdf-process, .pdf-total { text-align: center; font-size: 1.45333rem; } .pdf-num { margin-bottom: 1.2rem; } .pdf-box, .word-box, .table-box, .txt-box { width: 100vw; height: 100vh; } .viewBox { width: 100%; height: 100%; overflow: hidden; background: #ffffff; position: fixed; top: 0; left: 0; z-index: 2000; .closeBtn { position: absolute; top: 10px; right: 10px; z-index: 10; font-size: 24px; } } </style>
缺點:excel預(yù)覽顯示不了邊框,word預(yù)覽還沒有找到好的解決方案!!!有找到word移動端預(yù)覽和解決了excel預(yù)覽邊框顯示的小伙伴可以私聊我。
效果圖如下:
以上就是Vue移動端實現(xiàn)pdf/excel/圖片在線預(yù)覽的詳細(xì)內(nèi)容,更多關(guān)于Vue在線預(yù)覽的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
前端使用print.js實現(xiàn)打印功能(基于vue)
最近新接了一個需求,想要在前端實現(xiàn)打印功能,下面這篇文章主要給大家介紹了關(guān)于前端使用print.js實現(xiàn)打印功能(基于vue)的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05vue2和el-input無法修改和寫入并且不報錯的解決方案
這篇文章主要介紹了vue2和el-input無法修改和寫入并且不報錯的解決方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-07-07淺談vue3中effect與computed的親密關(guān)系
這篇文章主要介紹了淺談vue3中effect與computed的親密關(guān)系,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10