如何利用vue展示.docx文件、excel文件和csv文件內(nèi)容
一、展示word文件內(nèi)容
1、安裝并引入依賴mammoth
npm install --save mammoth
import mammoth from "mammoth"
2、頁面中使用
<div style="height:850px;overflow-y:auto;" v-html="content"/>
//根據(jù)文件url,以arraybuffer的形式獲取docx文件內(nèi)容,傳給插件轉(zhuǎn)成html格式,展示在頁面上
var xhr = new XMLHttpRequest()
xhr.open('GET', fileurl, true)
xhr.responseType = 'arraybuffer'
const rhis = this
xhr.onload = function(){
if(xhr.status === 200){
mammoth.convertToHtml({arrayBuffer: new Uint8Array(xhr.response)}).then(function(res){
rhis.$nextTick(()=>{
rhis.content = res.value
})
})
}
}
xhr.send()
二、展示excel/csv文件內(nèi)容
1、安裝并引入依賴handsontable、papaparse,excel文件需要安裝xlxs
npm install handsontable @handsontable/vue npm install papaparse npm install xlsx
import Papa from 'papaparse' import xlsx from 'xlsx'
2、公共組件sheet.vue
<template>
<div class="overf">
<div id="table" class="sheet">
<hot-table ref="hot" :data="data" :settings="hotSettings" />
</div>
</div>
</template>
<script>
import { HotTable } from '@handsontable/vue'
// import Handsontable from 'handsontable'
import 'handsontable/dist/handsontable.full.css'
export default {
components: { HotTable },
props: {
data: {
type: Array,
default() {
return []
}
}
},
data() {
return {
hot: null,
hotSettings: {
readOnly: true
// manualColumnResize: true,
// manualRowResize: true,
// minSpareRows: 0
}
}
},
watch: {
data(newValue) {
this.$refs.hot.hotInstance.loadData(newValue)
}
},
created() {
},
methods: {}
}
</script>
<style lang="scss" scoped>
.overf{
height: 300px;
overflow: hidden;
}
.sheet{
height: 100%;overflow: auto;
&>>>#hot-display-license-info{
display:none;
}
}
</style>3、頁面內(nèi)引入組件
import sheet from './sheet'
<sheet v-if="isCsv" :data="sheetData" />
data() {
return {
sheetData: [], // sheet
}
},
// csv文件
this.sheetData = []
const rhis = this
Papa.parse(fileurl, {
download: true,
complete: res => {
const arrs = res.data
const lastItem = arrs[arrs.length - 1].every(val => val === '')
lastItem && arrs.pop()
rhis.sheetData = arrs
rhis.isCsv = true
}
})
// excel文件
var xhr2 = new XMLHttpRequest()
xhr2.open('GET', fileurl, true)
xhr2.responseType = 'blob'
const rhis = this
xhr2.onload = function() {
var blob = this.response
var reader = new FileReader()
reader.onload = function(e) {
const wb = xlsx.read(e.target.result, {
type: 'binary'
})
rhis.outputWorkbook(wb) // 處理數(shù)據(jù)
}
reader.readAsBinaryString(blob)
}
xhr2.send()
// 讀取 excel 文件
outputWorkbook(workbook) {
this.sheetData = []
var sheetNames = workbook.SheetNames // 工作表名稱集合
sheetNames.forEach(name => {
var worksheet = workbook.Sheets[name] // 只能通過工作表名稱來獲取指定工作表
var data = xlsx.utils.sheet_to_csv(worksheet)
Papa.parse(data, { // 使用papaparse解析csv數(shù)據(jù),并展示在表格中
complete: res => {
const arrs = res.data
// 去除最后的空行
const lastItem = arrs[arrs.length - 1].every(val => val === '')
lastItem && arrs.pop()
this.sheetData = arrs
this.isCsv = true
}
})
})
},
總結(jié)
到此這篇關(guān)于如何利用vue展示.docx文件、excel文件和csv文件內(nèi)容的文章就介紹到這了,更多相關(guān)vue展示docx、excel和csv文件內(nèi)容內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3+ts+elementui-plus二次封裝彈框?qū)崙?zhàn)教程
這篇文章主要介紹了vue3+ts+elementui-plus二次封裝彈框?qū)崙?zhàn)教程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
vue項(xiàng)目回到頂部的兩種超簡單實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目回到頂部的兩種超簡單實(shí)現(xiàn)方法,?頁面切換回到頂部是一個很常見的功能,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
vue項(xiàng)目多環(huán)境配置(.env)的實(shí)現(xiàn)
最常見的多環(huán)境配置,就是開發(fā)環(huán)境配置,和生產(chǎn)環(huán)境配置,本文主要介紹了vue項(xiàng)目多環(huán)境配置的實(shí)現(xiàn),感興趣的可以了解一下2021-07-07
淺析vue如何實(shí)現(xiàn)手機(jī)橫屏功能
在項(xiàng)目開發(fā)中有時候需求需要手動實(shí)現(xiàn)橫屏功能,所以這篇文章主要為大家詳細(xì)介紹了如何使用Vue實(shí)現(xiàn)手機(jī)橫屏功能,需要的小伙伴可以參考一下2024-03-03
Vue動態(tài)面包屑功能的實(shí)現(xiàn)方法
面包屑功能是我們在項(xiàng)目中經(jīng)常遇到的功能,今天小編使用Element-UI 進(jìn)行實(shí)現(xiàn)在vue項(xiàng)目中實(shí)現(xiàn)面包屑功能,具體實(shí)現(xiàn)方式大家跟隨小編一起學(xué)習(xí)吧2019-07-07
vue + element-ui的分頁問題實(shí)現(xiàn)
這篇文章主要介紹了vue + element-ui的分頁問題實(shí)現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
解決vue單頁面應(yīng)用中動態(tài)修改title問題
這篇文章主要介紹了解決vue單頁面應(yīng)用中動態(tài)修改title問題,本文通過示例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06

