vue 导出页面内容为word文件
- 先创建一个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}`;
};
- 在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);
},
}