{"id":31460,"date":"2024-11-25T17:17:22","date_gmt":"2024-11-25T09:17:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31460\/"},"modified":"2024-11-25T17:17:22","modified_gmt":"2024-11-25T09:17:22","slug":"%e6%80%8e%e4%b9%88%e7%94%a8vue%e5%af%bc%e5%87%baexcel%e8%a1%a8%e6%a0%bc%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31460\/","title":{"rendered":"\u600e\u4e48\u7528Vue\u5bfc\u51faexcel\u8868\u683c\u529f\u80fd"},"content":{"rendered":"<p style=\"text-align: left\">\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u600e\u4e48\u7528Vue\u5bfc\u51faexcel\u8868\u683c\u529f\u80fd\uff0c\u7528Vue\u5bfc\u51faexcel\u8868\u683c\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002<\/p>\n<p style=\"text-align: left\"><span style=\"color: #ff0000\"><strong>\u5f15\u8a00\uff1a<\/strong><\/span><\/p>\n<p style=\"text-align: left\">\u6700\u8fd1\u4f7f\u7528vue\u5728\u505a\u4e00\u4e2a\u540e\u53f0\u7cfb\u7edf\uff0c\u6280\u672f\u6808 vue + iView \uff0c\u5728\u9875\u9762\u4e2d\u751f\u6210\u8868\u683c\u540e\uff0c iView\u53ef\u4ee5\u5b9e\u73b0\u8868\u683c\u7684\u5bfc\u51fa\uff0c\u4e0d\u8fc7\u53ea\u80fd\u5bfc\u51facsv\u683c\u5f0f\u7684\uff0c\u5e76\u4e0d\u9002\u5408\u3002<\/p>\n<p style=\"text-align: left\">\u5982\u679c\u60f3\u8981\u5bfc\u51faExcel<\/p>\n<ul>\n<li>\n<p style=\"text-align: left\">\u5728src\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6(vendor)\u8fdb\u5165 Blob.js \u548c Export2Excel.js<\/p>\n<\/li>\n<li>\n<p style=\"text-align: left\">npm install -S file-saver \u7528\u6765\u751f\u6210\u6587\u4ef6\u7684web\u5e94\u7528\u7a0b\u5e8f<\/p>\n<\/li>\n<li>\n<p style=\"text-align: left\">npm install -S xlsx \u7535\u5b50\u8868\u683c\u683c\u5f0f\u7684\u89e3\u6790\u5668<\/p>\n<\/li>\n<li>\n<p style=\"text-align: left\">npm install -D script-loader \u5c06js\u6302\u5728\u5728\u5168\u5c40\u4e0b<\/p>\n<\/li>\n<\/ul>\n<p style=\"text-align: left\"><strong>\u8868\u7ed3\u6784<\/strong><\/p>\n<p style=\"text-align: left\">\u6e32\u67d3\u9875\u9762\u4e2d\u7684\u8868\u7ed3\u6784\u662f\u7531 columns \u5217 \u548c tableData \u884c\uff0c\u6765\u6e32\u67d3\u7684 columns \u4e3a\u8868\u5934\u6570\u636e tableData \u4e3a\u8868\u5b9e\u4f53\u5185\u5bb9<\/p>\n<pre>columns1: [\r\n   {\r\n   title: '\u5e8f\u53f7',\r\n   key: 'serNum'\r\n   },\r\n   {\r\n   title: '\u9009\u62e9',\r\n   key: 'choose',\r\n   align: 'center',\r\n   render: (h, params) =&gt; {\r\n    if (params.row.status !== '1' &amp;&amp; params.row.status !== '2') {\r\n    return h('p', [\r\n     h('checkbox', {\r\n     props: {\r\n      type: 'selection'\r\n     },\r\n     on: {\r\n      'input': (val) =&gt; {\r\n      console.log(val)\r\n      }\r\n     }\r\n     })\r\n    ])\r\n    } else {\r\n    return h('span', {\r\n     style: {\r\n     color: '#587dde',\r\n     cursor: 'pointer'\r\n     },\r\n     on: {\r\n     click: () =&gt; {\r\n      \/\/ this.$router.push({name: '', query: { id: params.row.id }})\r\n     }\r\n     }\r\n    }, '\u67e5\u770b\u8ba2\u5355')\r\n    }\r\n   }\r\n   },\r\n   ...\r\n  ],<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">tableData \u5c31\u4e0d\u5199\u4e86\uff0c\u5177\u4f53\u6570\u636e\u7ed3\u6784\u67e5\u770biViewAPI<\/p>\n<p style=\"text-align: left\">\u5728build \u76ee\u5f55\u4e0b webpack.base.conf.js \u914d\u7f6e \u6211\u4eec\u8981\u52a0\u8f7d\u65f6\u7684\u8def\u5f84<\/p>\n<pre>alias: {\r\n  'src': path.resolve(dirname, '..\/src'),\r\n }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5728\u5f53\u524d\u9875\u9762\u4e2d\u5f15\u5165\u4f9d\u8d56<\/p>\n<pre>require('script-loader!file-saver')\r\n \/\/ \u8f6c\u4e8c\u8fdb\u5236\u7528\r\n require('script-loader!src\/vendor\/Blob')\r\n \/\/ xlsx\u6838\u5fc3\r\n require('script-loader!xlsx\/dist\/xlsx.core.min')<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5f53\u6211\u4eec\u8981\u5bfc\u51fa\u8868\u683c\u6267\u884c @click \u4e8b\u4ef6\u8c03\u7528 handleDownload \u51fd\u6570<\/p>\n<pre>handleDownload() {\r\n  this.downloadLoading = true\r\n  require.ensure([], () =&gt; {\r\n   const {export_json_to_excel} = require('src\/vendor\/Export2Excel')\r\n   const tHeader = Util.cutValue(this.columns1, 'title')\r\n   const filterVal = Util.cutValue(this.columns1, 'key')\r\n   const list = this.tableData1\r\n   const data = this.formatJson(filterVal, list)\r\n   export_json_to_excel(tHeader, data, '\u5217\u8868excel')\r\n   this.downloadLoading = false\r\n  })\r\n  },\r\n  formatJson(filterVal, jsonData) {\r\n  return jsonData.map(v =&gt; filterVal.map(j =&gt; v[j]))\r\n  }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">Util.cutValue \u662f\u516c\u5171\u65b9\u6cd5\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u5c06\uff0ctHeader \u548cfilterVal \u7684\u503c\u8f6c\u6210\u6570\u7ec4\u4ece\u800c\u751f\u6210\u8868\u683c<\/p>\n<pre>### Util module\r\n\/\/ \u622a\u53d6value\u503c\r\nutils.cutValue = function (target, name) {\r\n let arr = []\r\n for (let i = 0; i &lt; target.length; i++) {\r\n arr.push(target[i][name])\r\n }\r\n return arr\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">Export2Excel.js\/Blob.js \u7684\u4ee3\u7801<\/p>\n<p style=\"text-align: left\"><span style=\"color: #ff0000\"><strong>\u4e0b\u9762\u518d\u770b\u4e0bvue\u4e2dexcel\u8868\u683c\u7684\u5bfc\u5165\u548c\u5bfc\u51fa <\/strong><\/span><\/p>\n<p style=\"text-align: left\">\u6ce8\u610f\uff1avue\u4e2d\u8981\u5b9e\u73b0\u8868\u683c\u7684\u5bfc\u5165\u4e0e\u5bfc\u51fa\uff0c\u9996\u5148\u8981install\u4e24\u4e2a\u4f9d\u8d56\uff0c<\/p>\n<p style=\"text-align: left\">npm install -S file-saver xlsx&nbsp; \u548c&nbsp; npm install -D script-loader\u3002\u5176\u6b21\uff0c\u5728\u9879\u76eesrc\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939vendor\uff08\u540d\u5b57\u968f\u610f\uff09\uff0c\u5728\u6b64\u6587\u4ef6\u5939\u4e0b\u653e\u7f6e\u4e24\u4e2a\u6587\u4ef6Blob.js\u548cExport2Excal.js\uff08\u4e0b\u8f7d\u5730\u5740\uff1ahttp:\/\/files.cnblogs.com\/files\/wangyunhui\/vendor.rar\uff09\u3002\u4e4b\u540e\u5c31\u53ef\u4ee5\u6109\u5feb\u7684\u5bfc\u5165\u5bfc\u51fa\u4e86\u5fae\u7b11\u3002<\/p>\n<p style=\"text-align: left\"><strong>1\u3001\u5bfc\u5165<\/strong><\/p>\n<pre>1.&lt;input id=\"upload\" type=\"file\" @change=\"importfxx(this)\" accept=\".csv, application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application\/vnd.ms-excel\" \/&gt; \r\nimportfxx(obj) { \r\nlet _this = this; \r\nconsole.log(\"xxxxxxxxxxxxxxxxxxxxxxxxxxx1\"); \r\nlet inputDOM = this.$refs.inputer; \r\n\/\/ \u901a\u8fc7DOM\u53d6\u6587\u4ef6\u6570\u636e \r\nthis.file = event.currentTarget.files[0]; \r\nvar rABS = false; \/\/\u662f\u5426\u5c06\u6587\u4ef6\u8bfb\u53d6\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32 \r\nvar f = this.file; \r\nvar reader = new FileReader(); \r\n\/\/if (!FileReader.prototype.readAsBinaryString) { \r\nFileReader.prototype.readAsBinaryString = function(f) { \r\nvar binary = \"\"; \r\nvar rABS = false; \/\/\u662f\u5426\u5c06\u6587\u4ef6\u8bfb\u53d6\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32 \r\nvar pt = this; \r\nvar wb; \/\/\u8bfb\u53d6\u5b8c\u6210\u7684\u6570\u636e \r\nvar outdata; \r\nvar reader = new FileReader(); \r\nreader.onload = function(e) { \r\nvar bytes = new Uint8Array(reader.result); \r\nvar length = bytes.byteLength; \r\nfor(var i = 0; i &lt; length; i++) { \r\nbinary += String.fromCharCode(bytes[i]); \r\n} \r\nvar XLSX = require('xlsx'); \r\nif(rABS) { \r\nwb = XLSX.read(btoa(fixdata(binary)), { \/\/\u624b\u52a8\u8f6c\u5316 \r\ntype: 'base64' \r\n}); \r\n} else { \r\nwb = XLSX.read(binary, { \r\ntype: 'binary' \r\n}); \r\n} \r\noutdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);\/\/outdata\u5c31\u662f\u4f60\u60f3\u8981\u7684\u4e1c\u897f \r\n} \r\nreader.readAsArrayBuffer(f); \r\n} \r\nif(rABS) { \r\nreader.readAsArrayBuffer(f); \r\n} else { \r\nreader.readAsBinaryString(f); \r\n} \r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">2.\u5bfc\u51fa<\/p>\n<pre>inportexcel: function() {\u3000\/\/\u517c\u5bb9ie10\u54e6\uff01 \r\nrequire.ensure([], () =&gt; {\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000 \r\nconst { export_json_to_excel } = require('..\/..\/vendor\/Export2Excel');\u3000\u3000\/\/\u5f15\u5165\u6587\u4ef6\u3000\u3000\u3000\u3000\u3000\u3000 \r\nconst tHeader = ['\u7528\u6237\u540d', '\u59d3\u540d', '\u90e8\u95e8', '\u804c\u4f4d', '\u90ae\u7bb1', '\u5145\u503c']; \/\/\u5c06\u5bf9\u5e94\u7684\u5c5e\u6027\u540d\u8f6c\u6362\u6210\u4e2d\u6587 \r\n\/\/ const tHeader = []; \r\n\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000 \r\nconst filterVal = ['userName', 'realName', 'department', 'position', 'email', 'money'];\/\/table\u8868\u683c\u4e2d\u5bf9\u5e94\u7684\u5c5e\u6027\u540d\u3000\u3000\u3000\u3000\u3000 \u3000\u3000\u3000 \r\nconst list = this.sels;\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000 \r\nconst data = this.formatJson(filterVal, list);\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000 \r\nexport_json_to_excel(tHeader, data, '\u5217\u8868excel');\u3000\u3000\u3000\u3000\u3000\u3000 \r\n}) \r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\"><span style=\"color: #ff0000\"><\/span><\/p>\n<p>\u76f8\u4fe1\u770b\u4e86\u672c\u6587\u6848\u4f8b\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u65b9\u6cd5\uff0c\u66f4\u591a\u7cbe\u5f69\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n<p>\u63a8\u8350\u9605\u8bfb\uff1a<\/p>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u7528Vue\u5bfc\u51faexcel\u8868\u683c\u529f\u80fd\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u600e\u4e48\u7528Vue\u5bfc\u51faexcel\u8868\u683c\u529f\u80fd\uff0c\u7528Vue\u5bfc\u51faexcel\u8868\u683c\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002 \u5f15\u8a00\uff1a \u6700\u8fd1\u4f7f\u7528vue\u5728\u505a\u4e00\u4e2a\u540e\u53f0\u7cfb\u7edf\uff0c\u6280\u672f\u6808 vue + iView \uff0c\u5728\u9875\u9762\u4e2d\u751f\u6210\u8868\u683c\u540e\uff0c iView\u53ef\u4ee5\u5b9e\u73b0\u8868\u683c\u7684\u5bfc\u51fa\uff0c\u4e0d\u8fc7\u53ea\u80fd\u5bfc\u51facsv\u683c\u5f0f\u7684\uff0c\u5e76\u4e0d\u9002\u5408\u3002 \u5982\u679c\u60f3\u8981\u5bfc\u51faExcel \u5728src\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u6587\u4ef6(vendor)\u8fdb\u5165 Blob.js \u548c Export2Excel.js npm install -S file-saver \u7528\u6765\u751f\u6210\u6587\u4ef6\u7684web\u5e94\u7528\u7a0b\u5e8f npm install -S xlsx \u7535\u5b50\u8868\u683c\u683c\u5f0f\u7684\u89e3\u6790\u5668 npm install -D script-loader \u5c06js\u6302\u5728\u5728\u5168\u5c40\u4e0b \u8868\u7ed3\u6784 \u6e32\u67d3\u9875\u9762\u4e2d\u7684\u8868\u7ed3\u6784\u662f\u7531 columns \u5217 \u548c tableData \u884c\uff0c\u6765\u6e32\u67d3\u7684 columns \u4e3a\u8868\u5934\u6570\u636e tableData \u4e3a\u8868\u5b9e\u4f53\u5185\u5bb9 columns1: [ { title: &#8216;\u5e8f\u53f7&#8217;, key: &#8216;serNum&#8217; }, { title: &#8216;\u9009\u62e9&#8217;, key: &#8216;choose&#8217;, align: &#8216;center&#8217;, render: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-31460","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=31460"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31460\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}