Vue项目要在内网环境进行预览,pdf可以直接使用浏览器自带的方式进行预览,但是word和excel需要借助插件,预览本地文件,或者是内网文件,可以使用以下方法进行预览:

Word: docx-preview

Excel: SheetJS

PDF:vue-pdf

txt: iframe

word预览

安装依赖

1
2
npm i docx-preview@0.1.4
npm i jszip

预览在线地址文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<template>
<div class="home">
<div ref="file"></div>
</div>
</template>

<script>
import axios from 'axios'
const docx = require('docx-preview')

// 如果 docx-preview 版本比较新需要 es6 导出
// import { renderAsync } from 'docx-preview'

window.JSZip = require('jszip')
export default {
mounted(){
axios({
method: 'get',
responseType: 'blob', // 设置响应文件格式
url: '/docx',
}).then(({data}) => {
docx.renderAsync(data,this.$refs.file) // 渲染到页面预览
})
}
}
</script>

预览本地文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<template>
<div class="my-component" ref="preview">
<input type="file" @change="preview" ref="file">
</div>
</template>

<script>
const docx = require('docx-preview');
window.JSZip = require('jszip')
export default {
methods: {
preview(e){
docx.renderAsync(this.$refs.file.files[0],this.$refs.preview) // 渲染到页面预览
}
}
}
</script>

<style>
.my-component{
width: 100%;
height: 90vh;
border: 1px solid #000;
}
</style>

excel预览

安装依赖

1
npm install --save xlsx

excel.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<template>
<div id="table">
<el-table :data="tableData" style="width: 100%">
<el-table-column
v-for="(key,index) in tableHeader"
:key="index"
:prop="key"
:label="key"
></el-table-column>
</el-table>
</div>
</template>

<script>
import XLSX from 'xlsx'
export default {
data() {
return {
tableData: [],
tableHeader: [],
workbook: {},
excelURL: '',
}
},
mounted() {
this.excelURL = this.$route.query.excelURL
this.readWorkbookFromRemoteFile(this.excelURL)
},
methods: {
readWorkbookFromRemoteFile(url) {
var xhr = new XMLHttpRequest()
xhr.open('get', url, true)
xhr.responseType = 'arraybuffer'
let _this = this
xhr.onload = function(e) {
if (xhr.status === 200) {
var data = new Uint8Array(xhr.response)
var workbook = XLSX.read(data, { type: 'array' })

var sheetNames = workbook.SheetNames; // 工作表名称集合
_this.workbook = workbook;
_this.getTable(sheetNames[0])
}
};
xhr.send()
},
getTable(sheetName) {
var worksheet = this.workbook.Sheets[sheetName]
this.tableData = XLSX.utils.sheet_to_json(worksheet)

this.tableHeader = []
if (this.tableData.length > 0) {
for (var i in this.tableData[0]) {
this.tableHeader.push(i)
}
}
}
}
}
</script>

<style scoped>
#table {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #2c3e50;
margin-top: 60px;
border: 1px solid #ebebeb;
padding: 20px;
width: 80%;
margin: 20px auto;
border-shadow: 0 0 8px 0 rgba(232, 237, 250, 0.6), 0 2px 4px 0 rgba(232, 237, 250, 0.5);
border-radius: 10px;
overflow: scroll;
height: 100%;

.tab {
margin: 0 0 20px 0;
display: flex;
flex-direction: row;
}
}
</style>

<style scoped>
.is-active {
background-color: #409eff;
}
span {
background-color: red;
}
</style>

pdf预览

安装依赖

1
npm install --save vue-pdf

pdf.vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<template>
<div>
<pdf ref="pdf" :src="pdfUrl" style="width:100%;" />
</div>
</template>
<script>
import pdf from 'vue-pdf'
export default {
data() {
return: {
pdfUrl: '',
}
}
created() {
const path = 'test.pdf'// 你获取到的pdf路径
// pdf.createLoadingTask解决文件件跨域问题
this.pdfUrl = pdf.createLoadingTask(path)
}
}
</script>

txt预览

txt.vue

1
2
3
<template>
<iframe :src="fileurl" frameborder="0" width="100%" height="600" />
</template>