通過 PHPExcel_IOFactory 讀取 csv文件

public function ss()
    {
        $path = 'G:\PHP\Goods\uploads\20190715\4ff70347056c40876cda40e2f2fde18c.csv';
        
        try {
            //注意 setInputEncoding('GBK') 不設置將導致中文列內容返回boolean(false)或亂碼
            $objReader = \PHPExcel_IOFactory::createReader('CSV')->setDelimiter(',')->setInputEncoding('GBK');
            $objPHPExcel = $objReader->load($path);
            $sheet = $objPHPExcel->getSheet(0); // 讀取第一個工作表
            $highestRow = $sheet->getHighestRow(); // 取得總行數
            $highestColumm = $sheet->getHighestColumn(); // 取得總列數
            
            
            $str = "";
            for ($row = 1; $row <= $highestRow; $row ++) { // 行數是以第1行開始
                
                $str .= $row;
                // 輸出excel表格的內容
                for ($column = 'A'; $column <= $highestColumm; $column ++) {
                    $str .= $sheet->getCell($column . $row)->getValue() . "|";
                }
                $str .= '|' . PHP_EOL;
            }
            
            echo $str;
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        return;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章