thinkphp5 php读取超大的excel文件数据的方案

thinkphp5PHP读取超大的excel文件数据的方案

 public function convert($size)
{
    $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
    return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i];
}
    public function impOrder(){
        ini_set('display_errors',1);
        error_reporting(E_ALL);
        set_time_limit(0);   // 设置脚本最大执行时间 为0 永不过期
        ini_set('memory_limit','2048M');    // 临时设置最大内存占用
        
        Db::startTrans();
        try {
            
            // 引入扩展类
            include './extend/PHPExcel/PHPExcel.php';
            include './extend/PHPExcel/PHPExcel/Writer/Excel5.php';
            include './extend/PHPExcel/PHPExcel/Writer/Excel2007.php';
            include './extend/PHPExcel/PHPExcel/IOFactory.php';

            error_reporting(0);
            import("Excel.PHPExcel");

            $file_name  = input("post.filename");
            if(empty($file_name)){
                return ['code'=>0,'message'=>'请先上传文件'];
            }
            $file_typea = explode('.', $file_name);
            $file_type  = $file_typea[1];
            if (strtolower($file_type) == 'xls')//判断excel表类型为2003还是2007
            {
                $objReader = \PHPExcel_IOFactory::createReader('Excel5');
            } elseif (strtolower($file_type) == 'xlsx') {
                $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
            }
            $start = memory_get_usage();
            $highestRow1=$this->convert($start) . PHP_EOL;//导入文件大小
            $startTime = microtime(true);
            // $Reader = \PHPExcel_Reader_Excel5::SpreadsheetReader($inputFileName);
            $objReader->setReadDataOnly(true);
            $objPHPExcel        = $objReader->load(".$file_name", $encode = 'utf-8');
          
            $sheet              = $objPHPExcel->getSheet(0);
            $highestRow         = $sheet->getHighestRow(); // 取得总行数
            $highestColumn      = $sheet->getHighestColumn(); // 取得总列数
            $highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);
            $excelData          = array();
        //   echo $highestRow;die;
            for ($row = 2; $row <= $highestRow; $row++) {
                $arr=explode('-',$sheet->getCellByColumnAndRow(0, $row)->getValue());
                $excelData[$row]['storied']     = (string)$sheet->getCellByColumnAndRow(0, $row)->getValue();
                $excelData[$row]['name']        = (string)$sheet->getCellByColumnAndRow(1, $row)->getValue();
                $excelData[$row]['phone']       = (string)$sheet->getCellByColumnAndRow(2, $row)->getValue();
                $excelData[$row]['measure']     = (string)$sheet->getCellByColumnAndRow(3, $row)->getValue();
                $excelData[$row]['keytime']     = gmdate("Y-m-d", \PHPExcel_Shared_Date::ExcelToPHP(
                                                   $sheet->getCellByColumnAndRow(4, $row)->getValue()
                                                  ));
                $excelData[$row]['contracttime']= gmdate("Y-m-d", \PHPExcel_Shared_Date::ExcelToPHP(
                                                $sheet->getCellByColumnAndRow(5, $row)->getValue()
                                                ));
                $excelData[$row]['starttime']   =gmdate("Y-m-d", \PHPExcel_Shared_Date::ExcelToPHP(
                                                $sheet->getCellByColumnAndRow(6, $row)->getValue()
                                                ))?gmdate("Y-m-d", \PHPExcel_Shared_Date::ExcelToPHP(
                                                    $sheet->getCellByColumnAndRow(6, $row)->getValue()
                                                    )):date("Y-m-d");
                $excelData[$row]['deposit']     = (string)$sheet->getCellByColumnAndRow(7, $row)->getValue();
                $excelData[$row]['received']    = (string)$sheet->getCellByColumnAndRow(8, $row)->getValue();
                $excelData[$row]['birthday']    = gmdate("Y-m-d", \PHPExcel_Shared_Date::ExcelToPHP(
                                                $sheet->getCellByColumnAndRow(9, $row)->getValue()
                                                ));
                $excelData[$row]['plate']       = (string)$sheet->getCellByColumnAndRow(10, $row)->getValue();
                $excelData[$row]['garage']      = (string)$sheet->getCellByColumnAndRow(11, $row)->getValue();
                $excelData[$row]['area']        =$arr[0];
                $excelData[$row]['unit']        =$arr[1];
                $excelData[$row]['room']        =$arr[2];
                $excelData[$row]['state']       =$sheet->getCellByColumnAndRow(12, $row)->getValue();
                

            }
           
            sort($excelData);
            foreach($excelData as $k=>$v){
                $meResult=db('member')->where(array('storied'=>$v['storied']))->count();
                if($meResult>0){
                    unset($excelData[$k]);
                }
            }
           
            // $count1=ceil(count($excelData)/1000);
            // for ($i=1;$i<=$count1;$i++){
            //     $offset=($i-1)*1000;
            //     $ids=array_slice($excelData,$offset,1000);
            //     $ret=$this->saveAll($ids);
            
            // }
            // $endTime = microtime(true);
            // $memoryUse = memory_get_usage();

            // echo "内存占用:" . $this->convert($memoryUse) . "; 用时:" . ($endTime - $startTime) . PHP_EOL;die;
           $ret= $this->saveAll($excelData);
            Db::commit();
            if($ret){
                foreach ($excelData as $key => $value) {
                    AddLog("导入了【业主管理】栏目下标题为【".$value['name']."】的信息");
                }
                return ['code'=>1,'message'=>'导入成功'];
            }
                
        }catch (Exception $e)
        {
            Db::rollback();
            return ['code'=>0,'message'=>$e->getMessage()];
        }
发布了36 篇原创文章 · 获赞 8 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_41965172/article/details/104440716