excel文件的導出

if ($export == 1)//導入條件
        {
            $data = array();
            $data[0][] = 'ID';
            $data[0][] = '用戶id';
            $data[0][] = '申請金額';
            $data[0][] = '實際金額';
            $data[0][] = '開戶行';
            $data[0][] = '卡號';
            $data[0][] = '開戶人姓名';
            $data[0][] = '申請方式';
            $data[0][] = '狀態';
            $data[0][] = '時間';
            $result = $model->where($where)->order('id desc')->select();

            foreach ($result as $k=>$v)
            {
                $data[$k+1][] = $v['id'];
                $data[$k+1][] = $v['uid'];
                $data[$k+1][] = $v['price'];
                $data[$k+1][] = $v['tx_price'];
                $data[$k+1][] = $v['bank_name'];
                $data[$k+1][] = $v['bank_num'];
                $data[$k+1][] = $v['name'];
                if ($v['tixian_type'] == 1) { $data[$k+1][] = '申請提現'; }
                if ($v['tixian_type'] == 2) { $data[$k+1][] = '紅包發放'; }
                if ($v['tixian_type'] == 3) { $data[$k+1][] = '企業付款'; }
                if ($v['status'] == 1) { $data[$k+1][] = '已完成'; }
    //導出excel文件方法
    public function excel($data=null,$merge_arr=null,$sheetTitle=null){
        if(empty($sheetTitle)){
            $excelFileName=$sheetTitle='data';    
        }else{
            $excelFileName=$sheetTitle;
        }
        
        /*導入phpExcel核心類 */
        require_once APP_PATH.'PHPExcel/PHPExcel.php'; 
        require_once APP_PATH.'PHPExcel/PHPExcel/Writer/Excel5.php';     // 用於其餘低版本xls 
        require_once APP_PATH.'PHPExcel/PHPExcel/Writer/Excel2007.php'; // 用於 excel-2007 格式 
        /* 實例化類 */
        $objPHPExcel = new PHPExcel(); 
        
        /* 設置輸出的excel文件爲2007兼容格式 */
        //$objWriter=new PHPExcel_Writer_Excel5($objPHPExcel);//非2007格式
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        
        /* 設置當前的sheet */
        $objPHPExcel->setActiveSheetIndex(0);
        
        $objActSheet = $objPHPExcel->getActiveSheet();
        
        /*設置寬度*/
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
        
        //設置自動換行
        $arr=array('J','K','L','R');
        foreach ($arr as $v) {
            $objActSheet ->getStyle($v)->getAlignment()->setWrapText(true);//自動換行
        }    
        /* sheet標題 */
        $objActSheet->setTitle($sheetTitle);
        
        $i = 1;
        foreach($data as $value)
        {
            /* excel文件內容 */
            $j = 'A';
            foreach($value as $value2)
            {
                $objActSheet->setCellValueExplicit($j.$i,$value2,PHPExcel_Cell_DataType::TYPE_STRING);
                $j++;
            }
            $i++;
        }
        
        //guth 同一個訂單再把相同的內容給合併
        foreach($merge_arr as $info)
        {
            $start_num = $info[0];
            $end_num = $info[count($info)-1];
            $objPHPExcel->getActiveSheet()->mergeCells('A'.$start_num.':A'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('B'.$start_num.':B'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('C'.$start_num.':C'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('D'.$start_num.':D'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('E'.$start_num.':E'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('F'.$start_num.':F'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('G'.$start_num.':G'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('H'.$start_num.':H'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('I'.$start_num.':I'.$end_num.'');        
            $objPHPExcel->getActiveSheet()->mergeCells('J'.$start_num.':J'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('K'.$start_num.':K'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('Q'.$start_num.':Q'.$end_num.'');            
            $objPHPExcel->getActiveSheet()->mergeCells('R'.$start_num.':R'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('S'.$start_num.':S'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('T'.$start_num.':T'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('U'.$start_num.':U'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('V'.$start_num.':V'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('W'.$start_num.':W'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('X'.$start_num.':X'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('Y'.$start_num.':Y'.$end_num.'');
            $objPHPExcel->getActiveSheet()->mergeCells('Z'.$start_num.':Z'.$end_num.'');
        }
        
        //$objPHPExcel->getActiveSheet()->mergeCells('K5:L6');
        /* 生成到瀏覽器,提供下載 */ 
        ob_end_clean();  //清空緩存             
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header('Content-Disposition:attachment;filename="'.$excelFileName.'.xlsx"');
        header("Content-Transfer-Encoding:binary"); 
        $objWriter->save('php://output');
    }
 
 

 

elseif ($v['status'] == 2) { $data[$k+1][] = '已退回'; }
                elseif ($v['status'] == 3) { $data[$k+1][] = '紅包發放中'; }
                else{$data[$k+1][] = '待審覈';}
                $data[$k+1][] = $v['time'];
            }
            R('excel',array($data));
            exit;
        }
相關文章
相關標籤/搜索