vue 导出页面内容为word文件

vue 导出页面内容为word文件

  1. 先创建一个exportToWord.js的js文件
javascript 复制代码
// 导出Word
export const exportToWord = (id, name) => {
  // 获取选中区域Html
  const dom = document.getElementById(id);
  const content = dom.innerHTML;
  const convertedContent = convertToWordDocument(content);

  // Html类型数据 转换为 文件类型数据
  const blob = new Blob([convertedContent], { type: "application/msword" });

  // 下载Word文档
  const link = document.createElement("a");
  link.href = URL.createObjectURL(blob);
  link.download = name + ".doc" || "exported.doc";
  link.click();
};

//完善Html格式
const convertToWordDocument = (content) => {
  const header = `<!DOCTYPE html><html><head><meta charset='utf-8'><title>Exported Document</title></head><body>`;
  const footer = `</body></html>`;

  return `${header}${content}${footer}`;
};
  1. 在vue中使用
javascript 复制代码
<template>
  <div id="meet_demo" class="content-style">
   <div class="title">
      一、会议标题
    </div>
   <span slot="footer" class="dialog-footer">
     <el-button @click="downLoad('meet_demo', `纪要预览-${meetCurrentTime}`)"
       >下载</el-button
     >
   </span>
  </div>
 </template>
import { exportToWord } from "@/hooks/exportToWord";
 methods: {
   downLoad(id, name) {
	  exportToWord(id, name);
	},
 }
相关推荐
Moment9 小时前
写给迷茫的应届生,怎样在 AI 时代争取更多机会
前端·后端·面试
fengci.10 小时前
CTF2MISC中等部分
开发语言·前端·学习
涛涛ing10 小时前
那个PR太干净了,干净到我差点直接点了合并
前端
去伪存真10 小时前
稠密检索负责脑补,稀疏检索负责兜底:拆解 RAG 倒排索引的“查字典”艺术
前端·agent
喵个咪11 小时前
GoWind 拆解:全链 ID 类型体系——int64 定版与前端字符串化契约
前端·后端·go
喵个咪11 小时前
GoWind CMS 拆解:Flutter 设计系统内核——双态 token、四档断点与自适应壳层
前端·flutter·交互设计
喵个咪11 小时前
GoWind CMS 拆解:发布审批——多级推进、逐级角色与全程留痕
前端·后端·go
喵个咪11 小时前
GoWind Flow 拆解:一个 Go 工作流引擎与它的双模式设计器
前端·后端·go
喵个咪12 小时前
GoWind Admin 拆解:打印模板设计器——从画布到服务端 PDF 的完整打印链路
前端·后端·go
总要学点什么12 小时前
第96篇:三维数据加载-城市建筑白模样式+着色器——让色带按体量上色,最白的那档却涂不上去
前端·gis·webgl·cesium·着色器·空间分析·白模