要导出的页面
导出注意点
我只测试图片中的内容
1、图片:如果是网络图片,导出之后再wps可以查看,word 不行
2、a 标签链接可以
3、内联样式可以
4、flex 布局不可以
code
/* 导出方法 */
exportHTML() {
let header =
"<html xmlns:o='urn:schemas-microsoft-com:office:office' " +
"xmlns:w='urn:schemas-microsoft-com:office:word' " +
"xmlns='http://www.w3.org/TR/REC-html40'>" +
"<head><meta charset='utf-8'><title>Export HTML to Word Document with JavaScript</title></head><body>"
let footer = '</body></html>'
let sourceHTML = header + document.getElementById(/* 导出元素 id */'word-template').innerHTML + footer
let source = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(sourceHTML)
let fileDownload = document.createElement('a')
document.body.appendChild(fileDownload)
fileDownload.href = source
fileDownload.download = 'document.doc'
fileDownload.click()
document.body.removeChild(fileDownload)
},
导出结果