导出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`)
    })
}
相关推荐
jingqingdai39 小时前
别用正则格式化 HTML!我用 DOM 遍历实现零风险本地格式化,老项目重构效率直接拉满
前端·重构·html
拉里呱唧10 小时前
一个像在使用PPT的在线 HTML 编辑器:HeyHTML
javascript·交互·html5
changshuaihua00110 小时前
扣子开发指南
javascript·人工智能
光影少年11 小时前
对typescript开发框架的理解?
前端·javascript·typescript
a11177611 小时前
“像风之翼“无人机巡检平台仪表盘
前端·javascript·开源·html·无人机
a11177611 小时前
QQ 宠物(怀旧 开源)前端electron项目
前端·开源·html
ZC跨境爬虫11 小时前
跟着 MDN 学 HTML day_8:(高级文本语义标签+适配核心功底)
前端·css·笔记·ui·html
Dxy123931021611 小时前
HTML中的伪类详解:从基础到高级应用的全面指南
前端·html
Dxy123931021611 小时前
HTML中如何设置元素样式:从基础到进阶的完整指南
前端·html
We་ct16 小时前
LeetCode 5. 最长回文子串:DP + 中心扩展
前端·javascript·算法·leetcode·typescript