{"id":52876,"date":"2024-12-03T08:55:30","date_gmt":"2024-12-03T00:55:30","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52876\/"},"modified":"2024-12-03T08:55:30","modified_gmt":"2024-12-03T00:55:30","slug":"excel-js-react-js","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52876\/","title":{"rendered":"Excel js + React JS"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Excel js + React JS<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aExcel js + React JS\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241124\/17324233796742aed3f0460.jpg\" class=\"aligncenter\" title=\"Excel js + React JS\u63d2\u56fe\" alt=\"Excel js + React JS\u63d2\u56fe\" \/><\/p>\n<p>excel\u5e7f\u6cdb\u7528\u4e8e\u5404\u79cd\u6570\u636e\u62a5\u544a\u3002\u5728reactjs\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528exceljs\u5e93\u52a8\u6001\u521b\u5efaexcel\u6587\u4ef6\u3002\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u5728 react \u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0 exceljs \u4ee5\u521b\u5efa\u548c\u4e0b\u8f7d excel \u62a5\u544a\u3002<\/p>\n<p><strong>\u8bbe\u7f6e\u548c\u5b89\u88c5<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u5b89\u88c5exceljs\u5e93\u3002\u6253\u5f00\u7ec8\u7aef\u5e76\u5728 react \u9879\u76ee\u76ee\u5f55\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>npm install exceljs file-saver\n<\/pre>\n<p>exceljs \u5e93\u5c06\u7528\u4e8e\u521b\u5efa\u548c\u64cd\u4f5c\u5de5\u4f5c\u7c3f\uff08excel \u6587\u4ef6\uff09\uff0c\u800c file-saver \u5c06\u5904\u7406\u6d4f\u89c8\u5668\u4e2d\u7684\u6587\u4ef6\u4e0b\u8f7d\u3002<\/p>\n<p><strong>\u7b2c 1 \u6b65\uff1a\u521b\u5efa\u5bfc\u51fa excel \u51fd\u6570<\/strong><br \/> \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ec4\u4ef6\u6587\u4ef6\uff0c\u4f8b\u5982 exporttoexcel.js\u3002\u5728\u6b64\u7ec4\u4ef6\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2aexportexcel\u51fd\u6570\u6765\u5904\u7406\u5de5\u4f5c\u7c3f\u548c\u5de5\u4f5c\u8868\u7684\u521b\u5efa\uff0c\u4ee5\u53ca\u4fdd\u5b58\u751f\u6210\u7684excel\u6587\u4ef6\u3002<\/p>\n<pre>import react from 'react';\nimport exceljs from 'exceljs';\nimport { saveas } from 'file-saver';\n\nconst exporttoexcel = ({ data, filename }) =&gt; {\n  const exportexcel = async () =&gt; {\n    \/\/ 1. create a new workbook\n    const workbook = new exceljs.workbook();\n    const worksheet = workbook.addworksheet('data report');\n\n    \/\/ 2. define the table headers\n    worksheet.columns = [\n      { header: 'id', key: 'id', width: 10 },\n      { header: 'name', key: 'name', width: 30 },\n      { header: 'email', key: 'email', width: 30 },\n      { header: 'join date', key: 'joindate', width: 20 },\n    ];\n\n    \/\/ 3. insert data into the worksheet\n    data.foreach((item) =&gt; {\n      worksheet.addrow({\n        id: item.id,\n        name: item.name,\n        email: item.email,\n        joindate: item.joindate,\n      });\n    });\n\n    \/\/ 4. style the header\n    worksheet.getrow(1).eachcell((cell) =&gt; {\n      cell.font = { bold: true };\n      cell.alignment = { horizontal: 'center' };\n    });\n\n    \/\/ 5. save the workbook as an excel file\n    const buffer = await workbook.xlsx.writebuffer();\n    const blob = new blob([buffer], { type: 'application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });\n    saveas(blob, `${filename}.xlsx`);\n  };\n\n  return (\n    &lt;button onclick={exportexcel}&gt;download excel&lt;\/button&gt;\n  );\n};\n\nexport default exporttoexcel;\n<\/pre>\n<p><strong>\u7b2c 2 \u6b65\uff1a\u4f7f\u7528 exporttoexcel \u7ec4\u4ef6<\/strong><br \/> \u521b\u5efa exporttoexcel \u7ec4\u4ef6\u540e\uff0c\u5728\u76f8\u5173\u6587\u4ef6\uff08\u4f8b\u5982 app.js\uff09\u4e2d\u4f7f\u7528\u5b83\u3002\u786e\u4fdd\u60a8\u5df2\u51c6\u5907\u597d\u6570\u636e\u4ee5\u5bfc\u51fa\u5230 excel\u3002<\/p>\n<pre>import React from 'react';\nimport ExportToExcel from '.\/ExportToExcel';\n\nconst App = () =&gt; {\n  const data = [\n    { id: 1, name: 'Name 1', email: 'Name1@example.com', joinDate: '2023-01-15' },\n    { id: 2, name: 'Name 2', email: 'Name2@example.com', joinDate: '2023-02-20' },\n    \/\/ Add more data as needed\n  ];\n\n  return (\n    &lt;div&gt;\n      &lt;h1&gt;Export Data to Excel&lt;\/h1&gt;\n      &lt;ExportToExcel data={data} fileName=\"Data_Report\"\/&gt;\n    &lt;\/div&gt;\n  );\n};\n\nexport default App;\n<\/pre>\n<p>\u4f7f\u7528exceljs\uff0c\u5728reactjs\u4e2d\u751f\u6210excel\u62a5\u544a\u53d8\u5f97\u7b80\u5355\u7075\u6d3b\u3002\u672c\u6307\u5357\u6f14\u793a\u5982\u4f55\u521b\u5efa\u5305\u542b\u6807\u9898\u3001\u6570\u636e\u63d2\u5165\u548c\u57fa\u672c\u6807\u9898\u6837\u5f0f\u7684 excel \u62a5\u8868\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aExcel js + React JS\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5982\u4f55\u89e3\u51b3Redis\u7f13\u5b58\u4e0eMySQL\u6570\u636e\u4e0d\u4e00\u81f4\u5bfc\u81f4\u7684\u535a\u5ba2\u70b9\u8d5e\u7cfb\u7edf\u903b\u8f91\u95ee\u9898\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Excel js + React JS \u6536\u85cf \u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aExcel js + React JS\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002 excel\u5e7f\u6cdb\u7528\u4e8e\u5404\u79cd\u6570\u636e\u62a5\u544a\u3002\u5728reactjs\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528exceljs\u5e93\u52a8\u6001\u521b\u5efaexcel\u6587\u4ef6\u3002\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u5728 react \u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0 exceljs \u4ee5\u521b\u5efa\u548c\u4e0b\u8f7d excel \u62a5\u544a\u3002 \u8bbe\u7f6e\u548c\u5b89\u88c5 \u9996\u5148\uff0c\u5b89\u88c5exceljs\u5e93\u3002\u6253\u5f00\u7ec8\u7aef\u5e76\u5728 react \u9879\u76ee\u76ee\u5f55\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a npm install exceljs file-saver exceljs \u5e93\u5c06\u7528\u4e8e\u521b\u5efa\u548c\u64cd\u4f5c\u5de5\u4f5c\u7c3f\uff08excel \u6587\u4ef6\uff09\uff0c\u800c file-saver \u5c06\u5904\u7406\u6d4f\u89c8\u5668\u4e2d\u7684\u6587\u4ef6\u4e0b\u8f7d\u3002 \u7b2c 1 \u6b65\uff1a\u521b\u5efa\u5bfc\u51fa excel \u51fd\u6570 \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ec4\u4ef6\u6587\u4ef6\uff0c\u4f8b\u5982 exporttoexcel.js\u3002\u5728\u6b64\u7ec4\u4ef6\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2aexportexcel\u51fd\u6570\u6765\u5904\u7406\u5de5\u4f5c\u7c3f\u548c\u5de5\u4f5c\u8868\u7684\u521b\u5efa\uff0c\u4ee5\u53ca\u4fdd\u5b58\u751f\u6210\u7684excel\u6587\u4ef6\u3002 import react from &#8216;react&#8217;; import exceljs from &#8216;exceljs&#8217;; import { saveas } from &#8216;file-saver&#8217;; const exporttoexcel = [&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-52876","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52876","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=52876"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52876\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52876"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52876"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52876"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}