基於codeigniter3框架使用PHPspreadsheet包實現excel模板導出文件

基於codeigniter3框架使用PHPspreadsheet包實現excel模板導出文件

具體使用

  • 具體調用方法
/**
    * 方法 export_project_apply_detail_after_samp_as_excel_by_template_serv,通過excel模板導出項目抽檢申請測量後詳情數據到excel
    *
    * @param array $params 參數數組
    *
    * @return mixed 成功返回true,失敗返回false
    */
   public function export_project_apply_detail_after_samp_as_excel_by_template_serv(array $params){
       $this->load->helper('project_apply_export_excel');

       /*//獲取項目抽檢申請詳情(包含項目地址所屬公司等信息)
       $project_apply_department = $this->get_project_apply_department_detail_serv($params);
       //獲取項目抽檢申請檢查項列表
       $resolve_item_list = $this->deal_get_project_apply_item_list_serv($params);*/

       //獲取項目公司檢查項及測量數據
       $project_apply_department = $this->get_m_project_apply_samp_report_serv($params);

       //處理成可寫入excel的數據格式
       /*echo '<pre>';
       var_dump($project_apply_department);
       die();*/
       $excel_data = h_export_project_apply_detail_after_samp_as_excel_by_template($project_apply_department);
       //通過工廠模式創建內容
       try{
           $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load(FCPATH.'public/template/單個公司導出抽檢詳細Excel表3.xls');
           $worksheet = $spreadsheet->getActiveSheet();

           //填充數據
           foreach($excel_data as $k => $v){
               $worksheet->getCell($k)->setValue($v);
           }

           //生成文件到oss
           $oss_path = 's_c_s_l/excel/';
           $save_path = getcwd().'/'.$oss_path;
           $file_ext = 'xls';
           $file_name = $project_apply_department['project_name'].'-實測實量現場記錄表';

           //通過工廠模式來寫內容
           $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, ucfirst($file_ext));
           $writer->save($save_path.$file_name.'.'.$file_ext);

           $export_res = true;
           //導出,在瀏覽器輸出二進制流主動生成excel文件
           if($export_res){
               $export_res = [
                   'oss_path' => $oss_path.$file_name.'.'.$file_ext,
                   'local_full_path' => $save_path.$file_name.'.'.$file_ext,
               ];
           }
           return $export_res;
       }catch (Exception $e){
           return false;
       }
   }

支付寶
在這裏插入圖片描述

********************只要思想不滑坡,辦法總比困難多********************
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章