vue前端下载word 文档
1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | import { asBlob } from 'html-docx-js-typescript' // 要保存这个docx文件推荐引入file-saver哦,你可以用npm i -D file-saver来安装 import { saveAs } from 'file-saver' import html2canvas from 'html2canvas' async exportWord () { this .wordLoading = true try { await html2canvas( this .$refs.icmptimesImg, { //scale: -1,//缩放, dpi: 800, useCORS: true , }).then((canvas) => { this .icmptimesImgUrl = canvas.toDataURL() }) } catch (error) { console.log( "html2canvas error!" ) this .wordLoading = false } try { await html2canvas( this .$refs.tcptimesImg, { // scale: -1,//缩放, dpi: 800, useCORS: true }).then((canvas) => { this .tcptimesImgUrl = canvas.toDataURL() }) } catch (error) { console.log( "html2canvas error!" ) this .wordLoading = false } let content = this .$refs.wordhtml.innerHTML content = this .insertImg(content, 'id="icmptimesImg"' , this .icmptimesImgUrl) content = this .insertImg(content, 'id="tcptimesImg"' , this .tcptimesImgUrl) content = content.replace(/\b(el-tag el-tag--warning el-tag--dark|el-tag el-tag--success el-tag--dark|el-tag el-tag--dark|el-tag el-tag--danger el-tag--dark)\b/g, function ($0, $1, $2, $3) { return { "el-tag el-tag--warning el-tag--dark" : "el-tag--warning" , "el-tag el-tag--success el-tag--dark" : "el-tag--success" , "el-tag el-tag--dark" : "el-tag--dark" , "el-tag el-tag--danger el-tag--dark" : "el-tag--danger" }[$1]; }); let cssstyle = `html { font-size: 14px; line-height: 1.15; } body { height: 100%; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif; } table { width: 100%; border-collapse: collapse; border-spacing: 2px; border-spacing: 2px; table-layout: fixed; word- break : break -all; word-wrap: break -word; } table td, table th { border: 1px solid #cad9ea; color: #666; height: 30px; padding-left: 10px; } table td { word- break : break -all; word-wrap: break -word; } table>tr { vertical-align: middle; } tr { display: table-row; border-color: inherit; } .td-title { color: #000; background-color: #e8e8e8; } .el-tag--dark { height: 30px; margin: 5px 0px 5px 10px; display: inline-block; padding: 0 10px; line-height: 30px; font-size: 12px; border-width: 1px; border-style: solid; border-radius: 4px; box-sizing: border-box; white-space: nowrap; background-color: #409eff; border-color: #409eff; color: #fff; } .el-tag--success { height: 30px; margin: 5px 0px 5px 10px; display: inline-block; padding: 0 10px; line-height: 30px; font-size: 12px; border-width: 1px; border-style: solid; border-radius: 4px; box-sizing: border-box; white-space: nowrap; background-color: #67c23a; border-color: #67c23a; color: #fff; } .el-tag--warning { height: 30px; margin: 5px 0px 5px 10px; display: inline-block; padding: 0 10px; line-height: 30px; font-size: 12px; border-width: 1px; border-style: solid; border-radius: 4px; box-sizing: border-box; white-space: nowrap; background-color: #e6a23c; border-color: #e6a23c; color: #fff; } .el-tag--danger { height: 30px; margin: 5px 0px 5px 10px; display: inline-block; padding: 0 10px; line-height: 30px; font-size: 12px; border-width: 1px; border-style: solid; border-radius: 4px; box-sizing: border-box; white-space: nowrap; background-color: #f56c6c; border-color: #f56c6c; color: #fff; }` const htmlString = `<!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title>Document</title> <style> ${cssstyle} </style> </head> <body> ${content} </body> </html>` try { const fileData = asBlob(htmlString).then(data => { saveAs(data, '测试报告.docx' ) // 保存为docx文件 this .wordLoading = false }) // asBlob() 返回 Promise<Blob|Buffer>,用promise.then还是async语法都行 } catch (error) { this .wordLoading = false } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)