{"id":63,"date":"2023-07-20T09:50:40","date_gmt":"2023-07-20T01:50:40","guid":{"rendered":"https:\/\/iwebg.cn\/?p=63"},"modified":"2023-07-20T10:11:42","modified_gmt":"2023-07-20T02:11:42","slug":"%e5%89%8d%e7%ab%afexcel%e5%af%bc%e5%87%ba","status":"publish","type":"post","link":"http:\/\/iwebg.cn\/index.php\/2023\/07\/20\/%e5%89%8d%e7%ab%afexcel%e5%af%bc%e5%87%ba\/","title":{"rendered":"\u524d\u7aefExcel\u5bfc\u51fa"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\/\/\u83b7\u53d6\u5217\u8868\u6570\u636e\u4e4b\u540e\u524d\u7aef\u5bfc\u51fa\u4e3aExcel\nimport * as XLSX from 'xlsx'\nexport function MixinExportJosnToExcel (\n    json,\n    name = 'data',\n    type = 'application\/octet-stream'\n  ) {\n    const wb = { SheetNames: &#91;], Sheets: {}, Props: {} }\n    if (!Array.isArray(json)) json = &#91;json]\n    json.forEach(item =&gt; {\n      wb.SheetNames.push(item.sheet_name)\n      wb.Sheets&#91;item.sheet_name] = XLSX.utils.json_to_sheet(\n        item.sheet_values,\n        item.sheet_options\n      )\n    })\n    const wopts = { bookType: 'xlsx', bookSST: false, type: 'binary' }\n    const blob = new Blob(&#91;s2ab(XLSX.write(wb, wopts))], { type })\n    const link = document.createElement('a')\n    document.body.appendChild(link)\n    link.style.display = 'none'\n    link.href = window.URL.createObjectURL(blob)\n    link.download = `${name}.xlsx`\n    link.click()\n    \/\/ \u91ca\u653e\u8d44\u6e90\n    setTimeout(() =&gt; {\n      URL.revokeObjectURL(link.href)\n    }, 100)\n\n    function s2ab (s) {\n      if (typeof ArrayBuffer !== 'undefined') {\n        const buf = new ArrayBuffer(s.length)\n        const view = new Uint8Array(buf)\n        for (let i = 0; i !== s.length; ++i) view&#91;i] = s.charCodeAt(i) &amp; 0xff\n        return buf\n      } else {\n        const buf = new Array(s.length)\n        for (let i = 0; i !== s.length; ++i) buf&#91;i] = s.charCodeAt(i) &amp; 0xff\n        return buf\n      }\n    }\n  }\n\/\/\u4f7f\u7528\nconst json = {\n   sheet_name: '\u8868\u683c\u5bfc\u51fa\u540d\u5b57',\n   sheet_values: response.map(item =&gt; ({\n     'keyName': item.xxx,\n     'keyName': item.xxx,\n     'keyName': item.xxx,\n     'keyName': item.xxx,\n   }))\n }\n MixinExportJosnToExcel(json, '\u8868\u683c\u5bfc\u51fa\u540d\u5b57')\n\n\n\/\/\u8bf7\u6c42\u540e\u7aefBolb\n\n\/\/\u8bf7\u6c42\u4f53\u9700\u8981\u52a0\n{\n headers: {\n      'Content-Type': 'application\/x-download'\n    },\n responseType: 'blob'\/\/\u8fd9\u4e2a\u662f\u91cd\u70b9\n}\n\nconst blob = new Blob(&#91;res], { type: 'application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })\nconst url = window.URL.createObjectURL(blob)\nconst link = document.createElement('a')\nlink.href = url\nlink.download = `${this.treeNode.name}.xlsx`\nlink.click()\nURL.revokeObjectURL(url)\ndocument.removeChild(link)\n\n\n\/\/type:application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet\n\/\/\u8fd9\u4e2a\u662fxlsx\u683c\u5f0f\u7684Blob\u7c7b\u578b\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[11,12],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-10","tag-excel","tag-12"],"_links":{"self":[{"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":5,"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"predecessor-version":[{"id":68,"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/posts\/63\/revisions\/68"}],"wp:attachment":[{"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}