一、步骤

1、下载依赖:

npm install --save mammoth

2、在需要展示word文件的页面中引入并使用

<template>
    <div class="home">
        <div v-show="bigImg.type == 'DOCX'" class="word-box" id="wordView" v-loading="loading" element-loading-text="加载中" v-html="vHtml" />
    </div>
</template>

script:

import mammoth from 'mammoth'

create() {

this.previewWord(this.bigImg.src)

},

method: {

// 渲染 word 文件内容
        previewWord(url) {
            var vm = this
            const xhr = new XMLHttpRequest()
            xhr.open('get', url, true)
            xhr.responseType = 'arraybuffer'
            xhr.onload = function () {
                if (xhr.status === 200) {
                    mammoth.convertToHtml({ arrayBuffer: new Uint8Array(xhr.response) }).then(function (resultObject) {
                        vm.$nextTick(() => {
                            vm.vHtml = resultObject.value
                        })
                    })
                }
            }
            xhr.send()
        },

}

二、注意点

样式会丢失,此插件只提取了word文件的文字内容

觉得有用就收藏一下吧,不然下次会找不到了哟^ _ ^
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