当前位置: 首页 > news >正文

前端页面导出word

html-docx-js bug:
vite使用html-docx.js会报错,点击下载上方文件替换即可

正文

npm install html-docx-js -S
npm install file-saver -S
<template><div id="managerReport">word内容......</div>
</template><script><a-button type="primary" @click="wordDownload"> 导出 </a-button>function wordDownload() {const htmlContent = document.getElementById('managerReport');// 查找并修改表格的样式const tables = htmlContent.querySelectorAll('table');tables.forEach((table) => {table.style.borderCollapse = 'collapse';table.style.width = '100%';table.querySelectorAll('td, th').forEach((cell, index) => {if (cell) {cell.style.border = '1px solid black';cell.style.padding = '8px';}});});//去除<br>标签,内容加到<div>标签内const brs = htmlContent.querySelectorAll('br');brs.forEach((br) => {const parent = br.parentNode; //获取父节点let textNode = br.previousSibling; //前一个兄弟节点// while (textNode && textNode.nodeType !== Node.TEXT_NODE) {//   textNode = textNode.previousSibling;        //循环查找,直到找到一个文本节点或没有更多的兄弟节点// }if (textNode && textNode.nodeType === Node.TEXT_NODE && textNode.textContent.trim()) {//找到文本节点,并且内容不为空const div = document.createElement('div');div.textContent = textNode.textContent;parent.insertBefore(div, br);parent.removeChild(textNode); //移除原有的文本节点,避免内容重复} else {const div = document.createElement('div');div.innerHTML = '&nbsp;';parent.insertBefore(div, br);}parent.removeChild(br);});const htmlContentCopy = htmlContent.cloneNode(true);const imgs = htmlContentCopy.querySelectorAll('img');imgs.forEach((img) => {let docxWidth = 620;if (img.width > docxWidth) {img.height = (img.height * docxWidth) / img.width;img.width = docxWidth;}});// 将HTML转换为Blob对象const blob = asBlob(htmlContentCopy.innerHTML);saveAs(blob, `aaaaa.docx`);}</script>
http://www.lryc.cn/news/503346.html

相关文章:

  • 【考前预习】1.计算机网络概述
  • ubuntu20.04复现 Leg-KILO
  • Ensembl数据库下载参考基因组(常见模式植物)bioinfomatics 工具37
  • 简单介绍web开发和HTML CSS_web网站开发流程
  • Docker 中使用 PHP 通过 Canal 同步 Mysql 数据到 ElasticSearch
  • 数据结构之五:排序
  • 科研绘图系列:R语言绘制热图和散点图以及箱线图(pheatmap, scatterplot boxplot)
  • 基于 webRTC Vue 的局域网 文件传输工具
  • LeetCode 718. 最长重复子数组 java题解
  • 算法知识-15-深搜
  • 区块链dapp 开发详解(VUE3.0)
  • Plugin [id: ‘flutter‘] was not found in any of the following sources解决方法
  • 专升本-高数 1
  • 【考前预习】3.计算机网络—数据链路层
  • DockeUI 弱口令登录漏洞+未授权信息泄露
  • 【电子元器件】电感基础知识
  • 【SSH+X11】VsCode使用Remote-SSH在远程服务器的docker中打开Rviz
  • Vue Web开发(五)
  • HarmonyOS:使用Grid构建网格
  • 开源Java快速自测工具,可以调用系统内任意一个方法
  • 力扣刷题TOP101: 29.BM36 判断是不是平衡二叉树
  • 【在Linux世界中追寻伟大的One Piece】自旋锁
  • 前端编辑器JSON HTML等,vue2-ace-editor,vue3-ace-editor
  • C++ 中的运算符重载
  • 渗透测试工具 -- SQLmap安装教程及使用
  • 使用 Database Tools 实现高效数据查询的十大 IntelliJ IDEA 快捷键
  • SpringBoot 整合 RabbitMQ 实现流量消峰
  • 大数据挖掘建模平台案例分享
  • MySQL数据表的管理
  • SpringBoot【十三(实战篇)】集成在线接口文档Swagger2