Coder Social home page Coder Social logo

501351981 / vue-office Goto Github PK

View Code? Open in Web Editor NEW
3.1K 17.0 389.0 99.04 MB

支持word(.docx)、excel(.xlsx,.xls)、pdf等各类型office文件预览的vue组件集合,提供一站式office文件预览方案,支持vue2和3,也支持React等非Vue框架。Web-based pdf, excel, word preview library

Home Page: https://501351981.github.io/vue-office/examples/docs/

License: MIT License

JavaScript 59.55% Vue 40.45%
docx docx-preview excel pdf pdf-preview pdf-viewer vue xlsx xlsx-preview

vue-office's Introduction

vue-office

支持多种文件(docx、excel、pdf)预览的vue组件库,支持vue2/3。也支持非Vue框架的预览。

《演示效果》

《使用非Vue框架(原生js、React等)、或者Vue里面报错,看这里》

功能特色

  • 一站式:提供word(.docx)、pdf、excel(.xlsx, .xls)多种文档的在线预览方案,有它就够了
  • 简单:只需提供文档的src(网络地址)即可完成文档预览
  • 体验好:选择每个文档的最佳预览方案,保证用户体验和性能都达到最佳状态
  • 性能好:针对数据量较大做了优化

安装

#docx文档预览组件
npm install @vue-office/docx [email protected]

#excel文档预览组件
npm install @vue-office/excel [email protected]

#pdf文档预览组件
npm install @vue-office/pdf [email protected]

如果是vue2.6版本或以下还需要额外安装 @vue/composition-api

npm install @vue/composition-api

使用示例

文档预览场景大致可以分为两种:

  • 有文档网络地址,比如 https://***.docx
  • 文件上传时预览,此时可以获取文件的ArrayBuffer或Blob

.docx文件预览

使用网络地址预览

<template>
  <vue-office-docx 
      :src="docx"
      style="height: 100vh;"
      @rendered="rendered"
  />
</template>

<script>
//引入VueOfficeDocx组件
import VueOfficeDocx from '@vue-office/docx'
//引入相关样式
import '@vue-office/docx/lib/index.css'

export default {
  components:{
    VueOfficeDocx
  },
  data(){
    return {
      docx: 'http://static.shanhuxueyuan.com/test6.docx' //设置文档网络地址,可以是相对地址
    }
  },
  methods:{
    rendered(){
      console.log("渲染完成")
    }
  }
}
</script>

上传文件预览

读取文件的ArrayBuffer

<template>
  <div>
    <input type="file" @change="changeHandle"/>
    <vue-office-docx :src="src"/>
  </div>
</template>

<script>
import VueOfficeDocx from '@vue-office/docx'
import '@vue-office/docx/lib/index.css'

export default {
  components: {
    VueOfficeDocx
  },
  data(){
    return {
      src: ''
    }
  },
  methods:{
    changeHandle(event){
      let file = event.target.files[0]
      let fileReader = new FileReader()
      fileReader.readAsArrayBuffer(file)
      fileReader.onload =  () => {
        this.src = fileReader.result
      }
    }
  }
}
</script>

二进制文件预览

如果后端给的不是CDN地址,而是一些POST接口,该接口返回二进制流,则可以调用接口获取文件的ArrayBuffer数据,传递给src属性。

<template>
  <vue-office-docx 
      :src="docx"
      style="height: 100vh;"
      @rendered="rendered"
  />
</template>

<script>
//引入VueOfficeDocx组件
import VueOfficeDocx from '@vue-office/docx'
//引入相关样式
import '@vue-office/docx/lib/index.css'

export default {
  components:{
    VueOfficeDocx
  },
  data(){
    return {
      docx: '' 
    }
  }, 
  mounted(){
    fetch('你的API文件地址', {
        method: 'post'
    }).then(res=>{
        //读取文件的arrayBuffer
        res.arrayBuffer().then(res=>{
            this.docx = res
        })
    })
  },
  methods:{
    rendered(){
      console.log("渲染完成")
    }
  }
}
</script>

excel文件预览

通过网络地址预览示例如下,通过文件ArrayBuffer预览和上面docx的使用方式一致。

<template>
    <vue-office-excel
        :src="excel"
        style="height: 100vh;"
        @rendered="renderedHandler"
        @error="errorHandler"
    />
</template>

