{"id":1008,"date":"2023-03-12T09:01:08","date_gmt":"2023-03-12T01:01:08","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1008"},"modified":"2023-04-29T13:17:31","modified_gmt":"2023-04-29T05:17:31","slug":"vue-js-download-binary-stream-and-convert-it-to-excel-file","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/vue-js-download-binary-stream-and-convert-it-to-excel-file\/","title":{"rendered":"Vue.js\u4e0b\u8f7d\u4e8c\u8fdb\u5236\u6d41\u6587\u4ef6\u5e76\u8f6c\u4e3aExcel\u6587\u4ef6"},"content":{"rendered":"<p>\u6d41\u7a0b\u539f\u7406\uff1a<\/p>\n<p>1\u3001\u83b7\u53d6\u540e\u7aef\u4f20\u9012\u6765\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u6d41<br \/>\n2\u3001\u4f20\u9012\u5230\u5c01\u88c5\u7684\u51fd\u6570\u5f53\u4e2d\uff0c\u9700\u8981\u4f20\u9012\u4e8c\u8fdb\u5236\u6570\u636e\u548c\u6587\u4ef6\u540d\uff08\u4e0d\u4f20\u6709\u9ed8\u8ba4\u6587\u4ef6\u540d\uff09<br \/>\n3\u3001\u5c06\u4e8c\u8fdb\u5236\u6570\u636e\u6d41\u5305\u88f9\u6210\u4e00\u4e2anew Blob\u5bf9\u8c61<br \/>\n4\u3001\u5c06Blob\u5bf9\u8c61\u8f6c\u5316\u4e3a\u4e00\u4e2aURL\u8d44\u6e90\u5730\u5740\uff0c\u8fd9\u4e2a\u5730\u5740\u65f6\u4e00\u4e2a\u672c\u5730\u5730\u5740<br \/>\n5\u3001\u521b\u5efa\u4e00\u4e2aa\u6807\u7b7e\uff0c\u8bbe\u7f6e\u9690\u85cf\uff0c\u6dfb\u52a0\u4e0b\u8f7d\u5c5e\u6027\uff0c\u6dfb\u52a0\u5230body\u5f53\u4e2d\uff0c\u542f\u52a8\u4e0b\u8f7d<br \/>\n6\u3001\u4e0b\u8f7d\u5b8c\u6bd5\u4e4b\u540e\uff0c\u5220\u9664a\u6807\u7b7e<\/p>\n<p><!-- more --><\/p>\n<p>\u4ee5\u4e0b\u4e3a\u4e0b\u8f7dExcel\u6587\u4ef6\u7684\u6838\u5fc3\u903b\u8f91\uff0c\u4f20\u5165\u4e8c\u8fdb\u5236\u6d41data\u548c\u6587\u4ef6\u540dfileName\u5373\u53ef<\/p>\n<pre><code class=\"language-javascript\">let blob = new Blob([data], {\n  type: &#039;application\/octet-stream&#039;\n}); \/\/ \u8f6c\u5316\u4e3ablob\u5bf9\u8c61\nlet filename = fileName || &#039;filename.xls&#039;; \/\/ \u5224\u65ad\u662f\u5426\u4f7f\u7528\u9ed8\u8ba4\u6587\u4ef6\u540d\nif (typeof window.navigator.msSaveBlob !== &#039;undefined&#039;) {\n  window.navigator.msSaveBlob(blob, filename);\n} else {\n  var blobURL = window.URL.createObjectURL(blob); \/\/ \u5c06blob\u5bf9\u8c61\u8f6c\u4e3a\u4e00\u4e2aURL\n  var tempLink = document.createElement(&#039;a&#039;); \/\/ \u521b\u5efa\u4e00\u4e2aa\u6807\u7b7e\n  tempLink.style.display = &#039;none&#039;;\n  tempLink.href = blobURL;\n  tempLink.setAttribute(&#039;download&#039;, filename); \/\/ \u7ed9a\u6807\u7b7e\u6dfb\u52a0\u4e0b\u8f7d\u5c5e\u6027\n  if (typeof tempLink.download === &#039;undefined&#039;) {\n    tempLink.setAttribute(&#039;target&#039;, &#039;_blank&#039;);\n  }\n  document.body.appendChild(tempLink); \/\/ \u5c06a\u6807\u7b7e\u6dfb\u52a0\u5230body\u5f53\u4e2d\n  tempLink.click(); \/\/ \u542f\u52a8\u4e0b\u8f7d\n  document.body.removeChild(tempLink); \/\/ \u4e0b\u8f7d\u5b8c\u6bd5\u5220\u9664a\u6807\u7b7e\n  window.URL.revokeObjectURL(blobURL);\n}<\/code><\/pre>\n<blockquote>\n<p>\u6ce8\u610f\u4e8b\u9879\uff1a\u5728vue\u6846\u67b6\u5f53\u4e2d\uff0c\u6570\u636e\u8bf7\u6c42\u662f\u501f\u52a9axios\u7684\uff0c\u4e3a\u6b64\uff0c\u5728\u53d1\u9001\u8bf7\u6c42\u65f6\uff0c\u9700\u8981\u4fee\u6539responseType\uff0c\u6539\u4e3aarraybuffer\uff0caxios\u9ed8\u8ba4\u60c5\u51b5\u4e0bresponseType\u4e3ajson\uff0c\u82e5\u662f\u4e0d\u4fee\u6539\uff0c\u5f88\u53ef\u80fd\u4e0b\u8f7d\u65f6\u5019\u4f1a\u662f\u4e71\u7801\uff0c\u6216\u8005\u4e3anull\u3002<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u6d41\u7a0b\u539f\u7406\uff1a 1\u3001\u83b7\u53d6\u540e\u7aef\u4f20\u9012\u6765\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u6d41 2\u3001\u4f20\u9012\u5230\u5c01\u88c5\u7684\u51fd\u6570\u5f53\u4e2d\uff0c\u9700\u8981\u4f20\u9012\u4e8c\u8fdb\u5236\u6570\u636e\u548c\u6587\u4ef6\u540d\uff08\u4e0d\u4f20\u6709\u9ed8\u8ba4 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[],"class_list":["post-1008","post","type-post","status-publish","format-standard","hentry","category-vue"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1008","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1008"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1008\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}