{"id":7935,"date":"2024-11-14T13:45:21","date_gmt":"2024-11-14T05:45:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7935\/"},"modified":"2024-11-14T13:45:21","modified_gmt":"2024-11-14T05:45:21","slug":"%e4%bd%bf%e7%94%a8-php-%e8%87%aa%e5%8a%a8%e5%b0%86-csv-%e5%92%8c-excel-%e6%95%b0%e6%8d%ae%e5%af%bc%e5%85%a5-mysql-%e5%92%8c-postgresql-%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7935\/","title":{"rendered":"\u4f7f\u7528 PHP \u81ea\u52a8\u5c06 CSV \u548c Excel \u6570\u636e\u5bfc\u5165 MySQL \u548c PostgreSQL \u6570\u636e\u5e93"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173137340231264.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 PHP \u81ea\u52a8\u5c06 CSV \u548c Excel \u6570\u636e\u5bfc\u5165 MySQL \u548c PostgreSQL \u6570\u636e\u5e93\u63d2\u56fe\" alt=\"\u4f7f\u7528 PHP \u81ea\u52a8\u5c06 CSV \u548c Excel \u6570\u636e\u5bfc\u5165 MySQL \u548c PostgreSQL \u6570\u636e\u5e93\u63d2\u56fe\" \/><\/p>\n<p>\u8981\u4f7f\u7528 php \u81ea\u52a8\u5c06\u6570\u636e\u4ece csv \u6216 excel \u6587\u4ef6\u4f20\u8f93\u5230  \u548c postgresql \u6570\u636e\u5e93\uff0c\u8bf7\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a<\/p>\n<h3> \u5148\u51b3\u6761\u4ef6 <\/h3>\n<ol>\n<li>\n<p>\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\uff1a<\/p>\n<ul>\n<li>php \u9488\u5bf9 mysql \u548c postgresql \u7684 pdo \u6269\u5c55\u3002<\/li>\n<li>phpexcel \u5e93\uff08\u6216 phpspreadsheet\uff0c\u5982\u679c\u53ef\u7528\uff0c\u4f46\u6211\u4eec\u5c06\u4f7f\u7528 phpexcel\uff0c\u56e0\u4e3a\u5b83\u4e0e php 5.6 \u66f4\u517c\u5bb9\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u4e0b\u8f7d phpexcel \u5e93\u5e76\u5c06\u5176\u5305\u542b\u5728\u60a8\u7684\u9879\u76ee\u76ee\u5f55\u4e2d\u3002<\/p>\n<\/li>\n<\/ol>\n<hr>\n<h3> \u7b2c 1 \u6b65\uff1a\u8bbe\u7f6e\u6570\u636e\u5e93\u8fde\u63a5 <\/h3>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528 pdo \u8fde\u63a5\u5230 mysql \u548c postgresql\u3002<\/p>\n<pre>&lt;?php\n\/\/ mysql connection\n$mysqlhost = 'localhost';\n$mysqldb = 'mysql_database';\n$mysqluser = 'mysql_user';\n$mysqlpassword = 'mysql_password';\n\ntry {\n    $mysqlconnection = new pdo(\"mysql:host=$mysqlhost;dbname=$mysqldb\", $mysqluser, $mysqlpassword);\n    $mysqlconnection-&gt;setattribute(pdo::attr_errmode, pdo::errmode_exception);\n    echo \"connected to mysql successfully.&lt;br&gt;\";\n} catch (pdoexception $e) {\n    die(\"mysql connection failed: \" . $e-&gt;getmessage());\n}\n\n\/\/ postgresql connection\n$pghost = 'localhost';\n$pgdb = 'pgsql_database';\n$pguser = 'pgsql_user';\n$pgpassword = 'pgsql_password';\n\ntry {\n    $pgconnection = new pdo(\"pgsql:host=$pghost;dbname=$pgdb\", $pguser, $pgpassword);\n    $pgconnection-&gt;setattribute(pdo::attr_errmode, pdo::errmode_exception);\n    echo \"connected to postgresql successfully.&lt;br&gt;\";\n} catch (pdoexception $e) {\n    die(\"postgresql connection failed: \" . $e-&gt;getmessage());\n}\n?&gt;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u7b2c 2 \u6b65\uff1a\u4ece csv \u6216 excel \u6587\u4ef6\u52a0\u8f7d\u6570\u636e <\/h3>\n<p>\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u6765\u8bfb\u53d6 csv \u6216 excel \u6587\u4ef6\u5e76\u5c06\u6570\u636e\u4f5c\u4e3a\u6570\u7ec4\u8fd4\u56de\u3002<\/p>\n<pre>&lt;?php\nrequire 'path\/to\/phpexcel.php';\n\nfunction readfiledata($filepath) {\n    $filetype = strtolower(pathinfo($filepath, pathinfo_extension));\n\n    if ($filetype === 'csv') {\n        $data = [];\n        if (($handle = fopen($filepath, 'r')) !== false) {\n            while (($row = fgetcsv($handle, 1000, ',')) !== false) {\n                $data[] = $row;\n            }\n            fclose($handle);\n        }\n        return $data;\n    } elseif ($filetype === 'xls' || $filetype === 'xlsx') {\n        $data = [];\n        $excel = phpexcel_iofactory::load($filepath);\n        $sheet = $excel-&gt;getactivesheet();\n        foreach ($sheet-&gt;getrowiterator() as $row) {\n            $rowdata = [];\n            $celliterator = $row-&gt;getcelliterator();\n            $celliterator-&gt;setiterateonlyexistingcells(false);\n            foreach ($celliterator as $cell) {\n                $rowdata[] = $cell-&gt;getvalue();\n            }\n            $data[] = $rowdata;\n        }\n        return $data;\n    } else {\n        throw new exception(\"unsupported file format\");\n    }\n}\n?&gt;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u7b2c3\u6b65\uff1a\u5c06\u6570\u636e\u4f20\u8f93\u5230mysql\u548cpostgresql <\/h3>\n<p>\u5b9a\u4e49\u51fd\u6570\u4ee5\u5c06\u6570\u636e\u63d2\u5165 mysql \u548c postgresql\u3002\u6b64\u793a\u4f8b\u5047\u8bbe\u6570\u636e\u662f\u6570\u7ec4\u7684\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5185\u90e8\u6570\u7ec4\u4ee3\u8868\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u884c\u3002<\/p>\n<pre>&lt;?php\nfunction insertintomysql($mysqlconnection, $data) {\n    $query = \"insert into your_mysql_table (column1, column2, column3) values (?, ?, ?)\";\n    $stmt = $mysqlconnection-&gt;prepare($query);\n    foreach ($data as $row) {\n        $stmt-&gt;execute($row);\n    }\n    echo \"data inserted into mysql successfully.&lt;br&gt;\";\n}\n\nfunction insertintopostgresql($pgconnection, $data) {\n    $query = \"insert into your_pg_table (column1, column2, column3) values (?, ?, ?)\";\n    $stmt = $pgconnection-&gt;prepare($query);\n    foreach ($data as $row) {\n        $stmt-&gt;execute($row);\n    }\n    echo \"data inserted into postgresql successfully.&lt;br&gt;\";\n}\n?&gt;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u7b2c\u56db\u6b65\uff1a\u628a\u5b83\u4eec\u653e\u5728\u4e00\u8d77 <\/h3>\n<p>\u4ece\u6587\u4ef6\u4e2d\u52a0\u8f7d\u6570\u636e\uff0c\u7136\u540e\u5c06\u5176\u4f20\u9012\u7ed9\u6bcf\u4e2a\u51fd\u6570\u4ee5\u63d2\u5165\u5230 mysql \u548c postgresql \u4e2d\u3002<\/p>\n<pre>&lt;?php\n$filePath = 'path\/to\/yourfile.csv'; \/\/ or .xls \/ .xlsx\ntry {\n    $data = readFileData($filePath);\n    insertIntoMySQL($mysqlConnection, $data);\n    insertIntoPostgreSQL($pgConnection, $data);\n} catch (Exception $e) {\n    echo \"Error: \" . $e-&gt;getMessage();\n}\n?&gt;\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<hr>\n<h3> \u6267\u884c\u793a\u4f8b <\/h3>\n<ol>\n<li>\u786e\u4fdd mysql \u548c postgresql \u6570\u636e\u5e93\u548c\u8868\uff08your_mysql_table\u3001your_pg_table\uff09\u5df2\u8bbe\u7f6e\u5e76\u5177\u6709\u6b63\u786e\u7684\u5217\uff08column1\u3001column2\u3001column3\uff09\u3002<\/li>\n<li>\u5c06\u60a8\u7684 csv \u6216 excel \u6587\u4ef6\u653e\u7f6e\u5728\u6307\u5b9a\u8def\u5f84 ($filepath) \u4e2d\u3002<\/li>\n<li>\u4ece\u547d\u4ee4\u884c\u6216\u6d4f\u89c8\u5668\uff08\u5982\u679c\u5728 web \u670d\u52a1\u5668\u4e0a\uff09\u8fd0\u884c\u6b64 php \u811a\u672c\u3002<\/li>\n<\/ol>\n<p>\u6b64\u811a\u672c\u5c06\u4ece\u6307\u5b9a\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u5e76\u5c06\u5176\u63d2\u5165\u5230\u4e24\u4e2a\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u4e0e\u6211\u8054\u7cfb\uff1a@ linkedin \u5e76\u67e5\u770b\u6211\u7684\u4f5c\u54c1\u96c6\u3002<\/p>\n<p>\u8bf7\u7ed9\u6211\u7684 hub \u9879\u76ee\u4e00\u9897\u661f \u2b50\ufe0f<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 PHP \u81ea\u52a8\u5c06 CSV \u548c Excel \u6570\u636e\u5bfc\u5165 MySQL \u548c PostgreSQL \u6570\u636e\u5e93\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u4f7f\u7528 php \u81ea\u52a8\u5c06\u6570\u636e\u4ece csv \u6216 excel \u6587\u4ef6\u4f20\u8f93\u5230 \u548c postgresql \u6570\u636e\u5e93\uff0c\u8bf7\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a \u5148\u51b3\u6761\u4ef6 \u5b89\u88c5\u5fc5\u8981\u7684\u5e93\uff1a php \u9488\u5bf9 mysql \u548c postgresql \u7684 pdo \u6269\u5c55\u3002 phpexcel \u5e93\uff08\u6216 phpspreadsheet\uff0c\u5982\u679c\u53ef\u7528\uff0c\u4f46\u6211\u4eec\u5c06\u4f7f\u7528 phpexcel\uff0c\u56e0\u4e3a\u5b83\u4e0e php 5.6 \u66f4\u517c\u5bb9\uff09\u3002 \u4e0b\u8f7d phpexcel \u5e93\u5e76\u5c06\u5176\u5305\u542b\u5728\u60a8\u7684\u9879\u76ee\u76ee\u5f55\u4e2d\u3002 \u7b2c 1 \u6b65\uff1a\u8bbe\u7f6e\u6570\u636e\u5e93\u8fde\u63a5 \u6211\u4eec\u5c06\u4f7f\u7528 pdo \u8fde\u63a5\u5230 mysql \u548c postgresql\u3002 &lt;?php \/\/ mysql connection $mysqlhost = &#8216;localhost&#8217;; $mysqldb = &#8216;mysql_database&#8217;; $mysqluser = &#8216;mysql_user&#8217;; $mysqlpassword = &#8216;mysql_password&#8217;; [&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-7935","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7935","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=7935"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7935\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}