<script>
//引入VueOfficeExcel组件
import VueOfficeExcel from '@vue-office/excel'
//引入相关样式
import '@vue-office/excel/lib/index.css'

export default {
    components: {
        VueOfficeExcel
    },
    data() {
        return {
            excel: 'http://static.shanhuxueyuan.com/demo/excel.xlsx'//设置文档地址
        }
    },
    methods: {
        renderedHandler() {
            console.log("渲染完成")
        },
        errorHandler() {
            console.log("渲染失败")
        }
    }
}
</script>

pdf文件预览

通过网络地址预览示例如下,通过文件ArrayBuffer预览和上面docx的使用方式一致。

<template>
    <vue-office-pdf 
        :src="pdf"
        style="height: 100vh"
        @rendered="renderedHandler"
        @error="errorHandler"
    />
</template>

<script>
//引入VueOfficePdf组件
import VueOfficePdf from '@vue-office/pdf'

export default {
    components: {
        VueOfficePdf
    },
    data() {
        return {
            pdf: 'http://static.shanhuxueyuan.com/test.pdf' //设置文档地址
        }
    },
    methods: {
        renderedHandler() {
            console.log("渲染完成")
        },
        errorHandler() {
            console.log("渲染失败")
        }
    }
}
</script>

打赏支持

如果该项目帮您节省了开发时间,可以请作者喝杯咖啡,加微信交个朋友,交流前端问题。不能打赏的朋友麻烦帮点个免费的赞

赞助二维码

打赏50及以上可向作者索要源码(仅限自己项目使用,不能复制开源)

打赏的朋友欢迎添加微信,交流前端开发中遇到的技术、问题和困惑。

仅添加打赏过的用户,不定期删除屏蔽朋友圈的好友(为什么打赏了才能微信问问题?那别人为什么要先付出时间去解答你的问题?都是成年人了,人与人之间是价值交换,不是单向付出)】 常见问题

个人微信

vue-office's People

Contributors

501351981 avatar lianxin255 avatar redflagrf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-office's Issues

样例无法渲染

Failed to resolve component: vue-office-docx 
 at <Index onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< undefined > > 
 at <RouterView> 

警告

打包后线上无法使用

image
image

