导出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`)
    })
}
相关推荐
假装我不帅15 分钟前
jquery-validation使用
前端·javascript·jquery
怕浪猫20 分钟前
React从入门到出门第六章 事件代理机制与原生事件协同
前端·javascript·react.js
天府之绝23 分钟前
uniapp 中使用uview表单验证时,自定义扩展的表单,在改变时无法触发表单验证处理;
开发语言·前端·javascript·vue.js·uni-app
be or not to be25 分钟前
Html-CSS动画
前端·css·html
萧鼎33 分钟前
深入解析 Python 的 Word 模板引擎:docxtpl 全面指南
开发语言·python·word
局外人LZ35 分钟前
libsodium.js:web端与 Node.js 的现代加密工具集,构建前端安全加密体系
前端·javascript·node.js
炒茄子38 分钟前
Word:解决关于目录中各级标题的点/前导符粗细大小样式不一的问题
word
xkxnq41 分钟前
第二阶段:Vue 组件化开发(第 20天)
前端·javascript·vue.js
哈__44 分钟前
React Native 鸿蒙跨平台开发:Keyboard 键盘控制
javascript·react native·react.js
lili-felicity1 小时前
React Native 鸿蒙跨平台开发:TextInput 数据键盘实现与最大文字长度限制
javascript·react native·react.js·harmonyos