首页 > 其他分享 >vue在线预览word,excel , pdf组件化

vue在线预览word,excel , pdf组件化

时间:2022-11-07 10:26:47浏览次数:80  
标签:docx vue word type GLOBAL excel spread dialog data

方法一:如果文件是域名的,可以使用微软免费接口

//由于免费的在线查看,不支持ip+端口的形式所以单独处理
this.dialog.url='http://view.officeapps.live.com/op/view.aspx?src='+ encodeURIComponent(this.dialog.fileUrl);

方法二:如何是私有化部署,域名是ip加端口的可以使用如下
word:使用插件 docx-preview

npm i docx-preview --save

使用

 

<template>
    <div ref="file" id="fileid" style="width: 100%;height: 100%;overflow: auto;"></div>
</template>
<script>
    let that 
    let docx = require("docx-preview");
    export default {
        name: 'lzWps',
        data() {
            return {}
        },
        mounted() {
            that =this
        },
        methods: {
            docxFn(){
          //请求接口,下载文件 that.GLOBAL.doBlob(this.api.GLOBAL_file_fileInfo_downloadNoneNeedAccess+'/'+this.fileId,{responseType:'blob'},(res) => { docx.renderAsync(res.body, this.$refs.file); // 渲染到页面 }) } } } </script>

 

pdf 可以直接使用浏览器自带的打开功能

excel 打开可以使用  SpreadJS

npm install @grapecity/spread-sheets-vue @grapecity/spread-sheets
npm install @grapecity/spread-excelio

 

使用

<template>
    <div class="lzExcel">
        <gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread" style="width: 100%;height: 100%;">
            <gc-worksheet>
            </gc-worksheet>
        </gc-spread-sheets>
        
        <input type="file" id="file" @change="fileChange">
    </div>
</template>

<script>
    let that
    import "@grapecity/spread-sheets/styles/gc.spread.sheets.excel2016colorful.css";
    import * as GC from "@grapecity/spread-sheets";
    import "@grapecity/spread-sheets-vue";
    import { IO } from "@grapecity/spread-excelio";
    import {jsonData} from './data';
    import {
        mapGetters
    } from 'vuex'
    export default {
        computed: {
            ...mapGetters(['api', 'userInfo']),
        },
        name: 'lzExcel',
        props: {
            datas:null,
        },
        data() {
            return {
                spread:null,
            }
        },
        mounted() {
            that = this
            this.excelIO = new IO();
            
        },
        watch:{
            datas(){
                this.excelUrl=this.datas
                this.seeFn()
            }
        },
        methods: {
            initSpread: function (spread) {
                this.spread = spread;
                spread.options.calcOnDemand = true;
            },
            seeFn(){
                console.log(this.excelUrl);
                this.excelIO.open(this.excelUrl, (json)=> {
                    console.log(json);
                    // spread.fromJSON(json, {
                    //     incrementalLoading: {
                    //         loading: function (progress, args) {
                    //             progress = progress * 100;
                    //             loadingStatus.value = progress;
                    //             console.log("current loading sheet", args.sheet && args.sheet.name());
                    //         },
                    //         loaded: function () {
                    //         }
                    //     }
                    // });
                    this.spread.fromJSON(json);
                },(e)=> {
                    // process error
                    console.log(e);
                }, {
                    password: 0
                });
            },
            fileChange(data){
                this.excelUrl=data.target.files[0]
                this.seeFn()
            },
        }
    }
</script>

<style lang="scss" scoped>
    .lzExcel{
        width: 100%;height: 100%;overflow: auto;
    }
</style>

引入

<lzExcel :datas="excel.data"></lzExcel>
excelFn(){
//请求接口,下载文件
that.GLOBAL.doBlob(this.api.GLOBAL_file_fileInfo_downloadNoneNeedAccess+'/'+this.fileId,{responseType:'blob'},(res) => { this.datas = res.body   }) },

 




<script>let that import {mapGetters} from 'vuex'let docx = require("docx-preview");import lzExcel from '@/components/lzExcel/lzExcel'export default {computed: {...mapGetters(['api','userInfo']),},components:{lzExcel},name: 'lzWps',props: {close:{type:Function},confirm: {type:Function},cancel: {type:Function},width: {type: String,default: '80%'},title: {type: String,default: '选择'},filePath: {type: String,default: ''},fileId: {type: String,default: ''},fileName: {type: String,default: ''},},data() {return {dialog:{show:false,url:'',fileName:'',fileId:'',fileUrl:'',fileType:'word'},isIp:{show:false,loading:true,},excel:{data:""}}},mounted() {that =this},methods: {cancelFn(){this.dialog.show=falsethis.cancel()},submitFn(){this.confirm(true)},show(data){this.dialog.show=truethis.dialog.fileName=data.fileNamethis.dialog.fileUrl=this.api.GLOBAL_HOST_IMAGE + data.filePath + '?Authorization='+this.userInfo.tokenlet index= data.fileName.lastIndexOf('.')let suf=data.fileName.slice(index+1,data.fileName.length)this.dialog.fileType=suflet type=this.$lizhao.file.lzGetTypeSuffix(suf)if(type=='word'){if(suf=='pdf'){this.dialog.url=this.dialog.fileUrl+'&download=0'return}//若是私有化或者ip形式,自行判断if(this.checkdomain()==false){this.isIp.show=trueif(suf=='docx'||suf=='doc'){this.docxFn()}else if(suf=='xlsx'||suf=='xls'){this.excelFn()}return}// 若不是ip形式,直接使用免费的接口this.isIp.show=false//由于免费的在线查看,不支持ip+端口的形式所以单独处理this.dialog.url='http://view.officeapps.live.com/op/view.aspx?src='+ encodeURIComponent(this.dialog.fileUrl);}},excelFn(){this.isIp.loading=truethat.GLOBAL.doBlob(this.api.GLOBAL_file_fileInfo_downloadNoneNeedAccess+'/'+this.fileId,{responseType:'blob'},(res) => {this.excel.data = res.bodythis.isIp.loading=false})},docxFn(){this.isIp.loading=truethat.GLOBAL.doBlob(this.api.GLOBAL_file_fileInfo_downloadNoneNeedAccess+'/'+this.fileId,{responseType:'blob'},(res) => {docx.renderAsync(res.body, this.$refs.file); // 渲染到页面this.isIp.loading=false})},download(){window.location.href=this.dialog.fileUrl},handclose(){this.close()},//验证是否是域名checkdomain(){    let doname = /^([\w-]+\.)+((com)|(net)|(org)|(gov\.cn)|(info)|(cc)|(com\.cn)|(net\.cn)|(org\.cn)|(name)|(biz)|(tv)|(cn)|(mobi)|(name)|(sh)|(ac)|   (io)|(tw)|(com\.tw)|(hk)|(com\.hk)|(ws)|(travel)|(us)|(tm)|(la)|(me\.uk)|(org\.uk)|(ltd\.uk)|(plc\.uk)|(in)|(eu)|(it)|(jp))$/;    let flag_domain = doname.test(document.domain);    if(!flag_domain){//错误的域名        return false    }else{        return true    }}}}</script>

标签:docx,vue,word,type,GLOBAL,excel,spread,dialog,data
From: https://www.cnblogs.com/lizhao123/p/16865075.html

相关文章

  • ckeditor提供word文档转码功能
    ​ 自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了。一般情况下我们将Word内容粘贴到Web编辑......
  • Vue面试题44:Vue3为什么要用Proxy代替defineProperty?(总结自B站up主‘前端杨村长’视频,
    分析Vue3中最重大的更新之一就是响应式模块reactivity的重写。主要就是用Proxy替换`defineProperty实现响应式;此变化主要是从性能方面考量;思路属性拦截的几种方......
  • 初识Vue
    模板的理解html中包含了一些JS语法代码,语法分为两种,分别为:1.插值语法(双大括号表达式)2.指令(以v-开头)插值语法1.功能:用于解析标签体内容2.语法:{{xxx}},xx......
  • 谈谈你对 vue 的理解
    whatvue vue是创建用户界面的js框架,是创建spa应用的框架;使用mvvm数据驱动视图模型,后端数据和页面解构分离,后端处理数据,动态显示页面;采用组件化模式,提高代码......
  • Vue-computed计算属性
    转载于:https://blog.csdn.net/qq_25015861/article/details/123610969作者:冰咖啡iii本文只做学习参考; computed计算属性1、什么是计算属性计算属性本质是方法,只是......
  • vue $nextTick方法
    $nextTick方法问题:异步代码没有执行,但是同步代码已经完成逻辑,所以就需要使用$nextTick来等待dom完毕后再执行同步代码解决方法:使用nextTick()等待dom更新完毕后,在......
  • 初识vue3
    Vue3vue3安装vue--version##安装或者升级你的@vue/clinpminstall-g@vue/cli##创建vuecreatemyv3##启动cdmyv3npmrunservevue3特点新增组合式api......
  • 10.Vue中常用的第三方组件
    第三方插件可以增强vue,帮助我们更好的开发项目,如axios,vuex,vue-router,elementui等我们可以自定义插件,步骤如下:在vue项目下的src文件夹下创建plugins文件夹,创建一个i......
  • Vue3 用keep-alive实现tab页缓存
    一般国产味道的后台界面,都会有一个类似于浏览器tab标签的东西大概就是这样的,可以点击标签切换不同的路由,并且不丢失之前录入的数据。但是vue-router默认是不支持该操......
  • EXCEL用DB函数计算商品房4年后每年的折旧额
    DB函数主要是用于计算通过固定余额递减法来计算某资产在一定时间内的折旧值,其语法结构为:DB(cost,salvage,life,period,month),各参数的含义如下:cost参数:该参数用于指定资产原值......