package.json
{
"name": "my-project",
"version": "1.0.0",
"description": "A Vue.js project",
"author": "dxx",
"private": true,
"main": "index.js",
"scripts": {
"dev": "node build/dev-server.js",
"start": "npm run dev",
"build": "node --max_old_space_size=3072 build/build.js",
"unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run",
"e2e": "node test/e2e/runner.js",
"test": "npm run unit && npm run e2e",
"lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs"
},
"dependencies": {
"@antv/g6": "^2.2.6",
"@antv/g6-editor": "^1.2.0",
"@vue-office/docx": "^1.1.3",
"axios": "^0.19.0",
"babel-polyfill": "^6.26.0",
"currency-formatter": "^1.5.3",
"d3": "^3.5.12",
"d3-scale": "^2.1.2",
"dagre-d3": "^0.6.3",
"decimal.js": "^10.1.1",
"docx-preview": "^0.1.15",
"echarts": "^4.2.1",
"element-ui": "^2.13.2",
"html2canvas": "^1.0.0-alpha.12",
"jquery": "^3.2.1",
"js-base64": "^2.4.9",
"jspdf": "^1.4.1",
"jsplumb": "^2.5.7",
"jszip": "^3.10.1",
"lodash": "^4.17.10",
"moment": "^2.22.2",
"parse-asn1": "5.1.5",
"preview": "^0.1.3",
"qrcode": "1.4.4",
"underscore": "^1.8.3",
"v-viewer": "^1.2.1",
"view": "^1.1.1",
"vue": "^2.7.14",
"vue-demi": "^0.14.1",
"vue-pdf": "^4.3.0",
"vue-router": "^3.0.1",
"vue-template-compiler": "^2.7.14",
"vuex": "^3.0.1"
},
"devDependencies": {
"autoprefixer": "^7.1.2",
"babel-core": "^6.22.1",
"babel-eslint": "^7.1.1",
"babel-loader": "^7.1.1",
"babel-plugin-istanbul": "^4.1.1",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-preset-env": "^1.3.2",
"babel-preset-stage-2": "^6.22.0",
"babel-register": "^6.22.0",
"chai": "^4.1.2",
"chalk": "^2.0.1",
"connect-history-api-fallback": "^1.3.0",
"copy-webpack-plugin": "^4.0.1",
"cross-env": "^5.0.1",
"cross-spawn": "^5.0.1",
"crypto-js": "^3.1.9-1",
"css-loader": "^0.28.7",
"eslint": "^3.19.0",
"eslint-friendly-formatter": "^3.0.0",
"eslint-loader": "^1.7.1",
"eslint-plugin-html": "^3.0.0",
"eventsource-polyfill": "^0.9.6",
"express": "^4.14.1",
"extract-text-webpack-plugin": "^3.0.0",
"file-loader": "^1.1.5",
"friendly-errors-webpack-plugin": "^1.6.1",
"html-webpack-plugin": "^2.30.1",
"http-proxy-middleware": "^0.20.0",
"inject-loader": "^3.0.0",
"karma": "^1.4.1",
"karma-coverage": "^1.1.1",
"karma-mocha": "^1.3.0",
"karma-phantomjs-launcher": "^1.0.2",
"karma-phantomjs-shim": "^1.4.0",
"karma-sinon-chai": "^1.3.1",
"karma-sourcemap-loader": "^0.3.7",
"karma-spec-reporter": "0.0.31",
"karma-webpack": "^2.0.2",
"less": "^4.1.1",
"less-loader": "^5.0.0",
"mocha": "^3.2.0",
"nightwatch": "^0.9.12",
"node-sass": "^4.12.0",
"opn": "^5.1.0",
"optimize-css-assets-webpack-plugin": "^3.2.0",
"ora": "^1.2.0",
"phantomjs-prebuilt": "^2.1.14",
"portfinder": "^1.0.13",
"rimraf": "^2.6.0",
"sass-loader": "^7.1.0",
"selenium-server": "^3.0.1",
"semver": "^5.3.0",
"shelljs": "^0.7.6",
"sinon": "^4.0.0",
"sinon-chai": "^2.8.0",
"style-loader": "^0.19.0",
"ts-loader": "^9.4.2",
"typescript": "^5.0.4",
"url-loader": "^0.5.8",
"vue-loader": "^13.3.0",
"vue-style-loader": "^3.1.2",
"webpack": "^3.6.0",
"webpack-bundle-analyzer": "^2.9.0",
"webpack-dev-middleware": "^1.12.0",
"webpack-hot-middleware": "^2.18.2",
"webpack-merge": "^4.1.0"
},
"engines": {
"node": ">= 4.0.0",
"npm": ">= 3.0.0"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
]
}

可否考虑增加doc格式支持

感谢作者提供这么好用的组件库,在使用该组件时,貌似不支持doc格式的文档,可否考虑增加doc格式的支持?

vue3+vite4+ts无法import组件

组件名称
"@vue-office/docx": "^0.2.2",
"@vue-office/excel": "^0.2.3",

Vue和Vite版本:
"vue": "^3.2.45",
"vite": "^4.1.0",

import VueOfficeExcel from '@vue-office/excel'
或者
import VueOfficeExcel from '@vue-office/docx'
时报错,提示说:

TS7016: Could not find a declaration file for module '@vue-office/excel'. 

'/Users/xxx/node_modules/@vue-office/excel/lib/index.js' implicitly has an 'any' type.   

Try `npm i --save-dev @types/vue-office__excel` if it exists or add a new declaration (.d.ts) file containing `declare module '@vue-office/excel';`

如果 npm i --save-dev @types/vue-office__docx, 或者npm install -D @types/vue-office
, 提示not found

预览Excel时,表格宽度有问题,Uncaught TypeError: Cannot read properties of null (reading 'clientWidth')

1、组件名称及版本:
@vue-office/[email protected]
[email protected]

2、运行环境:
浏览器:谷歌浏览器 111.0.5563.65
vue版本:[email protected]
打包工具及版本:[email protected]

3、问题描述:(如果有报错,麻烦粘贴详细的报错信息、package.json内容等)

<a-modal dialogClass="file-modal" v-model="modelVisible" width="900px" :footer="null" @cancel="handleClose" destroyOnClose> <vue-office-excel :src="file.url" style="height:70vh;width:100%;" @rendered="renderedHandler" @error="errorHandler" /> </a-modal>

你好,我用a-modal做了一个弹窗预览excel,首次打开是宽度是正常的
image
关闭后再次打开,宽度就有问题了
image

