{"id":52809,"date":"2024-12-03T13:30:46","date_gmt":"2024-12-03T05:30:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52809\/"},"modified":"2024-12-03T13:30:46","modified_gmt":"2024-12-03T05:30:46","slug":"exceljs%e5%ba%93%e5%af%bc%e5%87%baexcel%e5%8d%a1%e9%a1%bf%ef%bc%8c%e5%a6%82%e4%bd%95%e6%8f%90%e5%8d%87%e5%af%bc%e5%87%ba%e6%95%88%e7%8e%87%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52809\/","title":{"rendered":"ExcelJS\u5e93\u5bfc\u51faExcel\u5361\u987f\uff0c\u5982\u4f55\u63d0\u5347\u5bfc\u51fa\u6548\u7387\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>ExcelJS\u5e93\u5bfc\u51faExcel\u5361\u987f\uff0c\u5982\u4f55\u63d0\u5347\u5bfc\u51fa\u6548\u7387\uff1f<\/h1>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aExcelJS\u5e93\u5bfc\u51faExcel\u5361\u987f\uff0c\u5982\u4f55\u63d0\u5347\u5bfc\u51fa\u6548\u7387\uff1f\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241123\/17323540606741a00c7f1bf.jpg\" class=\"aligncenter\" title=\"ExcelJS\u5e93\u5bfc\u51faExcel\u5361\u987f\uff0c\u5982\u4f55\u63d0\u5347\u5bfc\u51fa\u6548\u7387\uff1f\u63d2\u56fe\" alt=\"ExcelJS\u5e93\u5bfc\u51faExcel\u5361\u987f\uff0c\u5982\u4f55\u63d0\u5347\u5bfc\u51fa\u6548\u7387\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>exceljs\u5e93\u5bfc\u51faexcel\u5361\u987f\uff1f<\/strong><\/p>\n<p>\u5728\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u9700\u8981\u5bfc\u51fa\u53ef\u90e8\u5206\u7f16\u8f91\u7684excel\u8868\uff0c\u4f46\u5728\u6dfb\u52a0\u4e86\u4fdd\u62a4\u5de5\u4f5c\u8868\u548c\u89e3\u9501\u6307\u5b9a\u5217\u7684\u4ee3\u7801\u540e\uff0c\u5bfc\u51fa\u901f\u5ea6\u660e\u663e\u53d8\u6162\u3002<\/p>\n<p>\u4e3a\u4e86\u63d0\u5347\u5bfc\u51fa\u6548\u7387\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u65b9\u6848\uff1a<\/p>\n<ol>\n<li><strong>\u6279\u91cf\u64cd\u4f5c\uff1a<\/strong>\u51cf\u5c11\u9010\u4e2a\u5355\u5143\u683c\u7684\u64cd\u4f5c\uff0c\u5c1d\u8bd5\u4e00\u6b21\u6027\u6279\u91cf\u5904\u7406\u3002<\/li>\n<li><strong>\u4f7f\u7528\u6a21\u677f\uff1a<\/strong>\u9884\u5148\u521b\u5efa\u4e00\u4e2a\u6a21\u677f\u6587\u4ef6\uff0c\u5728\u5176\u4e2d\u8bbe\u7f6e\u597d\u4fdd\u62a4\u548c\u89e3\u9501\u7684\u5217\uff0c\u7136\u540e\u5c06\u6570\u636e\u5199\u5165\u6a21\u677f\u3002<\/li>\n<li><strong>\u4f18\u5316\u4ee3\u7801\u7ed3\u6784\uff1a<\/strong>\u6539\u8fdb\u4ee3\u7801\u7ed3\u6784\uff0c\u4f7f\u4ee3\u7801\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u6570\u636e\u3002\u793a\u4f8b\u4f18\u5316\u4ee3\u7801\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre>const ExcelJS = require('exceljs');\n\nasync function exportExcel(data) {\n  const workbook = new ExcelJS.Workbook();\n  const sheet = workbook.addWorksheet('Sheet1');\n\n  \/\/ \u6dfb\u52a0\u6570\u636e\n  data.forEach((row) =&gt; {\n    sheet.addRow(row);\n  });\n\n  \/\/ \u4fdd\u62a4\u6574\u4e2a\u5de5\u4f5c\u8868\n  sheet.protect('yourpassword', {\n    selectLockedCells: true,\n    selectUnlockedCells: true,\n  });\n\n  \/\/ \u6279\u91cf\u89e3\u9501\u6307\u5b9a\u5217\u7684\u5355\u5143\u683c\n  const unlockColumns = [6, 7, 8, 9, 12];\n  sheet.columns.forEach((col, colNumber) =&gt; {\n    if (unlockColumns.includes(colNumber + 1)) {\n      col.eachCell((cell, rowNumber) =&gt; {\n        if (rowNumber !== 1) { \/\/ \u8df3\u8fc7\u6807\u9898\u884c\n          cell.protection = { locked: false };\n        }\n      });\n    }\n  });\n\n  \/\/ \u5bfc\u51faExcel\u6587\u4ef6\n  await workbook.xlsx.writeFile('output.xlsx');\n}\n\n\/\/ \u793a\u4f8b\u6570\u636e\nconst data = [\n  ['Header1', 'Header2', 'Header3', 'Header4', 'Header5', 'Header6'],\n  ['Row1-Col1', 'Row1-Col2', 'Row1-Col3', 'Row1-Col4', 'Row1-Col5', 'Row1-Col6'],\n  \/\/ \u66f4\u591a\u6570\u636e...\n];\n\n\/\/ \u5bfc\u51faExcel\nexportExcel(data);<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aExcelJS\u5e93\u5bfc\u51faExcel\u5361\u987f\uff0c\u5982\u4f55\u63d0\u5347\u5bfc\u51fa\u6548\u7387\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u95ee\u53f7 (?) \u7684\u4f5c\u7528\uff1a\u4e3a\u4ec0\u4e48\u5339\u914d\u624b\u673a\u53f7\u7801\u65f6\u8981\u4f7f\u7528 0? \u800c\u4e0d\u662f\u76f4\u63a5\u4f7f\u7528 1?\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>ExcelJS\u5e93\u5bfc\u51faExcel\u5361\u987f\uff0c\u5982\u4f55\u63d0\u5347\u5bfc\u51fa\u6548\u7387\uff1f \u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9\u6587\u7ae0\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587\u300aExcelJS\u5e93\u5bfc\u51faExcel\u5361\u987f\uff0c\u5982\u4f55\u63d0\u5347\u5bfc\u51fa\u6548\u7387\uff1f\u300b\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~ exceljs\u5e93\u5bfc\u51faexcel\u5361\u987f\uff1f \u5728\u5e94\u7528\u573a\u666f\u4e2d\uff0c\u9700\u8981\u5bfc\u51fa\u53ef\u90e8\u5206\u7f16\u8f91\u7684excel\u8868\uff0c\u4f46\u5728\u6dfb\u52a0\u4e86\u4fdd\u62a4\u5de5\u4f5c\u8868\u548c\u89e3\u9501\u6307\u5b9a\u5217\u7684\u4ee3\u7801\u540e\uff0c\u5bfc\u51fa\u901f\u5ea6\u660e\u663e\u53d8\u6162\u3002 \u4e3a\u4e86\u63d0\u5347\u5bfc\u51fa\u6548\u7387\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u65b9\u6848\uff1a \u6279\u91cf\u64cd\u4f5c\uff1a\u51cf\u5c11\u9010\u4e2a\u5355\u5143\u683c\u7684\u64cd\u4f5c\uff0c\u5c1d\u8bd5\u4e00\u6b21\u6027\u6279\u91cf\u5904\u7406\u3002 \u4f7f\u7528\u6a21\u677f\uff1a\u9884\u5148\u521b\u5efa\u4e00\u4e2a\u6a21\u677f\u6587\u4ef6\uff0c\u5728\u5176\u4e2d\u8bbe\u7f6e\u597d\u4fdd\u62a4\u548c\u89e3\u9501\u7684\u5217\uff0c\u7136\u540e\u5c06\u6570\u636e\u5199\u5165\u6a21\u677f\u3002 \u4f18\u5316\u4ee3\u7801\u7ed3\u6784\uff1a\u6539\u8fdb\u4ee3\u7801\u7ed3\u6784\uff0c\u4f7f\u4ee3\u7801\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u6570\u636e\u3002\u793a\u4f8b\u4f18\u5316\u4ee3\u7801\u5982\u4e0b\uff1a const ExcelJS = require(&#8216;exceljs&#8217;); async function exportExcel(data) { const workbook = new ExcelJS.Workbook(); const sheet = workbook.addWorksheet(&#8216;Sheet1&#8217;); \/\/ \u6dfb\u52a0\u6570\u636e data.forEach((row) =&gt; { sheet.addRow(row); }); \/\/ \u4fdd\u62a4\u6574\u4e2a\u5de5\u4f5c\u8868 sheet.protect(&#8216;yourpassword&#8217;, { selectLockedCells: true, selectUnlockedCells: true, }); \/\/ \u6279\u91cf\u89e3\u9501\u6307\u5b9a\u5217\u7684\u5355\u5143\u683c const unlockColumns = [6, 7, 8, 9, 12]; sheet.columns.forEach((col, colNumber) =&gt; { [&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-52809","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52809","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=52809"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52809\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}