美文网首页程序员
JS将带图表的页面导成word

JS将带图表的页面导成word

作者: 八月方便面 | 来源:发表于2022-09-07 19:13 被阅读0次

一、使用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)

}

相关文章

网友评论

    本文标题:JS将带图表的页面导成word

    本文链接:https://www.haomeiwen.com/subject/uivonrtx.html