F12打开后,有如下报错信息

Uncaught TypeError: Cannot read properties of null (reading 'clientWidth')
at Object.width (index.js?b690:99:1)
at sh.viewWidth (index.js?b690:4:1)
at rp.getRect (index.js?b690:14:1)
at rp.getTableOffset (index.js?b690:14:1)
at rp.sn (index.js?b690:14:1)
at rp.reload (index.js?b690:14:1)
at eval (index.js?b690:14:1)

麻烦看一下是什么问题

清理下依赖

大佬,东西是很好用,不过好像有些无关依赖没有清理掉🤣,我看还有arcgis、supermap啥的。

Cannot read properties of null (reading 'clientWidth')

使用vue-office-excel时,放大缩小浏览器大小的时候会出现报错。
我把vue-office-excel放到了一个dialog里面(使用的element-ui库)
当第一次进入到dialog后操作浏览器大小不会出现这个问题,当关闭dialog后再次进入后操作浏览器就会出现报错。
完整的报错信息如下:
ERROR Cannot read properties of null (reading 'clientWidth') TypeError: Cannot read properties of null (reading 'clientWidth') at Object.width (webpack-internal:///./node_modules/@vue-office/excel/lib/index.js:35:91164) at sh.viewWidth (webpack-internal:///./node_modules/@vue-office/excel/lib/index.js:5:51875) at rp.getRect (webpack-internal:///./node_modules/@vue-office/excel/lib/index.js:5:148700) at rp.getTableOffset (webpack-internal:///./node_modules/@vue-office/excel/lib/index.js:5:148872) at rp.sn (webpack-internal:///./node_modules/@vue-office/excel/lib/index.js:5:138407) at rp.reload (webpack-internal:///./node_modules/@vue-office/excel/lib/index.js:5:148610) at eval (webpack-internal:///./node_modules/@vue-office/excel/lib/index.js:5:144665)
package.json信息如下
"dependencies": { "@vue-office/docx": "^1.1.3", "@vue-office/excel": "^1.1.3", "@vue/composition-api": "^1.7.1", "axios": "^0.24.0", "core-js": "^3.6.5", "element-ui": "^2.15.13", "fake-progress": "^1.0.4", "less": "^4.1.3", "less-loader": "^11.1.0", "luckyexcel": "^1.0.1", "mockjs": "^1.1.0", "node-polyfill-webpack-plugin": "^2.0.1", "nprogress": "^0.2.0", "sass": "^1.62.0", "sass-loader": "^13.2.2", "tui-image-editor": "^3.15.3", "vue": "^2.7.14", "vue-contextmenu": "^1.5.11", "vue-demi": "^0.13.11", "vue-page-split": "^2.0.1", "vuedraggable": "^2.24.3", "vuex": "^3.6.2" }, "devDependencies": { "@vue/cli-plugin-babel": "~5.0.0", "@vue/cli-plugin-eslint": "~5.0.0", "@vue/cli-service": "~5.0.0", "babel-eslint": "^10.1.0", "babel-polyfill": "^6.26.0", "eslint": "^7.32.0", "eslint-plugin-vue": "^8.0.3" }, "eslintConfig": { "root": true, "env": { "node": true }, "extends": [ "plugin:vue/essential", "eslint:recommended" ], "parserOptions": { "parser": "babel-eslint" }, "rules": {} },

亲们,提Issue须知~~

为了更加高效的沟通解决问题,麻烦您在提Issue时注明以下内容,感谢您的反馈~~

  1. 组件名称及版本: 如@vue-office/[email protected]
  2. 运行环境:
    浏览器:如谷歌浏览器/iOS/安卓
    vue版本:如[email protected]
    打包工具及版本:如[email protected]
  3. 问题描述:(如果有报错,麻烦粘贴详细的报错信息、package.json内容等)

提完Issue后也可以添加我的微信,详细沟通,微信号:_hit757_,麻烦注明bug反馈

vue2安装excel报错

ERROR Failed to compile with 1 errors 下午6:39:46

error in ./node_modules/@vue-office/excel/lib/index.js

Module parse failed: Unexpected token (44:181977)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)

vue2引入时报错Ar.defineComponent is not a function

主要依赖版本如下
├── @vue-office/[email protected]
├── @vue-office/[email protected]
├── @vue/[email protected]
├── @vue/[email protected]
├── @vue/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
使用import VueOfficeExcel from '@vue-office/excel'引入时报错Ar.defineComponent is not a function

