本文主要是介绍导出word文件 表格file-saver、html-docx-js-typescript,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
根据html代码转换为word文件
1、下载file-saver、html-docx-js-typescript
npm install file-saver html-docx-js-typescript
2、
<span @click="downloadWordWithHtmlString()">导出doc</span>
<div id="eltable" ref="eltable" style="display: none"><tableborder="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.innerHTMLlet html = document.getElementById('eltable').innerHTMLlet 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`)})
}
这篇关于导出word文件 表格file-saver、html-docx-js-typescript的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!