项目中导入docx文件实现预览效果

首先需要用到docx-preview组件

在页面中全部引入该组件

import * as docx from "docx-preview";
import * as docx from "docx-preview";
import { onMounted } from "vue";
const docxOptions = Object.assign(docx.defaultOptions, {
  debug: true,
  experimental: true,
});

onMounted(() => {
  let dome: HTMLElement = document.getElementById("loadButton");
  console.log(dome);

  dome.addEventListener("click", loadDocx);
});

function loadDocx() {
  var file: any = document.getElementById("files").files[0];
  console.log(file);

  if (!file) return;

  var container = document.getElementById("document-container");

  docx.renderAsync(file, container, null, docxOptions).then(function (x) {
    console.log(x);
  });
}

全部代码

<template>
  <div class="hstack p-2 gap-2 bg-light position-sticky top-0" style="z-index: 1;">
    <input id="files" type="file" class="form-control" style="width: 50ch;" accept=".docx" />
    <button id="loadButton" class="btn btn-primary px-4">Load</button>
    <!-- <div class="dropdown">
            <button class="btn btn-secondary dropdown-toggle" type="button" id="optionsMenuButton"
                data-bs-toggle="dropdown" aria-expanded="false">
                Options
            </button>
            <ul id="optionsMenu" class="dropdown-menu" aria-labelledby="optionsMenuButton">
            </ul>
    </div>-->
  </div>

  <div class="showExcal">
    <div id="document-container"></div>
  </div>
</template>


<script setup lang="ts">
import * as docx from "docx-preview";
import { onMounted } from "vue";
const docxOptions = Object.assign(docx.defaultOptions, {
  debug: true,
  experimental: true,
});

onMounted(() => {
  let dome: HTMLElement = document.getElementById("loadButton");
  console.log(dome);

  dome.addEventListener("click", loadDocx);
});

function loadDocx() {
  var file: any = document.getElementById("files").files[0];
  console.log(file);

  if (!file) return;

  var container = document.getElementById("document-container");

  docx.renderAsync(file, container, null, docxOptions).then(function (x) {
    console.log(x);
  });
}
</script>


<style lang="less" >
.showExcal {
  width: 50vw;
  height: 50vh;
  overflow: auto;
}
.docx-wrapper {
  background-color: white !important;
  border: 1px solid gray !important;
  overflow-y: scroll;
  margin: 100px auto;
}
.el-cascader-node.in-active-path,
.el-cascader-node.is-active,
.el-cascader-node.is-selectable.in-checked-path {
  color: red !important;
}
</style>```

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue应用实现本地预览Word(.docx)、Excel(.xlsx)和PDF文件,你可以使用一些现有的库和插件。以下是一种常见的方法,使用`vue-pdf`、`xlsx`和`mammoth.js`库来实现此功能: 1. 首先,安装所需的库: ```bash npm install vue-pdf xlsx mammoth ``` 2. 创建一个Vue组件,并导入所需的库: ```vue <template> <div> <div v-if="fileType === 'pdf'"> <pdf :src="fileUrl" /> </div> <div v-else-if="fileType === 'xlsx'"> <div>{{ excelData }}</div> </div> <div v-else-if="fileType === 'docx'"> <div v-html="wordContent"></div> </div> </div> </template> <script> import { read, utils } from 'xlsx'; import mammoth from 'mammoth'; import { pdf } from 'vue-pdf'; export default { components: { pdf, }, data() { return { fileType: '', fileUrl: '', excelData: [], wordContent: '', }; }, mounted() { this.loadFile(); }, methods: { loadFile() { const file = 'path/to/your/file'; const extension = file.split('.').pop(); if (extension === 'pdf') { this.fileType = 'pdf'; this.fileUrl = file; } else if (extension === 'xlsx') { this.fileType = 'xlsx'; this.loadExcelFile(file); } else if (extension === 'docx') { this.fileType = 'docx'; this.loadWordFile(file); } }, loadExcelFile(file) { const reader = new FileReader(); reader.onload = (e) => { const data = new Uint8Array(e.target.result); const workbook = read(data, { type: 'array' }); const worksheet = workbook.Sheets[workbook.SheetNames[0]]; this.excelData = utils.sheet_to_json(worksheet, { header: 1 }); }; reader.readAsArrayBuffer(file); }, loadWordFile(file) { const reader = new FileReader(); reader.onload = (e) => { const arrayBuffer = e.target.result; const options = {}; mammoth.extractRawText({ arrayBuffer }, options) .then((result) => { this.wordContent = result.value; }) .catch((error) => { console.error(error); }); }; reader.readAsArrayBuffer(file); }, }, }; </script> ``` 在上面的代码,你需要将`path/to/your/file`替换为实际文件的路径。根据文件的扩展名,组件将显示不同的预览方式。 请注意,该示例仅提供了一种基本实现方法。你可以根据自己的需求进行修改和调整。 希望这可以帮助到你!如果你有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值