根据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`)
})
}