Vue移動(dòng)端實(shí)現(xiàn)pdf/excel/圖片在線預(yù)覽
vue移動(dòng)端實(shí)現(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
二、父頁(yè)面代碼
<template>
<button @click="seeFile">
</button>
</template>
//引入子頁(yè)面,也就是預(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)
})
},
}
}
三、子頁(yè)面previewFile(預(yù)覽頁(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>上一頁(yè)</span>
</div>
<div
class="btn-def"
@click.stop="nextPage"><span>下一頁(yè)</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)前選中表格對(duì)象
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">暫無(wú)數(shù)據(jù)</div></body>' +
'</html>'
this.$nextTick(() => {
this.$refs.excPreview.innerHTML = HTML
// 獲取表格dom元素
const tables = this.$refs.excPreview.children[2]
// 添加完畢后 通過(guò)空格將數(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>缺點(diǎn):excel預(yù)覽顯示不了邊框,word預(yù)覽還沒(méi)有找到好的解決方案!!!有找到word移動(dòng)端預(yù)覽和解決了excel預(yù)覽邊框顯示的小伙伴可以私聊我。
效果圖如下:


以上就是Vue移動(dòng)端實(shí)現(xiàn)pdf/excel/圖片在線預(yù)覽的詳細(xì)內(nèi)容,更多關(guān)于Vue在線預(yù)覽的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue側(cè)邊欄動(dòng)態(tài)生成下級(jí)菜單的方法
今天小編就為大家分享一篇vue側(cè)邊欄動(dòng)態(tài)生成下級(jí)菜單的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vue3輸入單號(hào)和張數(shù)如何自動(dòng)生成連號(hào)的單號(hào)
最近遇到這樣的需求輸入連號(hào)事件,需要在表格中輸入物流單號(hào),物流號(hào)碼,生成的數(shù)量,名稱,點(diǎn)擊確定自動(dòng)生成固定數(shù)量的連號(hào)物流單號(hào),本文重點(diǎn)介紹vue3輸入單號(hào)和張數(shù),自動(dòng)生成連號(hào)的單號(hào),感興趣的朋友一起看看吧2024-02-02
前端使用print.js實(shí)現(xiàn)打印功能(基于vue)
最近新接了一個(gè)需求,想要在前端實(shí)現(xiàn)打印功能,下面這篇文章主要給大家介紹了關(guān)于前端使用print.js實(shí)現(xiàn)打印功能(基于vue)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
vue3.0實(shí)踐之寫tsx語(yǔ)法實(shí)例
很久不寫博客了,最近在使用ts和tsx開(kāi)發(fā)vue類項(xiàng)目,網(wǎng)上資料比較少,順便記錄一下方便同樣開(kāi)發(fā)的人互相學(xué)習(xí)共同進(jìn)步,下面這篇文章主要給大家介紹了關(guān)于vue3.0實(shí)踐之寫tsx語(yǔ)法的相關(guān)資料,需要的朋友可以參考下2022-07-07
vue2和el-input無(wú)法修改和寫入并且不報(bào)錯(cuò)的解決方案
這篇文章主要介紹了vue2和el-input無(wú)法修改和寫入并且不報(bào)錯(cuò)的解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-07-07
淺談vue3中effect與computed的親密關(guān)系
這篇文章主要介紹了淺談vue3中effect與computed的親密關(guān)系,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

