通过 PHPExcel_IOFactory 读取 csv文件

来源:csdn博客 分类: 文章浏览史 发布时间:2020-11-27 14:29:45 最后更新:2020-11-27 浏览:683
转载声明:
本文为摘录自“csdn博客”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2020-11-27 14:29:45
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;
    }

 

php技术微信