{"id":96492,"date":"2024-10-20T09:54:18","date_gmt":"2024-10-20T01:54:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96492\/"},"modified":"2024-10-20T09:54:18","modified_gmt":"2024-10-20T01:54:18","slug":"php%e5%b0%8e%e5%85%a5excel%e6%95%b8%e6%93%9a%e5%85%a5%e5%ba%ab-php%e5%be%9eexcel%e5%b0%8e%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96492\/","title":{"rendered":"php\u5c0e\u5165excel\u6578\u64da\u5165\u5eab (php\u5f9eexcel\u5c0e\u5165\u6578\u64da\u5eab\u6578\u64da)"},"content":{"rendered":"<h1 id=\"php%e5%b0%8e%e5%85%a5excel%e6%95%b8%e6%93%9a%e5%85%a5%e5%ba%ab-FlCjxaKzAW\">PHP\u5c0e\u5165Excel\u6578\u64da\u5165\u5eab<\/h1>\n<p>\u5728\u7576\u4eca\u6578\u64da\u9a45\u52d5\u7684\u4e16\u754c\u4e2d\uff0c\u5c07Excel\u6578\u64da\u5c0e\u5165\u6578\u64da\u5eab\u7684\u9700\u6c42\u65e5\u76ca\u589e\u52a0\u3002\u8a31\u591a\u4f01\u696d\u548c\u958b\u767c\u8005\u9700\u8981\u5c07\u5927\u91cf\u7684\u6578\u64da\u5f9eExcel\u8868\u683c\u8f49\u79fb\u5230\u6578\u64da\u5eab\u4e2d\uff0c\u4ee5\u4fbf\u9032\u884c\u66f4\u9ad8\u6548\u7684\u6578\u64da\u7ba1\u7406\u548c\u5206\u6790\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528PHP\u5c07Excel\u6578\u64da\u5c0e\u5165\u6578\u64da\u5eab\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u4ee3\u78bc\u793a\u4f8b\u548c\u6b65\u9a5f\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e9%81%b8%e6%93%87php%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%b0%8e%e5%85%a5-FlCjxaKzAW\">\u70ba\u4ec0\u9ebc\u9078\u64c7PHP\u9032\u884c\u6578\u64da\u5c0e\u5165<\/h2>\n<p>PHP\u662f\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u958b\u6e90\u8173\u672c\u8a9e\u8a00\uff0c\u7279\u5225\u9069\u5408\u65bcWeb\u958b\u767c\u3002\u5176\u9748\u6d3b\u6027\u548c\u5f37\u5927\u7684\u6578\u64da\u8655\u7406\u80fd\u529b\u4f7f\u5176\u6210\u70ba\u5f9eExcel\u5c0e\u5165\u6578\u64da\u7684\u7406\u60f3\u9078\u64c7\u3002\u4f7f\u7528PHP\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u8b80\u53d6Excel\u6587\u4ef6\uff0c\u4e26\u5c07\u6578\u64da\u63d2\u5165\u5230MySQL\u6216\u5176\u4ed6\u985e\u578b\u7684\u6578\u64da\u5eab\u4e2d\u3002<\/p>\n<h2 id=\"%e6%89%80%e9%9c%80%e5%b7%a5%e5%85%b7%e5%92%8c%e5%ba%ab-FlCjxaKzAW\">\u6240\u9700\u5de5\u5177\u548c\u5eab<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u5b89\u88dd\u4e86\u4ee5\u4e0b\u5de5\u5177\u548c\u5eab\uff1a<\/p>\n<ul>\n<li><strong>PHP<\/strong>\uff1a\u78ba\u4fdd\u60a8\u7684\u4f3a\u670d\u5668\u4e0a\u5b89\u88dd\u4e86PHP\u74b0\u5883\u3002<\/li>\n<li><strong>Composer<\/strong>\uff1a\u7528\u65bc\u7ba1\u7406PHP\u4f9d\u8cf4\u9805\u7684\u5de5\u5177\u3002<\/li>\n<li><strong>PhpSpreadsheet<\/strong>\uff1a\u4e00\u500b\u5f37\u5927\u7684PHP\u5eab\uff0c\u7528\u65bc\u8b80\u53d6\u548c\u5beb\u5165Excel\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%ae%89%e8%a3%9dphpspreadsheet-FlCjxaKzAW\">\u5b89\u88ddPhpSpreadsheet<\/h2>\n<p>\u4f7f\u7528Composer\u5b89\u88ddPhpSpreadsheet\u975e\u5e38\u7c21\u55ae\u3002\u60a8\u53ea\u9700\u5728\u547d\u4ee4\u884c\u4e2d\u904b\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><code>composer require phpoffice\/phpspreadsheet<\/code><\/pre>\n<h2 id=\"%e8%ae%80%e5%8f%96excel%e6%96%87%e4%bb%b6-FlCjxaKzAW\">\u8b80\u53d6Excel\u6587\u4ef6<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528PhpSpreadsheet\u8b80\u53d6Excel\u6587\u4ef6\u7684\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\nrequire 'vendor\/autoload.php';\n\nuse PhpOfficePhpSpreadsheetIOFactory;\n\n$inputFileName = 'path\/to\/your\/excel\/file.xlsx';\n$spreadsheet = IOFactory::load($inputFileName);\n$sheetData = $spreadsheet-&gt;getActiveSheet()-&gt;toArray(null, true, true, true);\n?&gt;<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5f15\u5165\u4e86PhpSpreadsheet\u5eab\uff0c\u7136\u5f8c\u52a0\u8f09Excel\u6587\u4ef6\u4e26\u5c07\u5176\u8f49\u63db\u70ba\u6578\u7d44\u683c\u5f0f\uff0c\u65b9\u4fbf\u5f8c\u7e8c\u8655\u7406\u3002<\/p>\n<h2 id=\"%e5%b0%87%e6%95%b8%e6%93%9a%e6%8f%92%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab-FlCjxaKzAW\">\u5c07\u6578\u64da\u63d2\u5165\u6578\u64da\u5eab<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u5c07\u8b80\u53d6\u5230\u7684\u6578\u64da\u63d2\u5165\u5230\u6578\u64da\u5eab\u4e2d\u3002\u4ee5\u4e0b\u662f\u5c07\u6578\u64da\u63d2\u5165MySQL\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n$servername = \"localhost\";\n$username = \"username\";\n$password = \"password\";\n$dbname = \"database_name\";\n\n\/\/ \u5275\u5efa\u9023\u63a5\n$conn = new mysqli($servername, $username, $password, $dbname);\n\n\/\/ \u6aa2\u67e5\u9023\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"Connection failed: \" . $conn-&gt;connect_error);\n}\n\n\/\/ \u904d\u6b77Excel\u6578\u64da\u4e26\u63d2\u5165\u6578\u64da\u5eab\nforeach ($sheetData as $row) {\n    $sql = \"INSERT INTO your_table (column1, column2) VALUES ('\".$row['A'].\"', '\".$row['B'].\"')\";\n    if ($conn-&gt;query($sql) !== TRUE) {\n        echo \"Error: \" . $sql . \"<br>\" . $conn-&gt;error;\n    }\n}\n\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u9996\u5148\u5275\u5efa\u4e86\u4e00\u500bMySQL\u9023\u63a5\uff0c\u7136\u5f8c\u904d\u6b77Excel\u6578\u64da\uff0c\u5c07\u6bcf\u4e00\u884c\u63d2\u5165\u5230\u6307\u5b9a\u7684\u6578\u64da\u8868\u4e2d\u3002\u8acb\u6839\u64da\u60a8\u7684\u6578\u64da\u5eab\u7d50\u69cb\u8abf\u6574SQL\u8a9e\u53e5\u3002<\/p>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-FlCjxaKzAW\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<ul>\n<li>\u78ba\u4fddExcel\u6587\u4ef6\u7684\u683c\u5f0f\u6b63\u78ba\uff0c\u4e26\u4e14\u6578\u64da\u985e\u578b\u8207\u6578\u64da\u5eab\u4e2d\u7684\u5b57\u6bb5\u5339\u914d\u3002<\/li>\n<li>\u8003\u616e\u4f7f\u7528\u9810\u8655\u7406\u8a9e\u53e5\u4f86\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002<\/li>\n<li>\u5728\u8655\u7406\u5927\u91cf\u6578\u64da\u6642\uff0c\u5efa\u8b70\u4f7f\u7528\u4e8b\u52d9\u4f86\u63d0\u9ad8\u6027\u80fd\u548c\u6578\u64da\u5b8c\u6574\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-FlCjxaKzAW\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4f7f\u7528PHP\u548cPhpSpreadsheet\uff0c\u5c07Excel\u6578\u64da\u5c0e\u5165\u6578\u64da\u5eab\u8b8a\u5f97\u76f8\u5c0d\u7c21\u55ae\u3002\u9019\u4e0d\u50c5\u63d0\u9ad8\u4e86\u6578\u64da\u8655\u7406\u7684\u6548\u7387\uff0c\u9084\u80fd\u5920\u5e6b\u52a9\u4f01\u696d\u66f4\u597d\u5730\u7ba1\u7406\u548c\u5206\u6790\u6578\u64da\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684\u4f3a\u670d\u5668\u4f86\u904b\u884c\u60a8\u7684PHP\u61c9\u7528\u7a0b\u5e8f\uff0c\u8003\u616e\u4f7f\u7528\u6211\u5011\u7684<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u89e3\u6c7a\u65b9\u6848\uff0c\u70ba\u60a8\u7684\u9805\u76ee\u63d0\u4f9b\u5f37\u5927\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528PHP\u5c07Excel\u6578\u64da\u5c0e\u5165\u6578\u64da\u5eab\uff0c\u7c21\u5316\u6578\u64da\u7ba1\u7406\u6d41\u7a0b\uff0c\u63d0\u5347\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-96492","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=96492"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96492\/revisions"}],"predecessor-version":[{"id":96493,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96492\/revisions\/96493"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}