通过 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;
    }
发布了27 篇原创文章 · 获赞 53 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/auspi12341/article/details/96040386