导出word文件 表格file-saver、html-docx-js-typescript

根据html代码转换为word文件

1、下载file-saver、html-docx-js-typescript

javascript 复制代码
npm install file-saver html-docx-js-typescript

2、

javascript 复制代码
<span @click="downloadWordWithHtmlString()">导出doc</span>
<div id="eltable" ref="eltable" style="display: none">
	<table
	 border="1"
     cellspacing="0"
     width="600"
     style="font-size: 12px; color: #000; text-align: center"
    >
    	<tr height="50">
        	<td>列1</td>
        	<td>列2</td>
        </tr>
        <tr height="50">
        	<td>1-1</td>
            <td>1-2</td>
        </tr>
        <tr height="50">
            <td>2-1</td>
            <td>2-2</td>
        </tr>
    </table>
</div>
import { saveAs } from 'file-saver'
import { asBlob } from 'html-docx-js-typescript'
downloadWordWithHtmlString() {
	// let eltable = this.$refs.eltable
    // let html = eltable.$el.innerHTML
    let html = document.getElementById('eltable').innerHTML
    let name = `${this.fieldName}-条件信息`
    let htmlString = `
    	<!DOCTYPE html>
        <html lang="en">
        <head>
        <meta charset="UTF-8">
        <title>Document</title>
        </head>
        <body>
        ${html}
        </body>
        </html>
    `
    asBlob(htmlString).then((data) => {
        saveAs(data, `${name}.doc`)
    })
}
相关推荐
把csdn当日记本的菜鸡37 分钟前
js查缺补漏
开发语言·javascript·ecmascript
BBB努力学习程序设计1 小时前
了解响应式Web设计:viewport网页可视区域
前端·html
zhangyao9403301 小时前
uni-app scroll-view特定情况下运用
前端·javascript·uni-app
鎏金铁匠1 小时前
跟着ECMAScript 规范,手写数组方法之map
javascript
顾安r2 小时前
11.14 脚本网页 青蛙过河
服务器·前端·python·游戏·html
不爱吃糖的程序媛3 小时前
Electron 智能文件分析器开发实战适配鸿蒙
前端·javascript·electron
flashlight_hi3 小时前
LeetCode 分类刷题:3217. 从链表中移除在数组中存在的节点
javascript·数据结构·leetcode·链表
Java追光着3 小时前
React Native 自建 JS Bundle OTA 更新系统:从零到一的完整实现与踩坑记录
javascript·react native·react.js
努力往上爬de蜗牛3 小时前
react native 运行问题和调试 --持续更新
javascript·react native·react.js
Achieve前端实验室5 小时前
JavaScript 原型/原型链
前端·javascript