天天看點

Tp5.1使用phpexcel導入導出項目場景:解決方案:問題總結:

項目場景:

TP5.1使用phpexcel第三方包進行資料導入導出操作,并實作導入後上傳檔案删除及導出後資料庫資料删除操作。

解決方案:

資源包下載下傳:https://github.com/PHPOffice/PHPExcel

操作方法:下載下傳後放入vendor檔案夾下

操作案例(實作導入後上傳檔案删除及導出後資料庫資料删除操作):

/**
 * [listExportDo 導入]
 * @return [type] [description]
 */
public function listExportDo()
    {
        $return_data['status'] = 0;
        $return_data['msg'] = "導入失敗!";

        $postParam = input('post.');

        if(!$postParam['list_fuser']){
            return $return_data;die;
        }

        vendor("PHPExcel.PHPExcel");
        $objPHPExcel = new \PHPExcel;

        $file = request()->file('upload_file');
        // dump($file);die;

        $info = $file->validate(['ext' => 'xls'])->move(ROOT_PATH . 'public' . DS . 'uploads');  
        try {
            if($info)
            {
               $exclePath = $info->getSaveName();  //擷取檔案名
               $file_name = ROOT_PATH . 'public' . DS . 'uploads\\' . $exclePath;//上傳檔案的位址
               // 檔案格式為xls 時,使用createReader("Exce15")
               // 檔案格式為xlsx時,使用createReader("Exce2007")
               // $objReader =\PHPExcel_IOFactory::createReader("Exce2007");
               $objReader =\PHPExcel_IOFactory::createReader("Excel5");
               $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');  //加載檔案内容,編碼utf-8
               $excel_array=$obj_PHPExcel->getSheet(0)->toArray();   //轉換為數組格式
               array_shift($excel_array);  //删除第一個數組(标題);
               $city = [];
               $i=0;
               $time = time();
               foreach($excel_array as $k=>$v) {
                   $city[$k]['list_name'] = $v[0];
                   $city[$k]['list_phone'] = $v[1];
                   $city[$k]['list_addtime'] =$time;
                   $city[$k]['list_fuserid'] =$postParam['list_fuser'];
                   $i++;
               }
               $insertRes = db("lists")->insertAll($city);
               if($insertRes){
                    $return_data['status'] = 1;
                    $return_data['msg'] = "導入成功!";

                    // 删除檔案
                    unset($info);
                    unlink($file_name);

               }
            }else
            {
               // echo $file->getError();
                $return_data['errormsg'] = $file->getError(); 
            }
        } catch (Exception $e) {
            $return_data['errormsg2'] = $e->getError();
        }

        return $return_data;
    }
    
    public function listImportDo(){
        // 身份驗證
        $currentUser = db("backlogin")->where('id','=',session("loginback_id"))->field('login_type')->find();

        if($currentUser['login_type']==1){
            $list = db('lists')->select();
        }else{
            $list = db('lists')->where('list_fuserid','=',session("loginback_id"))->select();
        }
        vendor("PHPExcel.PHPExcel");
        $objPHPExcel = new \PHPExcel();

        $objPHPExcel->getProperties()->setCreator("ctos")
            ->setLastModifiedBy("ctos")
            ->setTitle("Office 2007 XLSX Test Document")
            ->setSubject("Office 2007 XLSX Test Document")
            ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
            ->setKeywords("office 2007 openxml php")
            ->setCategory("Test result file");

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(8);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);


        //合并cell
        $objPHPExcel->getActiveSheet()->mergeCells('A1:D1');

        // set table header content
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '資料中心導出  時間:'.date('Y-m-d H:i:s'))
            ->setCellValue('A2', '編号')
            ->setCellValue('B2', '使用者名')
            ->setCellValue('C2', '手機号')
            ->setCellValue('D2', '時間');


        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<count($list);$i++){
            $autoId = $i+1;
            $objPHPExcel->getActiveSheet(0)->setCellValue('A'.($i+3), $autoId);
            $objPHPExcel->getActiveSheet(0)->setCellValue('B'.($i+3), $list[$i]['list_name']);
            $objPHPExcel->getActiveSheet(0)->setCellValue('C'.($i+3), $list[$i]['list_phone']);
            $objPHPExcel->getActiveSheet(0)->setCellValue('D'.($i+3), date("Y-m-d H:i:s",$list[$i]['list_addtime']));
            $objPHPExcel->getActiveSheet()->getRowDimension($i+3)->setRowHeight(16);
        }


        //  sheet命名
        $objPHPExcel->getActiveSheet()->setTitle('資料中心');


        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);

        // excel頭參數
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="資料中心('.date('Ymd-His').').xls"');  //日期為檔案名字尾
        header('Cache-Control: max-age=0');

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  //excel5為xls格式,excel2007為xlsx格式

        $objWriter->save('php://output');

        // 删除
        if($currentUser['login_type']==1){
            $list = db('lists')->where('1=1')->delete();
        }else{
            $list = db('lists')->where('list_fuserid','=',session("loginback_id"))->delete();
        }

    }
           

問題總結:

"continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"

Tp5.1使用phpexcel導入導出項目場景:解決方案:問題總結:

 ZipArchive::locateName(): Invalid or uninitialized Zip object

Tp5.1使用phpexcel導入導出項目場景:解決方案:問題總結: