vue中html导出成word
vue 中将 html 中内容转换为 word 文档导出,无需模板,可以导出 echarts 图表。
使用 html-docx-js、file-saver 。
先将html中内容获取,之后将页面上的元素转成图片,然后把图片放到word文件中进行导出。
参考链接:https://blog.csdn.net/weixin_47494533/article/details/137018678
https://www.jb51.net/javascript/300311dk9.htm
1、引用
1 2 | npm install html-docx-js --save npm install file-saver --save |
2、创建 exportToWord.js,添加导出方法
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 | // exportToWord.js import htmlDocx from 'html-docx-js/dist/html-docx' ; import saveAs from 'file-saver' ; /** * 导出html为word * @param fileName 导出文件名 * @param elementClass 导出块的class */ export function exportToWord(fileName, elementClass) { //.export-box 需要转word的最大dom盒子的类名 const app = document.querySelector(elementClass); const cloneApp = app.cloneNode( true ); const canvases = app.getElementsByTagName( 'canvas' ); const cloneCanvases = cloneApp.getElementsByTagName( 'canvas' ); const promises = Array.from(canvases).map((ca, index) => { return new Promise((res) => { const url = ca.toDataURL( 'image/png' , 1); const img = new Image(); img.onload = () => { URL.revokeObjectURL(url); res(); }; img.src = url; // 生成img插入clone的dom的canvas之前 cloneCanvases[index].parentNode.insertBefore( img, cloneCanvases[index], ); }); }); // 移除原来的canvas const cloneCanvas = cloneApp.getElementsByTagName( 'canvas' ); Array.from(cloneCanvas).forEach((ca) => ca.parentNode.removeChild(ca)); Promise.all(promises).then(() => { convertImagesToBase64(cloneApp); const converted = htmlDocx.asBlob(` <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><style> ${document.head.outerHTML} </head> <body> ${cloneApp.outerHTML} </body> </html>`); saveAs(converted, fileName); }); } /** * 将image转换成base64 * @param cloneApp */ function convertImagesToBase64(cloneApp){ let regularImages = cloneApp.getElementsByTagName(' img '); let canvas = document.createElement(' canvas '); let ctx = canvas.getContext(' 2d '); for (let index = 0; index < regularImages.length; index++) { canvas.width = regularImages[index].width; canvas.height = regularImages[index].height; ctx.drawImage( regularImages[index], 0, 0, regularImages[index].width * 0.8, regularImages[index].height * 0.8, ); let ext = regularImages[index].src .substring(regularImages[index].src.lastIndexOf(' . ') + 1) .toLowerCase(); let dataURL = canvas.toDataURL(' image/ ' + ext); regularImages[index].setAttribute(' src', dataURL); } canvas.remove(); } |
3、调用方法
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 | <template> <div> <div> <el-row> <button type= "button" @click= "exportWord1" >导出word</button> </el-row> <div id= "contentDom" style= "text-align: center" class = "export-box" > <div>内容</div> </div> </div> </div> </template> <script> import { exportToWord } from '@/utils/feature/exportToWord' ; export default { name: "exportToWord" , data() { return { } }, methods: { // html转为word exportWord1() { exportToWord( 'test.docx' , '.export-box' ); }, } } </script> <style scoped> </style> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!