vue前端实现导出页面为word(两种方法)

守听  金牌会员 | 2024-12-11 10:42:50 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 874|帖子 874|积分 2622

将vue页面导出为word文档,不消写模板,直接导出即可。

第一种方法(简单版)

第一步:安装所需依赖

  1. npm install html-docx-js -S
  2. npm install file-saver -S
复制代码
第二步:创建容器,页面使用方法(简单版:导出内容为纯笔墨,没有表格、图片这些东西)


第二步:创建容器,页面使用方法(复杂版:导出内容带有表格和图片的情况 【使用了tinymce富文本编辑器会有表格和图片,然后需要导出带有表格和图片的word文档】)

注意:使用v-html更新元素的 innerHTML,html结构会被解析为标签
以下是需要导出的内容(exportContent):
  1. <div
  2.     id="managerReport"
  3.     class="checkInfoStyle">
  4.     <div v-html="exportContent"></div>
  5. </div>
复制代码
把exportContent 内容导出为word文档
下边直接写导出方法了:

  1.     // 第一种方法
  2.     wordDownload1 () {
  3.       this.$nextTick(() => {
  4.         const htmlContent = document.getElementById("managerReport") // managerReport id要对应
  5.         // 注意:直接导出表格没有边框并且不是100%撑满的,所以需要做以下的处理
  6.         // 查找并修改表格的样式
  7.         const tables = htmlContent.querySelectorAll('table');
  8.         tables.forEach(table => {
  9.           table.style.borderCollapse = 'collapse'
  10.           table.style.width = '100%'
  11.           table.querySelectorAll('td, th').forEach((cell, index) => {
  12.             if (cell){
  13.               cell.style.border = '1px solid black'
  14.               cell.style.padding = '8px'
  15.             }
  16.           })
  17.         })
  18.                 // 拿到需要导出的内容
  19.         let htmlString = htmlContent.innerHTML
  20.         // 注意:以下操作是为了解决导出内容为两端对齐的情况,如果导出内容某一行中有几个字,那这几个字就会两端对齐,格式就错乱了
  21.         // 考虑到是因为<br>标签才会两端对齐,所以做如下的操作(去除<br>标签[br标签是换行标签],把内容加到<div>标签内)
  22.         const regex = /([^>]*?)<br.*?>/gi;  // 找到结束标签 ‘ <br /> ’ 和开始标签 ‘ > ’ 中间的内容,把这部分内容放到div标签内
  23.         htmlString = htmlString.replace(regex, (match, p1) => { // p1就是找到的br标签中间的内容
  24.           let ret = ''
  25.           if (p1.trim()){
  26.             ret += `<div>${p1}</div>` // 把找到的内容放到div标签内
  27.           } else {
  28.             ret += `<div>&nbsp;</div>` // 不加此步骤,如果导出内容中间有空行就会解析不了,直接吞掉空行了
  29.           }
  30.           return ret
  31.         })
  32.         // 将HTML转换为Blob对象
  33.         const blob = htmlDocx.asBlob(htmlString);
  34.         saveAs(blob, `${this.editData.cTopicC}(${this.editData.dDate}).docx`)
  35.       })
  36.     },
  37.    // 第二种方法
  38.     wordDownload2 () {
  39.       this.$nextTick(() => {
  40.         const htmlContent = document.getElementById("managerReport")
  41.         // 查找并修改表格的样式
  42.         const tables = htmlContent.querySelectorAll('table')
  43.         tables.forEach(table => {
  44.           table.style.borderCollapse = 'collapse'
  45.           table.style.width = '100%'
  46.           table.querySelectorAll('td, th').forEach((cell, index) => {
  47.             if (cell){
  48.               cell.style.border = '1px solid black'
  49.               cell.style.padding = '8px'
  50.             }
  51.           })
  52.         })
  53.         //去除<br>标签,内容加到<div>标签内
  54.         const brs = htmlContent.querySelectorAll('br')
  55.         brs.forEach(br => {
  56.           const parent = br.parentNode                  //获取父节点
  57.           let textNode = br.previousSibling             //前一个兄弟节点
  58.           // while (textNode && textNode.nodeType !== Node.TEXT_NODE) {
  59.           //   textNode = textNode.previousSibling;        //循环查找,直到找到一个文本节点或没有更多的兄弟节点
  60.           // }
  61.           if (textNode && textNode.nodeType === Node.TEXT_NODE && textNode.textContent.trim()){ //找到文本节点,并且内容不为空
  62.             const div = document.createElement('div')
  63.             div.textContent = textNode.textContent
  64.             parent.insertBefore(div, br)
  65.             parent.removeChild(textNode)                //移除原有的文本节点,避免内容重复
  66.           } else {
  67.             const div = document.createElement('div')
  68.             div.innerHTML = '&nbsp;'
  69.             parent.insertBefore(div, br)
  70.           }
  71.           parent.removeChild(br)
  72.         })
  73.         const htmlContentCopy = htmlContent.cloneNode(true)
  74.         const imgs = htmlContentCopy.querySelectorAll('img')
  75.         imgs.forEach(img => {
  76.           let docxWidth = 620
  77.           if (img.width > docxWidth){
  78.             img.height = img.height * docxWidth / img.width
  79.             img.width = docxWidth
  80.           }
  81.         })
  82.         // 将HTML转换为Blob对象
  83.         const blob = htmlDocx.asBlob(htmlContentCopy.innerHTML)
  84.         saveAs(blob, `${this.editData.cTopicC}(${this.editData.dDate}).docx`)
  85.       })
  86.     },
