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()];
        }

 

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