ToB企服应用市场:ToB评测及商务社交产业平台

标题: 前端HTML转word文档,绝对有效!!! [打印本页]

作者: 光之使者    时间: 2024-8-24 05:11
标题: 前端HTML转word文档,绝对有效!!!
2024.4.19 更新日志:有小搭档私信我element-ui等ui插件等复杂样式无法转换天生,考虑到
html-docx-js本身不支持大多c3样式及标签,先提供办理方法将dom转成图片插入文档中,只必要用标签盒子 包裹住 你必要天生的dom模块,给标签盒子添加类目的识,配置项中传入 drawCanvas:[' .btn' ] ,就可以天生 具体结果图结尾有。

2024.3.11 更新日志 :允许使用class标签在html模版中使用 此前只支持行内标签修改样式 过于繁琐与不便 ,配置项中参加 className: "xxx", 当前模版的class类明即可辨认使用


公司有业务需求 必要将前端页面一键导出word模版
后来找了半天只找到一个16年的插件html-docx-js(膜拜大神,但是已经无人维护了) ,使用后踩了许多坑,因为js版本兼容问题,运行都报错使用了with() 先版本不兼容,使用也比力繁琐,
最无法容忍的是如果是使用的在线图片还有跨域问题导致图片无法导出!!!
以是直接做了些优化处理 重要是对超时 错误图片处理 可以传入导出图片超时时间等等系列问题坑!!!已封装成傻瓜式一键导出插件,小搭档可以放心食用,现在已投入公司项目中使用。
xh-htmlword
使用方法

   npm install xh-htmlword
  1. <template>
  2.   <div class="export-box" style="width: 565pt">
  3.     <div id="main1"></div>
  4.     <p style="text-align: center">
  5.       <span
  6.         style="
  7.           font-family: 宋体;
  8.           font-weight: bold;
  9.           color: rgb(0, 0, 0);
  10.           min-height: 16pt;
  11.           font-size: 16pt;
  12.         "
  13.         >深水汴北配套管网运维日报表</span
  14.       >
  15.     </p>
  16.     <table
  17.       style="width: 565pt; border-collapse: collapse; border: 1px solid #dddddd"
  18.     >
  19.       <colgroup>
  20.         <col style="width: 89.65pt" />
  21.         <col style="width: 44.05pt" />
  22.         <col style="width: 121.5pt" />
  23.         <col style="width: 103.85pt" />
  24.         <col style="width: 73.3pt" />
  25.         <col style="width: 106.3pt" />
  26.       </colgroup>
  27.       <!-- 头部 -->
  28.       <tr
  29.         v-for="(item, index) in data.head"
  30.         :key="index"
  31.         style="border: 1px solid #cccccc; height: 25.5pt"
  32.       >
  33.         <td
  34.           v-for="(item2, index) in item"
  35.           :key="index"
  36.           style="padding-left: 5.4pt; border: 1px solid #cccccc"
  37.           :style="{ width: item2.width }"
  38.           :colspan="index % 2 == 0 ? 2 : 1"
  39.         >
  40.           <span
  41.             style="
  42.               font-weight: bold;
  43.               color: #595959;
  44.               font-size: 9pt;
  45.               margin-right: 3pt;
  46.             "
  47.           >
  48.             {{ item2.title }}</span
  49.           >
  50.           <span style="color: #595959; font-size: 9pt">
  51.             {{ item2.label }}
  52.           </span>
  53.         </td>
  54.       </tr>
  55.       <!-- 到岗人员 -->
  56.       <tr style="border: 1px solid #cccccc; height: 25.5pt">
  57.         <td colspan="6" style="padding-left: 5.4pt; border: 1px solid #cccccc">
  58.           <span
  59.             style="
  60.               font-weight: bold;
  61.               color: #595959;
  62.               font-size: 9pt;
  63.               margin-right: 3pt;
  64.             "
  65.           >
  66.             到岗人员:</span
  67.           >
  68.           <span
  69.             v-for="(item, index) in data.personnel"
  70.             :key="index"
  71.             style="color: #595959; font-size: 9pt"
  72.           >
  73.             <span v-if="index !== 0"> &nbsp;</span>
  74.             <span>{{ item.title }}</span>
  75.             <span>
  76.               {{ item.label }}
  77.             </span>
  78.           </span>
  79.         </td>
  80.       </tr>
  81.       <!-- 标准化执行情况 -->
  82.       <tr style="border: 1px solid #cccccc; height: 25.5pt">
  83.         <td colspan="6" style="padding-left: 5.4pt; border: 1px solid #cccccc">
  84.           <span
  85.             style="
  86.               font-weight: bold;
  87.               color: #595959;
  88.               font-size: 9pt;
  89.               margin-right: 3pt;
  90.             "
  91.           >
  92.             标准化执行情况:</span
  93.           >
  94.           <span
  95.             v-for="(item, index) in executionList"
  96.             :key="index"
  97.             style="color: #595959; font-size: 9pt"
  98.           >
  99.             <span v-if="index !== 0">&nbsp;&nbsp;&nbsp;</span>
  100.             <span>{{ data.execution == index ? "√" : "□" }}</span>
  101.             <span>
  102.               {{ item }}
  103.             </span>
  104.           </span>
  105.         </td>
  106.       </tr>
  107.       <!-- 巡检记录 -->
  108.       <tr style="border: 1px solid #cccccc; height: 36pt">
  109.         <td
  110.           colspan="6"
  111.           style="
  112.             text-align: center;
  113.             border: 1px solid #cccccc;
  114.             background-color: #f1f1f1;
  115.             color: rgb(89, 89, 89);
  116.             font-size: 14pt;
  117.             letter-spacing: 1pt;
  118.           "
  119.         >
  120.           巡检记录
  121.         </td>
  122.       </tr>
  123.       <tr>
  124.         <td
  125.           colspan="6"
  126.           style="
  127.             text-align: center;
  128.             border: 1px solid #cccccc;
  129.             background-color: #f1f1f1;
  130.             color: rgb(89, 89, 89);
  131.             font-size: 14pt;
  132.             letter-spacing: 1pt;
  133.           "
  134.         >
  135.           <img
  136.             src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage109.360doc.com%2FDownloadImg%2F2021%2F04%2F0713%2F219519055_1_20210407012803425&refer=http%3A%2F%2Fimage109.360doc.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1716081866&t=af941e4f60f743e1081f7cda8fcfb299"
  137.             width="565pt"
  138.             height="200pt"
  139.           />
  140.         </td>
  141.       </tr>
  142.     </table>
  143.     <div class="btn">
  144.       <el-progress :percentage="50" />
  145.       <el-progress :percentage="100" :format="format" />
  146.       <el-progress :percentage="100" status="success" />
  147.       <el-progress :percentage="100" status="warning" />
  148.       <el-progress :percentage="50" status="exception" />
  149.       <el-steps style="max-width: 600px" :active="0" finish-status="success">
  150.         <el-step title="Step 1" />
  151.         <el-step title="Step 2" />
  152.         <el-step title="Step 3" />
  153.       </el-steps>
  154.     </div>
  155.   </div>
  156.   <button @click="handleExport">导出</button>
  157. </template>
  158. <script setup>
  159. import { onMounted, nextTick } from "vue";
  160. import handleExportWord from "xh-htmlword";
  161. // 标准化执行情况
  162. const executionList = ["优秀", "良好", "中等", "差"];
  163. // 巡检记录
  164. const data = {
  165.   head: [
  166.     [
  167.       { title: "作业日期:", label: "2024年01月25日", width: "150pt" },
  168.       { title: "填表人:", label: "吴宇、白阳", width: "110pt" },
  169.       {
  170.         title: "运维单位:",
  171.         label: "安徽中益达管道疏通有限公司",
  172.         width: "200pt",
  173.       },
  174.       { title: "天气:", label: "多云(24°C)", width: "120pt" },
  175.     ],
  176.     [
  177.       {
  178.         title: "现场负责人:",
  179.         label: "吴宇",
  180.         width: "150pt",
  181.       },
  182.       {
  183.         title: "手机号码:",
  184.         label: "13112345678",
  185.         width: "110pt",
  186.       },
  187.       {
  188.         title: "项目负责人:",
  189.         label: "吴宇",
  190.         width: "205pt",
  191.       },
  192.       {
  193.         title: "手机号码:",
  194.         label: "13198765432",
  195.         width: "120pt",
  196.       },
  197.     ],
  198.   ],
  199.   personnel: [
  200.     {
  201.       title: "安全员:",
  202.       label: "1人",
  203.     },
  204.     {
  205.       title: "监护人员:",
  206.       label: "1人",
  207.     },
  208.     {
  209.       title: "施工人员:",
  210.       label: "3人",
  211.     },
  212.     {
  213.       title: "巡查人员:",
  214.       label: "2人",
  215.     },
  216.     {
  217.       title: "潜水员:",
  218.       label: "1人",
  219.     },
  220.     {
  221.       title: "有限空间作业人员:",
  222.       label: "1人",
  223.     },
  224.   ],
  225.   execution: 1,
  226. };
  227. const handleExport = () => {
  228.   // dom:需要渲染的html父盒子标签 , 类型:string 例如 id/class
  229.   //  fileName:文件名称 类型:string
  230.   //  timeOut:设置导出图片加载 超时时间 默认值 5000  (5s)
  231.   //  callBack:导出成功回调函数
  232.   //  options:配置项  类型:object 例如可传 {left:1440,right:1440} 控制页边距
  233.   //  defultImg: 错误或者超时图片 默认图片地址 类型:string
  234.   //  className:当前组件的class属性名标识 类型:string  配置此项后可以在标签写入class样式
  235.   //  drawCanvas:当当前页面有比较复杂的样式或组件(element-ui等) 页面中可以用样式标签将它包裹起来然后将标签 传入drawCanvas数组中 开启转换
  236.   handleExportWord({
  237.     dom: ".export-box",
  238.     fileName: "托尔斯泰222",
  239.     drawCanvas: [".btn"],
  240.   });
  241. };
  242. </script>
复制代码
导出结果如下:


可以看到了element UI的组件也一起导出在最下方,项目是采用的vue3+vite,现实中vue2我试了也是没问题的,插件注意事项和使用方法可以去xh-htmlword检察,如果有使用疑惑或问题可以加我QQ:1031945252 交换联系,共同进步

注意:如果是使用的vue2版本,报了以下错误:

肯定要在vue.config.js中加上配置项:

会转译node_modules中的包,办理报错!


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4