{"id":3428,"date":"2024-11-10T12:15:11","date_gmt":"2024-11-10T04:15:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3428\/"},"modified":"2024-11-10T12:15:11","modified_gmt":"2024-11-10T04:15:11","slug":"js-xlsx-js%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3428\/","title":{"rendered":"js-xlsx.js\u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<blockquote><p>\n  js-xlsx.js\u662f\u4e00\u4e2ajavascript\u5e93\uff0c\u7528\u4e8e\u8bfb\u5199microsoft excel\uff08.xlsx\uff09\u6587\u6863\u3002\u5b83\u7684\u4f7f\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u5b89\u88c5\u5e93\u52a0\u8f7d\u5de5\u4f5c\u7c3f\u8bbf\u95ee\u5de5\u4f5c\u8868\u904d\u5386\u5355\u5143\u683c\u8bbe\u7f6e\u5355\u5143\u683c\u503c\u4fdd\u5b58\u5de5\u4f5c\u7c3f\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u529f\u80fd\uff0c\u5982\u6837\u5f0f\u64cd\u4f5c\u3001\u56fe\u8868\u652f\u6301\u548c\u516c\u5f0f\u8ba1\u7b97\u3002\u66f4\u591a\u4fe1\u606f\u8bf7\u53c2\u8003js-xlsx.js\u6587\u6863\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110812064794394.jpg\" class=\"aligncenter\" title=\"js-xlsx.js\u5982\u4f55\u4f7f\u7528\u63d2\u56fe\" alt=\"js-xlsx.js\u5982\u4f55\u4f7f\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>js-xlsx.js\u7684\u4f7f\u7528\u6307\u5357<\/strong><\/p>\n<p><strong>\u4f55\u4e3ajs-xlsx.js\uff1f<\/strong><\/p>\n<p>js-xlsx.js\u662f\u4e00\u4e2a\u5f00\u6e90\u7684JavaScript\u5e93\uff0c\u53ef\u7528\u4e8e\u8bfb\u5199Microsoft Excel\uff08.xlsx\uff09\u6587\u6863\u3002\u5b83\u5229\u7528File API\u548cHTML5\u62d6\u653e\u529f\u80fd\uff0c\u53ef\u4ee5\u5728Web\u6d4f\u89c8\u5668\u4e2d\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u5904\u7406Excel\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528js-xlsx.js\uff1f<\/strong><\/p>\n<p><strong>1. \u5b89\u88c5\u5e93<\/strong><\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0b\u4efb\u4e00\u65b9\u5f0f\u5b89\u88c5js-xlsx.js\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528npm\uff1anpm install js-xlsx<\/li>\n<li>\u901a\u8fc7CDN\uff1a <\/li>\n<\/ul>\n<p><strong>2. \u52a0\u8f7d\u5de5\u4f5c\u7c3f<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 XLSX.readFile \u65b9\u6cd5\u4ece\u6587\u4ef6\u6216URL\u52a0\u8f7d\u5de5\u4f5c\u7c3f\uff1a<\/p>\n<pre>const workbook = XLSX.readFile(\"myfile.xlsx\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u8bbf\u95ee\u5de5\u4f5c\u8868<\/strong><\/p>\n<p>\u52a0\u8f7d\u5de5\u4f5c\u7c3f\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 workbook.Sheets \u8bbf\u95ee\u5de5\u4f5c\u8868\uff1a<\/p>\n<pre>const worksheet = workbook.Sheets.Sheet1;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u904d\u5386\u5355\u5143\u683c<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 worksheet.range \u65b9\u6cd5\u904d\u5386\u5355\u5143\u683c\uff1a<\/p>\n<pre>for (let cell of worksheet.range('A1:C5')) {\n  console.log(cell.value);\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. \u8bbe\u7f6e\u5355\u5143\u683c\u503c<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 cell.value \u5c5e\u6027\u8bbe\u7f6e\u5355\u5143\u683c\u503c\uff1a<\/p>\n<pre>worksheet.getCell('A1').value = \"New Value\";<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>6. \u4fdd\u5b58\u5de5\u4f5c\u7c3f<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u4fdd\u5b58\u5de5\u4f5c\u7c3f\uff1a<\/p>\n<ul>\n<li> <strong>\u4fdd\u5b58\u5230\u6587\u4ef6\uff1a<\/strong>XLSX.writeFile(workbook, &#8220;myfile.xlsx&#8221;)<\/li>\n<li> <strong>\u83b7\u5f97\u6570\u636eURL\uff1a<\/strong>XLSX.write(workbook, {type: &#8216;array&#8217;})<\/li>\n<\/ul>\n<p><strong>7. \u5176\u4ed6\u529f\u80fd<\/strong><\/p>\n<p>js-xlsx.js\u8fd8\u63d0\u4f9b\u5176\u4ed6\u529f\u80fd\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u6837\u5f0f\u64cd\u4f5c<\/li>\n<li>\u56fe\u8868\u652f\u6301<\/li>\n<li>\u516c\u5f0f\u8ba1\u7b97<\/li>\n<li>\u6570\u636e\u9a8c\u8bc1<\/li>\n<\/ul>\n<p><strong>\u53c2\u8003\u6587\u6863<\/strong><\/p>\n<p>\u6709\u5173\u66f4\u591a\u4fe1\u606f\u548c\u7528\u6cd5\u793a\u4f8b\uff0c\u8bf7\u53c2\u9605js-xlsx.js\u6587\u6863\uff1ahttps:\/\/hub.com\/SheetJS\/js-xlsx<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs-xlsx.js\u5982\u4f55\u4f7f\u7528\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>js-xlsx.js\u662f\u4e00\u4e2ajavascript\u5e93\uff0c\u7528\u4e8e\u8bfb\u5199microsoft excel\uff08.xlsx\uff09\u6587\u6863\u3002\u5b83\u7684\u4f7f\u7528\u65b9\u6cd5\u5305\u62ec\uff1a\u5b89\u88c5\u5e93\u52a0\u8f7d\u5de5\u4f5c\u7c3f\u8bbf\u95ee\u5de5\u4f5c\u8868\u904d\u5386\u5355\u5143\u683c\u8bbe\u7f6e\u5355\u5143\u683c\u503c\u4fdd\u5b58\u5de5\u4f5c\u7c3f\u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u529f\u80fd\uff0c\u5982\u6837\u5f0f\u64cd\u4f5c\u3001\u56fe\u8868\u652f\u6301\u548c\u516c\u5f0f\u8ba1\u7b97\u3002\u66f4\u591a\u4fe1\u606f\u8bf7\u53c2\u8003js-xlsx.js\u6587\u6863\u3002 js-xlsx.js\u7684\u4f7f\u7528\u6307\u5357 \u4f55\u4e3ajs-xlsx.js\uff1f js-xlsx.js\u662f\u4e00\u4e2a\u5f00\u6e90\u7684JavaScript\u5e93\uff0c\u53ef\u7528\u4e8e\u8bfb\u5199Microsoft Excel\uff08.xlsx\uff09\u6587\u6863\u3002\u5b83\u5229\u7528File API\u548cHTML5\u62d6\u653e\u529f\u80fd\uff0c\u53ef\u4ee5\u5728Web\u6d4f\u89c8\u5668\u4e2d\u4ee5\u7f16\u7a0b\u65b9\u5f0f\u5904\u7406Excel\u6587\u4ef6\u3002 \u5982\u4f55\u4f7f\u7528js-xlsx.js\uff1f 1. \u5b89\u88c5\u5e93 \u901a\u8fc7\u4ee5\u4e0b\u4efb\u4e00\u65b9\u5f0f\u5b89\u88c5js-xlsx.js\uff1a \u4f7f\u7528npm\uff1anpm install js-xlsx \u901a\u8fc7CDN\uff1a 2. \u52a0\u8f7d\u5de5\u4f5c\u7c3f \u53ef\u4ee5\u4f7f\u7528 XLSX.readFile \u65b9\u6cd5\u4ece\u6587\u4ef6\u6216URL\u52a0\u8f7d\u5de5\u4f5c\u7c3f\uff1a const workbook = XLSX.readFile(&#8220;myfile.xlsx&#8221;); \u767b\u5f55\u540e\u590d\u5236 3. \u8bbf\u95ee\u5de5\u4f5c\u8868 \u52a0\u8f7d\u5de5\u4f5c\u7c3f\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528 workbook.Sheets \u8bbf\u95ee\u5de5\u4f5c\u8868\uff1a const worksheet = workbook.Sheets.Sheet1; \u767b\u5f55\u540e\u590d\u5236 4. \u904d\u5386\u5355\u5143\u683c \u53ef\u4ee5\u4f7f\u7528 worksheet.range \u65b9\u6cd5\u904d\u5386\u5355\u5143\u683c\uff1a for (let cell of worksheet.range(&#8216;A1:C5&#8217;)) { console.log(cell.value); } \u767b\u5f55\u540e\u590d\u5236 5. \u8bbe\u7f6e\u5355\u5143\u683c\u503c \u53ef\u4ee5\u4f7f\u7528 cell.value \u5c5e\u6027\u8bbe\u7f6e\u5355\u5143\u683c\u503c\uff1a worksheet.getCell(&#8216;A1&#8217;).value [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3428","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3428","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=3428"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3428\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}