复制代码
注意:在当前页面引入依赖

  1. import FileSaver from "file-saver";
  2. import htmlDocx from "html-docx-js/dist/html-docx";**
复制代码

问题:用此方法,最近碰到了一个问题,就是导出内容很少的情况,好比:导出内容只有一行或者两行、三行,并且每行只有几个字的情况,导出内容就成乱码了。如果有碰到此种情况并且有办理方案的大佬,感谢批评区分享。


第二种方法(需要使用jquery)

第一步:安装所需依赖

  1. npm install jquery --save
  2. npm install file-saver
复制代码
第二步:创建两个js文件,一个是jquery文件(jq.js),一个是插件js的文件(jquery.wordexport.js),我把这两个js文件都放到utils文件夹下,注意:使用的时候肯定要注意引用路径。这两个js文件代码我都放到文章末了(有一个插件没有依赖包,所以需要本身创建一个js文件(jquery.wordexport.js))

第三步:在需要导出的页面引入文件

  1. import $ from "@/utils/jq"; // 文件引入路径一定要正确,这是第二步创建的js文件(jq.js)
  2. import saveAs from "file-saver/dist/FileSaver";
  3. import "@/utils/jquery.wordexport"; // 文件引入路径一定要正确,这是第二步创建的js文件(jquery.wordexport.js)
复制代码
第四步:页面使用方法


注意:如果导出的时候出现bug,大多是由于文件路径引入有问题,再次排查路径引入
jq.js

  1. import $ from "jquery";
  2.   window.$ = $;
  3.   window.jQuery = $;
  4. export default $;
