导出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`)
    })
}
相关推荐
丷丩43 分钟前
MapLibre GL JS第27课:添加COG栅格源
javascript·map·mapbox·maplibre gl js
不好听6132 小时前
JavaScript 到底是怎么运行的?从编译阶段到执行上下文全面解析
javascript
丷丩2 小时前
MapLibre GL JS第29课:添加Canvas源
javascript·gis·map·mapbox·maplibre gl js
就叫_这个吧2 小时前
HTML常用标签及举例使用
前端·html
utf8mb4安全女神3 小时前
【rsyslog服务】把所有服务的“临界点”以上的错误都保存在/var/log/alert.log⽇志中
java·前端·javascript
csdn_aspnet3 小时前
javascript 算法 LeetCode 编号 70 - 爬楼梯
开发语言·javascript·算法·leetcode·ecmascript
swipe3 小时前
DeepAgents 多 Agent 深度调研助手工程实战:从 createDeepAgent 到可控调研工作流
javascript·面试·langchain
moMo3 小时前
JavaScript 变量提升,执行上下文里的各种门道
javascript·面试
weixin_471383033 小时前
由浅入深递归练习
前端·javascript·vue.js
gc_22994 小时前
学习C#调用OpenXml操作word文档的基本用法(38:学习特殊符号类-2)
word·openxml·特殊符号