php知识总结

PhpSpreadsheet导入Csv中文无法读取

2022-02-21  本文已影响0人  响呼雷
 public function departThirdInfo()
    {
        $file = $request->file('file');
        if (!$file){
            return error('请传入文件');
        }
        $realpath = $file->getRealPath();
        $extension = $file->getClientOriginalExtension();
        if ($extension == 'xlsx'){
            $reader = new Xlsx();
            $spreadsheet = $reader->load($realpath);
        }elseif ($extension == 'xls'){
            $reader = new Xls();
            $spreadsheet = $reader->load($realpath);
        }elseif ($extension == 'csv'){
            $reader = new Csv();
            $reader->setInputEncoding('GB2312'); //这一步必须,否则无法识别中文
            $spreadsheet = $reader->load($realpath);
        }
        $sheet = $spreadsheet->getActiveSheet();
        $row_count = $sheet->getHighestRow();
        for ($row = 2;$row <= $row_count+1;$row++){
            $third_police_units = $sheet->getCell('I'.$row)->getValue();
            //编写自己的逻辑
        }
        return success();
    }

导出操作请看PhpSpreadsheet导出Csv

上一篇下一篇

猜你喜欢

热点阅读