{"id":199348,"date":"2025-05-03T08:28:09","date_gmt":"2025-05-03T00:28:09","guid":{"rendered":"https:\/\/server.hk\/cnblog\/199348\/"},"modified":"2025-05-03T08:28:09","modified_gmt":"2025-05-03T00:28:09","slug":"%e8%a7%a3%e5%86%b3ecshop%e5%8a%a0%e5%85%a5phpexcel%e5%af%bc%e5%85%a5%e5%8f%98%e6%88%90%e4%b9%b1%e7%a0%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/199348\/","title":{"rendered":"\u89e3\u51b3ecshop\u52a0\u5165PHPExcel\u5bfc\u5165\u53d8\u6210\u4e71\u7801"},"content":{"rendered":"<p>\u7ecf\u8fc783\u6b21\u5b9e\u8df5\uff0c\u53d1\u73b0\u4ee3\u7801\u7684\u4e2d\u95f4\u7a7a\u4e86\u51e0\u884c\uff1a\u5c31\u662fecshop\u751f\u6210\u4ee3\u7801\u7ed3\u675f ?&gt; &nbsp;\u4e0e\u65b0\u4ee3\u7801&lt;?php \u5f00\u59cb\u4e4b\u95f4\u6709\u7a7a\u9699\u3002\u6c57\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u5c0f\u95ee\u9898\u6298\u817e\u4e86\u597d\u6a21\u677f\u8fd1\u4e00\u4e2a\u5c0f\u65f6\u3002\u540e\u6765\u5728\u7f51\u4e0a\u627e\u5230\u76f8\u4f3c\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u73b0\u4e00\u5e76\u8d34\u51fa\u6765\u3002<\/p>\n<p> \u5728PHP\u4ee3\u7801\u7684\u6700\u524d\u9762\u52a0\u5165\uff1aob_start();\u5728excel\u8981\u751f\u6210\u7684\u524d\u9762\u52a0\u4e0a\uff1aob_end_clean();\u8fd9\u6837\u5c31\u53ef\u4ee5\u89e3\u51b3\u4e71\u7801\u7684\u95ee\u9898\u4e86\u3002&nbsp;&lt;?php  define(&#8216;IN_ECS&#8217;, true);   &nbsp;   require(dirname(__FILE__) . &#8216;\/includes\/init.php&#8217;);   require_once(ROOT_PATH . &#8216;includes\/lib_order.php&#8217;);   &nbsp;   &nbsp;   \/* \u6743\u9650\u5224\u65ad *\/   admin_priv(&#8216;xxx_order_stats&#8217;);   &nbsp;   &nbsp;   $sql = &#8216;select goods_id, goods_sn, goods_name, market_price, shop_price, promote_price, &nbsp;is_on_sale from goods where is_delete=0 order by goods_id desc&#8217;;   $data_list = $db-&gt;getAll($sql);   &nbsp;   &nbsp;   \/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; *\/   \/\/&#8211; \u4e0b\u8f7dEXCEL\u62a5\u8868   \/*&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; *\/   &nbsp;\/* \u6587\u4ef6\u540d *\/   $filename = &#8216;\u5546\u54c1\u8868&#8217;.date(&#8220;Y-m-d&#8221;,time());   &nbsp;   header(&#8220;Content-type: application\/vnd.ms-excel; charset=utf-8&#8221;);   header(&#8220;Content-Disposition: attachment; filename=$filename.xls&#8221;);   &nbsp;   \/* \u6587\u4ef6\u6807\u9898 *\/   \/\/echo ecs_iconv(EC_CHARSET, &#8216;GB2312&#8217;, $filename ) . &#8220;\\t\\n&#8221;;   &nbsp;   \/* \u5934\u90e8\u4fe1\u606f *\/   echo ecs_iconv(EC_CHARSET, &#8216;GB2312&#8217;, &#8216;\u5546\u54c1ID&#8217;) .&#8221;\\t&#8221;;   echo ecs_iconv(EC_CHARSET, &#8216;GB2312&#8217;, &#8216;\u5546\u54c1\u540d\u79f0&#8217;) .&#8221;\\t&#8221;;   echo ecs_iconv(EC_CHARSET, &#8216;GB2312&#8217;, &#8216;\u8d27\u53f7&#8217;) .&#8221;\\t&#8221;;   echo ecs_iconv(EC_CHARSET, &#8216;GB2312&#8217;, &#8216;\u5e02\u573a\u4ef7&#8217;) .&#8221;\\t&#8221;;   echo ecs_iconv(EC_CHARSET, &#8216;GB2312&#8217;, &#8216;\u672c\u5e97\u4ef7&#8217;) .&#8221;\\t&#8221;;   echo ecs_iconv(EC_CHARSET, &#8216;GB2312&#8217;, &#8216;\u4fc3\u9500\u4ef7&#8217;) .&#8221;\\t&#8221;;   echo ecs_iconv(EC_CHARSET, &#8216;GB2312&#8217;, &#8216;\u4e0a\/\u4e0b\u67b6&#8217;) .&#8221;\\t&#8221;;   &nbsp;   echo &#8220;\\n&#8221;;   &nbsp;   foreach ($data_list AS $data)   {   &nbsp; &nbsp; echo ecs_iconv(EC_CHARSET, &#8216;GB2312&#8217;, $data[&#8216;goods_id&#8217;]) . &#8220;\\t&#8221;;   &nbsp; &nbsp; echo ecs_iconv(EC_CHARSET, &#8216;GB2312&#8217;, $data[&#8216;goods_name&#8217;]) . &#8220;\\t&#8221;;   &nbsp; &nbsp; echo ecs_iconv(EC_CHARSET, &#8216;GB2312&#8217;, strval($data[&#8216;goods_sn&#8217;])) . &#8220;\\t&#8221;;   &nbsp; &nbsp; echo ecs_iconv(EC_CHARSET, &#8216;GB2312&#8217;, $data[&#8216;market_price&#8217;]) . &#8220;\\t&#8221;;   &nbsp; &nbsp; echo ecs_iconv(EC_CHARSET, &#8216;GB2312&#8217;, $data[&#8216;shop_price&#8217;]) . &#8220;\\t&#8221;;   &nbsp; &nbsp; echo ecs_iconv(EC_CHARSET, &#8216;GB2312&#8217;, (intval($data[&#8216;promote_price&#8217;]) == 0 ? &nbsp;&#8221; : $data[&#8216;promote_price&#8217;])) . &#8220;\\t&#8221;;   &nbsp;   &nbsp; &nbsp; echo ecs_iconv(EC_CHARSET, &#8216;GB2312&#8217;, ($data[&#8216;is_on_sale&#8217;] == 1 ? &#8216;\u4e0a\u67b6&#8217; : &#8221;)) . &#8220;\\t&#8221;;   &nbsp; &nbsp; echo &#8220;\\n&#8221;;   }<br \/>\n<br \/> \/\/\u4ee5\u4e0a\u8fd9\u6bb5\u4ee3\u7801\u751f\u6210Excel\u6b63\u5e38\uff0c\u4e0d\u4f1a\u51fa\u73b0\u95ee\u9898\uff0c\u4f46\u5728\u6253\u5f00\u65f6\u4f1a\u63d0\u793a\u4e0d\u517c\u5bb9\u7684\u95ee\u9898\u3002\u5bf9\u4e8e\u90a3\u4e9b\u9886\u5bfc\u6216\u8005\u5916\u884c\u4eba\u58eb\u7528\uff0c\u4f1a\u611f\u89c9\u8fd9\u4e2a\u6587\u6863\u6709\u95ee\u9898\uff0c\u4e8e\u662f\u597d\u6a21\u677f\u627e\u5230PHPExcel\u6765\u6b63\u5e38\u751f\u6210excel\u3002   ?&gt;<br \/>\n<br \/> &nbsp;   &lt;?php   header(&#8216;Content-type:text\/html;charset=utf-8&#8217;);   require_once ROOT_PATH . &#8216;\/Classes\/PHPExcel.php&#8217;;   require_once ROOT_PATH . &#8216;\/Classes\/PHPExcel\/Writer\/Excel2007.php&#8217;;   require_once ROOT_PATH . &#8216;\/Classes\/PHPExcel\/Writer\/Excel5.php&#8217;;   include_once ROOT_PATH . &#8216;\/Classes\/PHPExcel\/IOFactory.php&#8217;;   &nbsp;   $objExcel = new PHPExcel();   \/\/\u8bbe\u7f6e\u5c5e\u6027 (\u8fd9\u6bb5\u4ee3\u7801\u65e0\u5173\u7d27\u8981\uff0c\u5176\u4e2d\u7684\u5185\u5bb9\u53ef\u4ee5\u66ff\u6362\u4e3a\u4f60\u9700\u8981\u7684)   \/\/$objExcel-&gt;getProperties()-&gt;setCreator(&#8220;andy&#8221;);   \/\/$objExcel-&gt;getProperties()-&gt;setLastModifiedBy(&#8220;andy&#8221;);   \/\/$objExcel-&gt;getProperties()-&gt;setTitle(&#8220;Office 2003 XLS Test Document&#8221;);   \/\/$objExcel-&gt;getProperties()-&gt;setSubject(&#8220;Office 2003 XLS Test Document&#8221;);   \/\/$objExcel-&gt;getProperties()-&gt;setDescription(&#8220;Test document for Office 2003 XLS, generated using PHP classes.&#8221;);   \/\/$objExcel-&gt;getProperties()-&gt;setKeywords(&#8220;office 2003 openxml php&#8221;);   \/\/$objExcel-&gt;getProperties()-&gt;setCategory(&#8220;Test result file&#8221;);   \/\/$objExcel-&gt;setActiveSheetIndex(0);   &nbsp;   $i=0;   \/\/\u8868\u5934   $k1=&#8221;\u5546\u54c1ID&#8221;;   $k2=&#8221;\u5546\u54c1\u540d\u79f0&#8221;;   $k3=&#8221;\u8d27\u53f7&#8221;;   $k4=&#8221;\u5e02\u573a\u4ef7&#8221;;   $k5=&#8221;\u672c\u5e97\u4ef7&#8221;;   $k6=&#8221;\u4fc3\u9500\u4ef7&#8221;;   $k7=&#8221;\u4e0a\/\u4e0b\u67b6&#8221;;   &nbsp;   $objExcel-&gt;getActiveSheet()-&gt;setCellValue(&#8216;a1&#8217;, &#8220;$k1&#8221;)-&gt;getStyle(&#8216;a1&#8217;)-&gt;getFont()-&gt;getColor()-&gt;setARGB(PHPExcel_Style_Color::COLOR_RED);   $objExcel-&gt;getActiveSheet()-&gt;setCellValue(&#8216;b1&#8217;, &#8220;$k2&#8221;)-&gt;getStyle(&#8216;b1&#8217;)-&gt;getFont()-&gt;getColor()-&gt;setARGB(PHPExcel_Style_Color::COLOR_RED);   $objExcel-&gt;getActiveSheet()-&gt;setCellValue(&#8216;c1&#8217;, &#8220;$k3&#8221;)-&gt;getStyle(&#8216;c1&#8217;)-&gt;getFont()-&gt;getColor()-&gt;setARGB(PHPExcel_Style_Color::COLOR_RED);   $objExcel-&gt;getActiveSheet()-&gt;setCellValue(&#8216;d1&#8217;, &#8220;$k4&#8221;)-&gt;getStyle(&#8216;d1&#8217;)-&gt;getFont()-&gt;getColor()-&gt;setARGB(PHPExcel_Style_Color::COLOR_RED);   $objExcel-&gt;getActiveSheet()-&gt;setCellValue(&#8216;e1&#8217;, &#8220;$k5&#8221;)-&gt;getStyle(&#8216;e1&#8217;)-&gt;getFont()-&gt;getColor()-&gt;setARGB(PHPExcel_Style_Color::COLOR_RED);   $objExcel-&gt;getActiveSheet()-&gt;setCellValue(&#8216;f1&#8217;, &#8220;$k6&#8221;)-&gt;getStyle(&#8216;f1&#8217;)-&gt;getFont()-&gt;getColor()-&gt;setARGB(PHPExcel_Style_Color::COLOR_RED);   $objExcel-&gt;getActiveSheet()-&gt;setCellValue(&#8216;g1&#8217;, &#8220;$k7&#8221;)-&gt;getStyle(&#8216;g1&#8217;)-&gt;getFont()-&gt;getColor()-&gt;setARGB(PHPExcel_Style_Color::COLOR_RED);   &nbsp;   foreach($data_list as $k=&gt;$v) {   &nbsp; &nbsp; $u1=$i+2;   &nbsp; &nbsp; \/*&#8212;&#8212;&#8212;-\u5199\u5165\u5185\u5bb9&#8212;&#8212;&#8212;&#8212;-*\/   &nbsp; &nbsp; $objExcel-&gt;getActiveSheet()-&gt;setCellValue(&#8216;a&#8217;.$u1, $v[&#8220;goods_id&#8221;]);   &nbsp; &nbsp; $objExcel-&gt;getActiveSheet()-&gt;setCellValue(&#8216;b&#8217;.$u1, $v[&#8220;goods_name&#8221;]);   &nbsp; &nbsp; $objExcel-&gt;getActiveSheet()-&gt;setCellValue(&#8216;c&#8217;.$u1, &#8220;&#8216;&#8221;.strval($v[&#8216;goods_sn&#8217;]));   &nbsp; &nbsp; $objExcel-&gt;getActiveSheet()-&gt;setCellValue(&#8216;d&#8217;.$u1, $v[&#8220;market_price&#8221;]);   &nbsp; &nbsp; $objExcel-&gt;getActiveSheet()-&gt;setCellValue(&#8216;e&#8217;.$u1, $v[&#8220;shop_price&#8221;]);   &nbsp; &nbsp; $objExcel-&gt;getActiveSheet()-&gt;setCellValue(&#8216;f&#8217;.$u1, (intval($v[&#8216;promote_price&#8217;]) == 0 ? &nbsp;&#8221; : $v[&#8216;promote_price&#8217;]));   &nbsp; &nbsp; $objExcel-&gt;getActiveSheet()-&gt;setCellValue(&#8216;g&#8217;.$u1, ($v[&#8216;is_on_sale&#8217;] == 1 ? &#8216;\u4e0a\u67b6&#8217; : &#8221;));   &nbsp; &nbsp; $i++;   }   &nbsp;   \/\/ \u9ad8\u7f6e\u5217\u7684\u5bbd\u5ea6   $objExcel-&gt;getActiveSheet()-&gt;getColumnDimension(&#8216;A&#8217;)-&gt;setWidth(10);   $objExcel-&gt;getActiveSheet()-&gt;getColumnDimension(&#8216;B&#8217;)-&gt;setWidth(80);   $objExcel-&gt;getActiveSheet()-&gt;getColumnDimension(&#8216;C&#8217;)-&gt;setWidth(20);   $objExcel-&gt;getActiveSheet()-&gt;getColumnDimension(&#8216;D&#8217;)-&gt;setWidth(10);   $objExcel-&gt;getActiveSheet()-&gt;getColumnDimension(&#8216;E&#8217;)-&gt;setWidth(10);   $objExcel-&gt;getActiveSheet()-&gt;getColumnDimension(&#8216;F&#8217;)-&gt;setWidth(10);   $objExcel-&gt;getActiveSheet()-&gt;getColumnDimension(&#8216;G&#8217;)-&gt;setWidth(10);   &nbsp;   $objExcel-&gt;getActiveSheet()-&gt;getHeaderFooter()-&gt;setOddHeader(&#8216;&amp;L&amp;BPersonal cash register&amp;RPrinted on &amp;D&#8217;);   $objExcel-&gt;getActiveSheet()-&gt;getHeaderFooter()-&gt;setOddFooter(&#8216;&amp;L&amp;B&#8217; . $objExcel-&gt;getProperties()-&gt;getTitle() . &#8216;&amp;RPage &amp;P of &amp;N&#8217;);   &nbsp;   \/\/ \u8bbe\u7f6e\u9875\u65b9\u5411\u548c\u89c4\u6a21   $objExcel-&gt;getActiveSheet()-&gt;getPageSetup()-&gt;setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);   $objExcel-&gt;getActiveSheet()-&gt;getPageSetup()-&gt;setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);   $objExcel-&gt;setActiveSheetIndex(0);   ob_end_clean();   $ex = &#8216;xxx&#8217;;   if($ex == &#8216;2007&#8217;) { \/\/\u5bfc\u51faexcel2007\u6587\u6863   &nbsp; &nbsp; header(&#8216;Content-Type: application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet&#8217;);   &nbsp; &nbsp; header(&#8216;Content-Disposition: attachment;filename=&#8221;&#8216;.$filename.&#8217;.xlsx&#8221;&#8216;);   &nbsp; &nbsp; header(&#8216;Cache-Control: max-age=0&#8217;);   &nbsp; &nbsp; $objWriter = PHPExcel_IOFactory::createWriter($objExcel, &#8216;Excel2007&#8217;);   &nbsp; &nbsp; $objWriter-&gt;save(&#8216;php:\/\/output&#8217;);   &nbsp; &nbsp; exit;   } else { &nbsp;\/\/\u5bfc\u51faexcel2003\u6587\u6863   &nbsp; &nbsp; header(&#8216;Content-Type: application\/vnd.ms-excel&#8217;);   &nbsp; &nbsp; header(&#8216;Content-Disposition: attachment;filename=&#8221;&#8216;.$filename.&#8217;.xls&#8221;&#8216;);   &nbsp; &nbsp; header(&#8216;Cache-Control: max-age=0&#8217;);   &nbsp; &nbsp; $objWriter = PHPExcel_IOFactory::createWriter($objExcel, &#8216;Excel5&#8217;);   &nbsp; &nbsp; $objWriter-&gt;save(&#8216;php:\/\/output&#8217;);   &nbsp; &nbsp; exit;   }   ?&gt;<br \/>\n<br \/> \u6ce8\u91ca\u6389ecshop\u7684\u751f\u6210\u4ee3\u7801\u540e\uff0cPHPExcel\u751f\u6210\u7684excel\u5374\u53d8\u6210\u4e86\u4e71\u7801\uff0c\u91cd\u65b0\u628aPHPExcel\u5355\u72ec\u62ff\u51fa\u6765\u53c8\u6b63\u5e38\u3002  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7ecf\u8fc783\u6b21\u5b9e\u8df5\uff0c\u53d1\u73b0\u4ee3\u7801\u7684\u4e2d\u95f4\u7a7a&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-199348","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199348","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=199348"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199348\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=199348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=199348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=199348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}