一、使用html2canvas将图表的canvas转为image图片;
二、使用js将页面导出;
三、如果是表格导成word会带边框,运用"border": "1px white solid"属性,将边框设为白色;
代码如下:
html2canvas($(this)[0], { allowTaint: !0, taintTest: !1 }).then(function(t) {
try {
var img = new Image();
img.src = t.toDataURL("image/png", 1);
} catch (t) { console.log("导出生成图片失败") }
})
function exportWord(){
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 */ 'page1').innerHTML + footer
let fileName = document.title
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 = `${fileName}.doc`
fileDownload.click()
document.body.removeChild(fileDownload)
}
网友评论