vue 通过dom元素将页面导出成word文档

根据项目需求,通过dom元素将页面导出成word文档,支持word文档的查看和编辑,(来人,直接上代码)

1 先下载html-docx-js依赖,官网html-docx-js

npm  i html-docx-js

2 在进行下载file-saver,将文件进行本地下载

npm  i file-saver

3 页面内使用下载的依赖

import { saveAs } from "file-saver";
import htmlToDocx from "html-docx-js/dist/html-docx";

4 代码演示(注意一定要写行内样式 不然导出来的样式不生效)

<template> 
    <div class="report-box" ref="reportRef">
        <h2 style="text-align: center">分析报告</h2>
  </div>
</template>

<script>
    import { saveAs } from "file-saver";
    import htmlToDocx from "html-docx-js/dist/html-docx";
    export default {
      methods: {
      
        handelExprot() {
          // 获取需要转换的dom元素
          const sectionContent = this.$refs.reportRef.innerHTML;
          // 使用html-docx-js插件将转换的dom引入插件中
          const converted = htmlToDocx.asBlob(sectionContent);
          // 使用file-saver 插件进行本地下载
          saveAs(converted, "exported-document.docx");
        },
      },

    }
    
</script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值