近期作的項目也是奇葩,頁面上須要導出excel,pdf,還有導出圖片等。。。,剛把前幾個慫好,還有下載成word文件,如何處理?html
function getOutword (id, fileName) { /* id :文檔dom節點 fileName:文件名稱(.doc) */ var 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>" var footer = '</body></html>' var sourceHTML = header + document.getElementById(id).innerHTML + footer var source = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(sourceHTML) var fileDownload = document.createElement('a') document.body.appendChild(fileDownload) fileDownload.href = source fileDownload.download = fileName // 下載名稱 fileDownload.click() document.body.removeChild(fileDownload) } export default getOutword
頁面使用:getOutword('downloadwrap', '名字.doc')app
備註:dom
1.頁面須要內聯樣式,其餘樣式不起做用spa
2.IE上面有兼容性問題,未作處理。若是有好的方式可留言哦excel