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

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值