使用后跨域问题

兄弟使用了你的代码后,将src指向我我们自己的服务器保存的文件地址,浏览器直接显示跨域!后端都配置了解决跨域的方案。然而前端开始跨域

vue2+ant-design 使用office无效

组件名称及版本: 如@vue-office/[email protected]
运行环境:
浏览器:谷歌浏览器
vue版本:@vue/cli 5.0.8
打包工具及版本:vue-cli
问题描述:
引入后 使用没效果
image
image
src是内网搭建的minio文件服务器上传后的url

vite+ts 打包报错

组件名称及版本:
image

运行环境:vscode

vue版本:如vue3.2 .47
打包工具及版本:如[email protected]
问题描述:
image

image

可以解决打包报错 将 "build": "vue-tsc && vite build" 改成 "build": "vite build"
ts打包报错希望解决

做的不错

希望能够支持切页、缩放、宽高自适应的效果

vue3+ts报错

vue-office_docx:

main.ts:27 Uncaught (in promise) Error: Dynamic require of "vue-demi" is not supported
at chunk-QJ3XAL7T.js?v=262d692d:12:9
at @vue-office_docx.js?v=115aedc0:16:88
at node_modules/@vue-office/docx/lib/index.js (@vue-office_docx.js?v=115aedc0:17:7)
at __require2 (chunk-QJ3XAL7T.js?v=262d692d:18:50)
at @vue-office_docx.js?v=115aedc0:5320:31

ios 引用报错

组件名称: "@vue-office/pdf": "^0.2.4",
浏览器: ios
vue版本: [email protected]
打包工具: @vue/[email protected]
问题描述
在 iphoneXr 上报错
syntaxError: Invalid character: '/#/'
data:text/javascript;base64,......................往后省略

undefined is not an object(evaluating 'window.pdfjsLib.GlobalWorkerOptions')
两个错误 pdf不能正常显示

package.json 内容
"dependencies": {
"@vue-office/pdf": "^0.2.4",
"@vue/composition-api": "^1.7.1",
"axios": "1.2.1",
"compression-webpack-plugin": "^10.0.0",
"core-js": "^3.8.3",
"docx-preview": "^0.1.4",
"echarts": "^5.4.1",
"jszip": "^3.10.1",
"lib-flexible": "^0.3.2",
"vant": "^2.12.53",
"vconsole": "^3.3.4",
"vue": "^2.6.14",
"vue-demi": "^0.13.11",
"vue-draggable-resizable": "^2.3.0",
"vue-router": "^3.5.1",
"vuex": "^3.6.2",
"vuex-persistedstate": "^4.1.0"
},
"devDependencies": {
"@babel/cli": "^7.21.0",
"@babel/core": "^7.21.0",
"@babel/plugin-proposal-optional-chaining": "^7.21.0",
"@babel/plugin-syntax-top-level-await": "^7.14.5",
"@typescript-eslint/eslint-plugin": "^5.4.0",
"@typescript-eslint/parser": "^5.4.0",
"@vue/cli-plugin-babel": "~5.0.0",
"@vue/cli-plugin-eslint": "~5.0.0",
"@vue/cli-plugin-router": "~5.0.0",
"@vue/cli-plugin-typescript": "~5.0.0",
"@vue/cli-plugin-vuex": "~5.0.0",
"@vue/cli-service": "~5.0.0",
"@vue/eslint-config-typescript": "^9.1.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-vue": "^8.0.3",
"less": "^4.1.3",
"less-loader": "^7.3.0",
"lint-staged": "^11.1.2",
"prettier": "^2.4.1",
"px2rem-loader": "^0.1.9",
"typescript": "~4.5.5",
"vue-template-compiler": "^2.6.14"
},

VueOfficePdf不展示

测试机: SE, oppo

SyntaxError: Invalid character: '#'\n\tdata:text/javascript;base64,IWZ1bmN0aW9uIHdlYnBhY2tVbml2ZXJzYWxNb2R1bGVEZWZpbml0aW9uKHQsZSl7Im9iamVjdCI9PXR5cGVvZiBleHBvcnR...

Uncaught (in promise) {"name": "TypeError", "message": "undefined is not an object (evaluating 'window.pdfjsLib.GlobalWorkerOptions')"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.