复制代码
jquery.wordexport.js

  1. if (typeof jQuery !== "undefined" && typeof saveAs !== "undefined") {
  2.   (function ($) {
  3.       $.fn.wordExport = function (fileName) {
  4.           fileName = typeof fileName !== 'undefined' ? fileName : "jQuery-Word-Export";
  5.           var static = {
  6.               mhtml: {
  7.                   top:
  8.                     "Mime-Version: 1.0
  9. Content-Base: " +
  10.                     location.href +
  11.                     '
  12. Content-Type: Multipart/related; boundary="NEXT.ITEM-BOUNDARY";type="text/html"
  13. --NEXT.ITEM-BOUNDARY
  14. Content-Type: text/html; charset="utf-8"
  15. Content-Location: ' +
  16.                     location.href +
  17.                     "
  18. <!DOCTYPE html>
  19. " +
  20.                     '<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
  21. _html_</html>',
  22.                   head:
  23.                     '<head>
  24. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  25. <style>
  26. _styles_
  27. </style>
  28. <!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:TrackMoves>false</w:TrackMoves><w:TrackFormatting/><w:ValidateAgainstSchemas/><w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid><w:IgnoreMixedContent>false</w:IgnoreMixedContent><w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText><w:DoNotPromoteQF/><w:LidThemeOther>EN-US</w:LidThemeOther><w:LidThemeAsian>ZH-CN</w:LidThemeAsian><w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript><w:Compatibility><w:BreakWrappedTables/><w:SnapToGridInCell/><w:WrapTextWithPunct/><w:UseAsianBreakRules/><w:DontGrowAutofit/><w:SplitPgBreakAndParaMark/><w:DontVertAlignCellWithSp/><w:DontBreakConstrainedForcedTables/><w:DontVertAlignInTxbx/><w:Word11KerningPairs/><w:CachedColBalance/><w:UseFELayout/></w:Compatibility><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><m:mathPr><m:mathFont m:val="Cambria Math"/><m:brkBin m:val="before"/><m:brkBinSub m:val="--"/><m:smallFrac m:val="off"/><m:dispDef/><m:lMargin m:val="0"/> <m:rMargin m:val="0"/><m:defJc m:val="centerGroup"/><m:wrapIndent m:val="1440"/><m:intLim m:val="subSup"/><m:naryLim m:val="undOvr"/></m:mathPr></w:WordDocument></xml><![endif]--></head>
  29. ',
  30.                   body: "<body>_body_</body>",
  31.               },
  32.           };
  33.           var options = {
  34.               maxWidth: 624,//最大宽度
  35.           };
  36.           // Clone selected element before manipulating it
  37.           var markup = $(this).clone();
  38.           // Remove hidden elements from the output
  39.           markup.each(function () {
  40.               var self = $(this);
  41.               if (self.is(':hidden'))
  42.                   self.remove();
  43.           });
  44.           // Embed all images using Data URLs
  45.           var images = Array();
  46.           var img = markup.find('img');
  47.           // var img = new Image(); 用这一行的话,WPS不显示图片,用上面的——只兼容office Word。
  48.           var mhtmlBottom = "
  49. ";
  50.           for (var i = 0; i < img.length; i++) {
  51.               // Calculate dimensions of output image
  52.               var w = Math.min(img[i].width == 0 ? options.maxWidth : img[i].width, options.maxWidth);
  53.               var h = (img[i].height == 0 ? options.defaultLength : img[i].height) * (w / (img[i].width == 0 ? options.maxWidth : img[i].width));
  54.               // Create canvas for converting image to data URL
  55.               var canvas = document.createElement("CANVAS");
  56.               canvas.width = w;
  57.               canvas.height = h;
  58.               // Draw image to canvas
  59.               var context = canvas.getContext('2d');
  60.               context.drawImage(img[i], 0, 0, w, h);
  61.               // Get data URL encoding of image
  62.               var uri = canvas.toDataURL("image/png");
  63.               // console.log(i+":"+uri);
  64.               $(img[i]).attr("src", img[i].src);
  65.               img[i].width = w;
  66.               img[i].height = h;
  67.               mhtmlBottom += "--NEXT.ITEM-BOUNDARY
  68. ";
  69.               mhtmlBottom += "Content-Location: " + uri + "
  70. ";
  71.               mhtmlBottom += "Content-Type: " + uri.substring(uri.indexOf(":") + 1, uri.indexOf(";")) + "
  72. ";
  73.               mhtmlBottom += "Content-Transfer-Encoding: " + uri.substring(uri.indexOf(";") + 1, uri.indexOf(",")) + "
  74. ";
  75.               mhtmlBottom += uri.substring(uri.indexOf(",") + 1) + "
  76. ";
  77.           }
  78.           mhtmlBottom += "--NEXT.ITEM-BOUNDARY--";
  79.           //TODO: load css from included stylesheet
  80.           var styles = "";
  81.           // Aggregate parts of the file together
  82.           var fileContent = static.mhtml.top.replace("_html_", static.mhtml.head.replace("_styles_", styles) + static.mhtml.body.replace("_body_", markup.html())) + mhtmlBottom;
  83.           // Create a Blob with the file contents
  84.           var blob = new Blob([fileContent], {
  85.               type: "application/msword;charset=utf-8"
  86.           });
  87.           saveAs(blob, fileName + ".doc"); // 注意:不要改成docx,不然会打不开!!!
  88.          
  89.       };
  90.   })(jQuery);
  91. } else {
  92.   if (typeof jQuery === "undefined") {
  93.       console.error("jQuery Word Export: missing dependency (jQuery)");
  94.   }
  95.   if (typeof saveAs === "undefined") {
  96.       console.error("jQuery Word Export: missing dependency (FileSaver.js)");
  97.   }
  98. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

守听

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表