{"id":2056,"date":"2023-01-01T22:19:59","date_gmt":"2023-01-01T14:19:59","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2056"},"modified":"2023-01-01T22:19:59","modified_gmt":"2023-01-01T14:19:59","slug":"2056","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2056.html","title":{"rendered":"PHP \u5c06\u6570\u636e\u8ffd\u52a0\u5230Excel\u8868\u683c\u5185\u5e76\u4fdd\u5b58\uff0c\u53ef\u4e0b\u8f7d\uff0c\u4e5f\u53ef\u4ee5\u4e0d\u4e0b\u8f7d"},"content":{"rendered":"

\u8ffd\u52a0\u65b9\u6cd5\uff1a<\/span><\/p>\n

    \/**\r\n     * [readExcel \u5f80Excel\u8868\u63d2\u5165\u6570\u636e]\r\n     * @param  [array] $data      [\u6570\u636e]\r\n     * @param  [string] $filePath [\u6587\u4ef6\u8def\u5f84]\r\n     * @param  [array] $column    [\u8868\u683c\u5217]\r\n     * @return [type]             [description]\r\n     *\/\r\n    public function readExcel($data=array(),$filePath,$column){\r\n \r\n        \/\/\u83b7\u53d6\u540e\u7f00\u540d\r\n        $extension = strtolower( pathinfo($filePath, PATHINFO_EXTENSION) );\r\n        \/\/\u5224\u65ad\r\n        if ($extension =='xlsx') {\r\n            $objReader = PHPExcel_IOFactory::createReader('Excel2007');\/\/use excel2007 for 2007 format\r\n        } else if ($extension =='xls') {\r\n            \/\/$objReader = new PHPExcel_Reader_Excel5();\/\/use excel2007 for 2007 format\r\n            $objReader = PHPExcel_IOFactory::createReader('Excel5');\/\/use excel2007 for 2007 format\r\n        } else if ($extension=='csv') {\r\n            \/\/\u8fd8\u6ca1\u6709\u6d4b\u8bd5\u8fc7\r\n            $objReader = new PHPExcel_Reader_CSV();\r\n            \/\/\u9ed8\u8ba4\u8f93\u5165\u5b57\u7b26\u96c6\r\n            $objReader->setInputEncoding('utf-8');\r\n            \/\/\u9ed8\u8ba4\u7684\u5206\u9694\u7b26\r\n            $objReader->setDelimiter(',');\r\n        }\r\n        \/\/\u52a0\u8f7d\u65b0\u6587\u4ef6\r\n        $objPHPExcel = $objReader->load($filePath);\r\n        $sheet = $objPHPExcel->getSheet(0);\r\n        $highestRow = $sheet->getHighestRow();\r\n        $highestColumn = $sheet->getHighestColumn();\r\n        \/\/\u4ece\u7b2c\u51e0\u884c\u8ffd\u52a0\r\n        $h = 2;\r\n        \/\/\u5faa\u73af\u6570\u636e\r\n        for($j = 0; $j <= count($data); $j++){\r\n            \/\/\u5faa\u73af\u6570\u636e\u5185\u6709\u591a\u5c11\u4e2a\r\n            for ($i=0; $i < count($data[$j]); $i++) { \r\n                \/\/\u6309\u7167\u8868\u683c\u7b2c\u51e0\u5217\u8ffd\u52a0\u5185\u5bb9\r\n                $objPHPExcel->getActiveSheet()->setCellValue("$column[$i]"."$h",$data[$j][$i]); \/\/\u586b\u5145\u6587\u5b57\r\n            }\r\n            \/\/\u8868\u683c\u884c+1\r\n            $h++;\r\n \r\n        }\r\n \r\n        $obj_writer = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007'); \/\/\u751f\u6210\u6587\u4ef6\r\n        $obj_writer->save($filePath); \/\/\u4fdd\u5b58\u6587\u4ef6\r\n        \/\/\u8fd4\u56de\u6587\u4ef6\u8def\u5f84\r\n        return $filePath;\r\n \r\n    }<\/pre>\n

\u4f7f\u7528\u65b9\u6cd5\uff1a<\/span><\/p>\n

    \/**\r\n     * [packExcels \u8ffd\u52a0\u6570\u636e\u5230Excel\u8868\u683c]\r\n     * @param  array  $data     [\u6570\u636e]\r\n     * @param  string $fileName [\u540d\u79f0]\r\n     * @return [type]           [description]\r\n     *\/\r\n    public function packExcels($data=array(),$fileName='report'){\r\n \r\n        \/\/\u5f15\u5165PHPExcel\u7d2f\r\n        require_once ROOT_PATH.'plugins\/PHPExcel\/Classes\/PHPExcel.php';\r\n        require_once ROOT_PATH.'plugins\/PHPExcel\/Classes\/PHPExcel\/IOFactory.php';\r\n        require_once ROOT_PATH.'plugins\/PHPExcel\/Classes\/PHPExcel\/Reader\/Excel5.php';\r\n \r\n        \/\/\u5b9a\u4e49\u4e00\u4e2a\u6587\u4ef6\u6a21\u677f\r\n        $path = ROOT_PATH . 'public\/user.xlsx';\r\n        \/\/\u5b58\u50a8\u6587\u4ef6\u65b0\u5730\u5740\r\n        $fileWjj = ROOT_PATH . 'public\/user_excel\/'.date('Ymd').'\/';\r\n        \/\/\u6587\u4ef6\u540d\u79f0\u62fc\u63a5\u540e\u7f00\r\n        $fileName = $fileName.'.xlsx';\r\n        \/\/\u5224\u65ad\u6587\u4ef6\u5939\u662f\u5426\u5b58\u5728\uff0c\u6ca1\u6709\u5219\u521b\u5efa\r\n        if(!is_dir($fileWjj)){\r\n            \/\/\u53bb\u9664\u5de6\u53f3\u7684\u659c\u6746 \/ \uff0c\u518d\u5206\u5272\u51fa\u6570\u7ec4\r\n            $arr = explode('\/',trim($fileWjj,"\/"));\r\n            \/\/\u5b9a\u4e49\u7a7a\u53d8\u91cf\r\n            $a = '';\r\n            \/\/\u5faa\u73af\u6570\u7ec4\r\n            foreach ($arr as $k => $v) {\r\n                \/\/\u62fc\u63a5\u8def\u5f84\r\n                $a .= $v.'\/';\r\n                \/\/\u5224\u65ad\u5982\u679c\u5f53\u524d\u7684\u8def\u5f84\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\r\n                if(!is_dir($a)){\r\n                    @mkdir($a);\r\n                }\r\n            }\r\n        }\r\n        \/\/\u62fc\u63a5\u8def\u5f84\r\n        $file = $fileWjj.$fileName;\r\n        \/\/\u5c06\u6587\u4ef6\u6a21\u677f\u590d\u5236\u5230\u65b0\u5730\u5740\r\n        copy($path,$file);\r\n        \/\/\u6807\u9898\r\n        \/\/$title = array('\u7f16\u53f7','\u59d3\u540d','\u6027\u522b','\u5e74\u9f84');\r\n        \/\/\u6570\u636e\r\n        \/\/$data = array(array('id'=>1,'username'=>'YQJ','sex'=>'\u7537','age'=>24));\r\n        \/\/\u8868\u683c\u5217\r\n        $column = array('A','B','C');\r\n \r\n        return $this->readExcel($data,$file,$column);\r\n \r\n    }<\/pre>\n

\u4e0b\u8f7d\u65b9\u6cd5\uff1a<\/span><\/p>\n

    \/**\r\n     * [exportexcel \u4e0b\u8f7dExcel]\r\n     * @param  array  $data     [description]\r\n     * @param  array  $title    [description]\r\n     * @param  string $filename [description]\r\n     * @return [type]           [description]\r\n     *\/\r\n    public function exportexcel($data=array(),$title=array(),$filename="report.xlsx"){\r\n \r\n        header("Content-type:application\/octet-stream");\r\n        header("Accept-Ranges:bytes");\r\n        header("Content-type:application\/vnd.ms-excel");\r\n        header("Content-Disposition:attachment;filename=".$filename);\r\n        header("Pragma:no-cache");\r\n        header("Expires:0");\r\n \r\n        if(!empty($title)){\r\n \r\n            foreach ($title as $k => $v) {\r\n                $title[$k] = iconv("utf-8","utf-8",$v);\r\n            }\r\n \r\n            $title = implode("\\t",$title);\r\n            echo "$title\\n";\r\n \r\n        }\r\n \r\n        if(!empty($data)){\r\n \r\n            foreach ($data as $key => $val) {\r\n                \r\n                foreach ($val as $ck => $cv) {\r\n                    $data[$key][$ck] = iconv("utf-8","utf-8",$cv);\r\n                }\r\n \r\n                $data[$key] = implode("\\t",$data[$key]);\r\n \r\n            }\r\n            echo implode("\\n",$data);\r\n \r\n        }\r\n \r\n    }<\/pre>\n

\u6ce8\u610f\uff1a<\/span><\/p>\n

1\u3001\u5f15\u5165\u7684PHPExcel\u6587\u4ef6\u8def\u5f84\u5fc5\u987b\u6539\u6210\u4f60\u81ea\u5df1\u7684\uff0c\u5426\u5219\u62a5\u9519\uff1b<\/span><\/p>\n

2\u3001\u6ce8\u610f\u6587\u4ef6\u540e\u7f00\u540d\uff0cxls \/ xlsx  \u8fd9\u4e24\u4e2a\u662f\u6709\u533a\u522b\u7684\uff0c\u4e0a\u9762\u4f7f\u7528\u7684\u662fxlsx\uff0c\u5982\u679c\u4f7f\u7528xls\u7684\u9700\u8981\u6539\u6b63\uff1b<\/span><\/p>\n

<\/p>\n","protected":false},"excerpt":{"rendered":"

\u8ffd\u52a0\u65b9\u6cd5\uff1a     \/**    &nb […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,92],"tags":[],"class_list":["post-2056","post","type-post","status-publish","format-standard","hentry","category-php","category-phpcode"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2056"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2056"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2056\/revisions"}],"predecessor-version":[{"id":2057,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2056\/revisions\/2057"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}