{"id":941,"date":"2022-07-14T08:47:15","date_gmt":"2022-07-14T00:47:15","guid":{"rendered":"https:\/\/usei.cn\/?p=941"},"modified":"2022-07-14T08:47:16","modified_gmt":"2022-07-14T00:47:16","slug":"php-%e9%80%9a%e7%94%a8%e5%8c%96excel-%e5%af%bc%e5%87%ba","status":"publish","type":"post","link":"https:\/\/usei.cn\/index.php\/2022\/07\/14\/php-%e9%80%9a%e7%94%a8%e5%8c%96excel-%e5%af%bc%e5%87%ba\/","title":{"rendered":"php \u901a\u7528\u5316excel \u5bfc\u51fa"},"content":{"rendered":"\n<p>\u5982\u679c\u4f60\u7684\u6570\u636e\u4e00\u4e0b\u5f62\u5f0f\u7684\uff0c\u53ef\u4f7f\u7528 ExcelHelper \u5bfc\u51fa\u6807\u51c6\u6587\u4ef6<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u67e5\u8be2\u6570\u636e\u7ed3\u679c<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$list =&#91;\n  &#91;'id'=>1,'name'=>'\u5f20\u4e09','idcard'=>'152622198901010000'],\n  &#91;'id'=>2,'name'=>'\u674e\u56db','idcard'=>'152622198901010001'],\n];<\/code><\/pre>\n\n\n\n<p>\u5bfc\u51fa\u7ed3\u679c<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\u7f16\u53f7<\/td><td>\u59d3\u540d<\/td><td>\u8eab\u4efd\u8bc1<\/td><\/tr><tr><td>1<\/td><td>\u5f20\u4e09<\/td><td>152622198901010000<\/td><\/tr><tr><td>2<\/td><td>\u674e\u56db<\/td><td>152622198901010001<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">\u51c6\u5907\u5b57\u6bb5\u6620\u5c04<\/h3>\n\n\n\n<p>\u4e0d\u8bbe\u7f6e\u4e5f\u53ef\u4ee5\uff0c\u5bfc\u51fa\u7684\u8868\u683c\u6807\u9898\u4e3a\u952e\u540d\uff0c\u4e0d\u7ebf\u4e0a\u4e2d\u6587\u540d\u4e86<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> $field_map =&#91;\n            &#91;'key'=>'id','title'=>'\u7f16\u53f7'],\n            &#91;'key'=>'name','title'=>\"\u59d3\u540d\"],\n            &#91;'key'=>'idacard','title'=> '\u8eab\u4efd\u8bc1','type'=>\"string\"]\n        ];<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$excel = new ExcelHelper();\n$excel->DataToExcel($result&#91;'items'],$field_map);\n$excel->export('\u65e5\u62a5\u8868.xlsx');\n\n\/\/ $excel->exportFile('download\/report','\u62a5\u8868.xlsx'); \/\/\u5bfc\u51fa\u5230\u6587\u4ef6<\/code><\/pre>\n\n\n\n<p>helper \u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/**\n * Created by PhpStorm.\n * User: Santiago\n *\/\n\nnamespace app\\common\\utils;\n\nuse PHPExcel_Style_Alignment;\n\n\/**\n * \u901a\u7528 \u6570\u636e\u5bfc\u51fa\n * Class ExcelHelper\n * @package app\\common\\utils\n *\/\nclass ExcelHelper\n{\n    const COLUMN_NAME =&#91;'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ','BA','BB','BC','BD','BE','BF','BG','BH','BI','BJ','BK','BL','BM','BN','BO','BP','BQ','BR','BS','BT','BU','BV','BW','BX','BY','BZ'];\n    public $objPHPExcel;\n    public $sheet; \/\/ \u5de5\u4f5c\u8868\n    private $data_row = 0; \/\/ \u6570\u636e\u603b\u884c\u6570\uff08\u4e0d\u542b\u6807\u9898\u548c\u6458\u8981\uff09\n\n    function __construct(){\n        $this->objPHPExcel = new \\PHPExcel();\n        \/\/-----\u5168\u5c40\u6837\u5f0f------\n        \/\/\u9ed8\u8ba4\u5b57\u4f53\u3001\u5927\u5c0f\n        $this->objPHPExcel->getDefaultStyle()->getFont()->setSize(12);\n        \/\/ \u5bf9\u9f50\u65b9\u5f0f\n        $this->objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);\n        $this->objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);\n\n        $this->sheet = $this->objPHPExcel->getActiveSheet();\n        \/\/\u8bbe\u7f6e\u9ed8\u8ba4\u884c\u9ad8\n        $this->sheet->getDefaultRowDimension()->setRowHeight(25);\n        \/\/\u8bbe\u7f6e\u9ed8\u8ba4\u5217\u5bbd\n        $this->sheet->getDefaultColumnDimension()->setWidth(15);\n    }\n\n    \/**\n     * \u901a\u7528\u5bfc\u51fa\u6570\u636e\n     * @param $list array \u8be6\u7ec6\u6570\u636e\n     * @param $field_map array \u8be6\u7ec6\u6570\u636e\u5bf9\u5e94\u5b57\u6bb5\u540d\u79f0\n     * @param $summary array \u7edf\u8ba1\u4fe1\u606f\n     *\/\n    public function DataToExcel($list,$field_map){\n        \/\/ \u8bbe\u7f6e\u6807\u9898\u884c\n        $this->setTitleRow($field_map);\n        \/\/ \u8bbe\u7f6e\u6570\u636e\u884c\n        $this->setDataRow($list,$field_map);\n    }\n\n    \/**\n     * \u5bfc\u51fa\u5230\u6d4f\u89c8\u5668\n     *\/\n    public function export($fileName){\n        ob_end_clean();\/\/\u6e05\u9664\u7f13\u51b2\u533a,\u907f\u514d\u4e71\u7801\n        \/\/ \u6fc0\u6d3b\u6d4f\u89c8\u5668\u7a97\u53e3\n        header(\"Content-Disposition:attachment;filename=$fileName\");\n        \/\/\u7f13\u5b58\u63a7\u5236\n        header(\"Cache-Control:max-age=0\");\n        \/\/ \u8c03\u7528\u65b9\u6cd5\u6267\u884c\u4e0b\u8f7d\n        $objWriter = \\PHPExcel_IOFactory::createWriter($this->objPHPExcel,'Excel2007');\n        \/\/ \u6570\u636e\u6d41\n        $objWriter->save(\"php:\/\/output\");\n    }\n\n    \/**\n     * \u5bfc\u51fa\u5230\u6587\u4ef6\n     * @param $dir_path\n     * @param $fileName\n     * @throws \\PHPExcel_Reader_Exception\n     * @throws \\PHPExcel_Writer_Exception\n     *\/\n    public function exportFile($dir_path,$fileName){\n        if(!is_dir($dir_path)){mkdir($dir_path,0777,true);}\n\n        ob_end_clean();\/\/\u6e05\u9664\u7f13\u51b2\u533a,\u907f\u514d\u4e71\u7801\n        $path=$dir_path.\"\/\".$fileName;\n        $objWriter = \\PHPExcel_IOFactory::createWriter($this->objPHPExcel,'Excel2007');\n        $objWriter->save($path);\n    }\n\n    \/**\n     * \u8bbe\u7f6e\u6807\u9898\u884c\u6837\u5f0f\n     *\/\n    public function setTitleRow($field_map){\n        \/\/ \u8bbe\u7f6e\u6807\u9898\u884c\n        $colName='';\n        foreach($field_map as $k=>$v){\n            $colName = self::COLUMN_NAME&#91;$k].'1';\n            $this->sheet->setCellValue( $colName, $v&#91;'title']);\n        }\n        \/\/ \u9996\u884c\u5c45\u4e2d\n        $this->sheet->getStyle('A1:'.$colName)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);\n    }\n\n    \/**\n     * \u8bbe\u7f6e\u6570\u636e\n     * @param $list\n     * @param $field_map\n     *\/\n    public function setDataRow($list,$field_map){\n        \/\/ \u7ed9\u8868\u683c\u6dfb\u52a0\u6570\u636e\n        $this->data_row = 0;\n        foreach ($list as $k=>$good){\n            $row = $k+2;\n            $this->data_row ++;\n            foreach ($field_map as $col_index=>$col_info){\n                $col_name = self::COLUMN_NAME&#91;$col_index].$row;\n                $value = $good&#91;$col_info&#91;'key']];\n                \/\/ \u7c7b\u578b\u5904\u7406\n                if(isset($col_info&#91;'type']) &amp;&amp; $col_info&#91;'type']=='string'){\n                    $value = \"'\".$value;\n                }\n                $this->sheet->setCellValue( $col_name,$value);\n            }\n        }\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u4f60\u7684\u6570\u636e\u4e00\u4e0b\u5f62\u5f0f\u7684\uff0c\u53ef\u4f7f\u7528 ExcelHelper \u5bfc\u51fa\u6807\u51c6\u6587\u4ef6 \u67e5\u8be2\u6570\u636e\u7ed3\u679c \u5bfc\u51fa\u7ed3\u679c \u7f16\u53f7 \u59d3\u540d \u8eab\u4efd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-941","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts\/941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/comments?post=941"}],"version-history":[{"count":9,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts\/941\/revisions"}],"predecessor-version":[{"id":950,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/posts\/941\/revisions\/950"}],"wp:attachment":[{"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/media?parent=941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/categories?post=941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/usei.cn\/index.php\/wp-json\/wp\/v2\/tags?post=941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}