1. 程式人生 > >php CI框架Excel匯出xlsx表

php CI框架Excel匯出xlsx表

1.最近在工作中用到這個匯出

2.

//前端頁面定義按鈕指到控制器的匯入方法  
public function export(){
        $fileName = "重點交通違法行為資料統計報表.xlsx";
        $fileNames = iconv('UTF-8', 'GBK', $fileName);
        include ROOT_PATHS . 'classes/PHPExcel.php';
        include ROOT_PATHS . 'classes/PHPExcel/Writer/Excel2007.php';
        $objPHPExcel = new PHPExcel();
        //設定excel的屬性:
        //建立人
        $objPHPExcel->getProperties()->setCreator("admin");
        //最後修改人
        $objPHPExcel->getProperties()->setLastModifiedBy("admin");
        //標題
        $objPHPExcel->getProperties()->setTitle("重點交通行為資料統計表");
        //題目
        $objPHPExcel->getProperties()->setSubject("重點交通行為資料統計報表");
        //描述
        $objPHPExcel->getProperties()->setDescription("重點交通違法行為資料統計報表");
        //關鍵字
        $objPHPExcel->getProperties()->setKeywords("重點交通違法行為資料統計報表");
        //種類
        $objPHPExcel->getProperties()->setCategory("重點交通違法行為資料統計報表");
        //設定當前的sheet
        $objPHPExcel->setActiveSheetIndex(0);
        //設定sheet的name
        $objPHPExcel->getActiveSheet()->setTitle('重點交通違法行為資料統計報表');
        //所有表格居中
        $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getRowDimension("1")->setRowHeight(25);
        $objPHPExcel->getActiveSheet()->getRowDimension("2")->setRowHeight(25);
        $objPHPExcel->getActiveSheet()->mergeCells('A1:A2');
        $objPHPExcel->getActiveSheet()->setCellValue('A1', "處罰部門");
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(35);
        //查詢當前違法行為程式碼
        $dm = $this -> m_keytraffic_statistics->select_all_keytraffic();
        $zm = $this -> IntToChr(count($dm));
        $objPHPExcel->getActiveSheet()->mergeCells('B1:'.$zm.'1');
        $objPHPExcel->getActiveSheet()->setCellValue('B1', '2018年重點交通違法資料統計');
        for($k = 1;$k <=count($dm);$k++){
            $zm1 = $this -> IntToChr($k);
            $objPHPExcel->getActiveSheet()->getColumnDimension($zm1)->setWidth(20);
        }
        for($i=1;$i<=count($dm);$i++){
            $zm1 = $this -> IntToChr($i);
            $objPHPExcel->getActiveSheet()->setCellValue($zm1.'2', $dm[$i-1]['WFMS']);
        }
        $objPHPExcel->getActiveSheet()->getStyle( 'A1:'.$zm.'2')->applyFromArray(
            array(
                'font'    => array (
                    'bold'      => true
                ),
                'borders' => array(
                    'allborders' => array( //設定全部邊框
                        'style' => \PHPExcel_Style_Border::BORDER_THIN
                    ),
                ),
            )
        );
        $objPHPExcel->getActiveSheet()->getStyle( 'A1:'.$zm.'32')->applyFromArray(
            array(
                'borders' => array(
                    'allborders' => array( //設定全部邊框
                        'style' => \PHPExcel_Style_Border::BORDER_THIN
                    ),
                ),
            )
        );
        $start_time = $this -> input -> get('start_time',TRUE);
        $end_time = $this -> input -> get('end_time',TRUE);
        $result_data = $this -> data_statistics1($start_time,$end_time);
        $objPHPExcel->getActiveSheet()->setCellValue('A3', "合計");
        for($k = 1;$k <=count($dm);$k++){
            $zm1 = $this -> IntToChr($k);
            $objPHPExcel->getActiveSheet()->setCellValue($zm1.'3', $result_data['zs'][$k-1]);
        }
        for($l = 1; $l <= count($result_data['orgname']);$l++){
            $num = $l + 3;
            $objPHPExcel->getActiveSheet()->setCellValue('A'.$num, $result_data['orgname'][$l-1]);
        }
        for($a = 1;$a<=count($dm);$a++){
            $zm1 = $this -> IntToChr($a);
            for($b = 1;$b <=count($result_data['data'][$a-1]);$b++){
                $num1 = $b + 3;
                $objPHPExcel->getActiveSheet()->setCellValue($zm1.$num1, $result_data['data'][$a-1][$b-1]);
            }
        }
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        //或者$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 非2007格式
        $objWriter->save(ROOT_PATHS."assets/uploads/excel/".$fileNames);
        //下載
        $excelPath = ROOT_PATHS.'assets/uploads/excel/'.$fileNames;
        header( "Content-Disposition:  attachment;  filename=".$fileNames); //告訴瀏覽器通過附件形式來處理檔案
        header('Content-Length: ' . filesize($excelPath)); //下載檔案大小
        readfile($excelPath);  //讀取檔案內容
    }
    //將數字轉化為字母 零就是A  以此類推
    public function IntToChr($index, $start = 65) {
        $str = '';
        if (floor($index / 26) > 0) {
            $str .= IntToChr(floor($index / 26)-1);
        }
        return $str . chr($index % 26 + $start);
    }
    public function data_statistics1($start_time,$end_time)
    {
        $data = $this -> m_keytraffic_statistics->select_all_orgname();  //部門名稱和部門程式碼
        $wfxw = $this -> m_keytraffic_statistics->select_all_keytraffic1();

        foreach ($wfxw as $k1 => $v1){
            $a[] = explode(',',$v1['WFXW']);
        }

        foreach ($data as $k => $v){
            $orgname[] = $v['orgname'];

            $res2 = substr($v['orgnum'],0,8);
            foreach ($a as $k2 => $v2){
                if(count($v2) == 1){
                    $num2 = $this -> db -> like('orgnum',$res2,'after') -> where('wfxw1',$v2['0']) -> where("wfsj between '$start_time 'and '$end_time'") -> where('sendstatus',1) -> where('issend',1) -> get('forceinfo') -> num_rows();
                    $num3 = $this -> db -> like('orgnum',$res2,'after') -> where('wfxw',$v2['0']) -> where("wfsj between '$start_time 'and '$end_time'") -> where('sendstatus',1) -> get('surveilinfo') -> num_rows();
                    $num4 = $this -> db -> like('orgnum',$res2,'after') -> where('wfxw',$v2['0']) -> where("wfsj between '$start_time 'and '$end_time'") -> where('sendstatus',1) -> where('issend',1) -> get('simpleinfo') -> num_rows();
                    $num6[$k2][] = $num2 + $num3 + $num4;
                }else{
                    $num5 = $this -> db -> like('orgnum',$res2,'after') -> where_in('wfxw1',$v2) -> where("wfsj between '$start_time 'and '$end_time'") -> where('sendstatus',1) -> where('issend',1) ->get('forceinfo') -> num_rows();
                    $num7 = $this -> db -> like('orgnum',$res2,'after') -> where_in('wfxw',$v2) -> where("wfsj between '$start_time 'and '$end_time'") -> where('sendstatus',1) -> get('surveilinfo') -> num_rows();
                    $num8 = $this -> db -> like('orgnum',$res2,'after') -> where_in('wfxw',$v2) -> where("wfsj between '$start_time 'and '$end_time'") ->  where('sendstatus',1) -> where('issend',1) ->get('simpleinfo') -> num_rows();
                    $num6[$k2][] = $num5 + $num7 + $num8;
                }
            }
        }
        foreach ($num6 as $k3 => $v3){
            $date['zs'][] = array_sum($v3);
        }
        $date['data'] = $num6;
        $date['orgname'] = $orgname;
        return $date;
    }