php 生成 excel

/**
 * 導出當前的提現記錄 ci框架  部分要更具本身狀況修改
 * @return [ecexl] [生成excel文檔]
 */
function excelapply(){
		$keysid = $_GET['keysid'];

        $memberid = $this->userinfo['id'];
        $date = array();
        $sql="select * from wx_cash_withdrawal where memberid={$memberid} and id in ({$keysid})";
        $sql .=" order by id desc";
        $query = $this->db->query($sql);
        $res = $query->result_array();

        require_once('./application/libraries/Phpexcel.php');
        require_once './application/libraries/PHPExcel/Writer/Excel5.php';
        // Create new PHPExcel object
        $objPHPExcel = new PHPExcel();

        $objProps = $objPHPExcel->getProperties();
        $objProps->setCreator("tax");
        $objProps->setTitle("提現記錄");

        $objPHPExcel->setActiveSheetIndex(0);
        $sheet = $objPHPExcel->getActiveSheet();
        $objPHPExcel->getActiveSheet()->getStyle('J')->getAlignment()->setWrapText(true);
        $objPHPExcel->getActiveSheet()->getStyle('K')->getAlignment()->setWrapText(true);
        $objPHPExcel->getActiveSheet()->getStyle('L')->getAlignment()->setWrapText(true);
        $objPHPExcel->getActiveSheet()->getStyle('M')->getAlignment()->setWrapText(true);
// 標題
        $sheet->setTitle('提現記錄');
        $sheet->setCellValue('A1', '序號');
        $sheet->setCellValue('B1', '開戶人姓名');

        $sheet->setCellValue('C1', '金額(元)');
        $sheet->setCellValue('D1', '開戶行/支行');
        $sheet->setCellValue('E1', '申請時間');
        $sheet->setCellValue('F1', '手機號');
        $sheet->setCellValue('G1', '狀態');


        /*$sheet->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $sheet->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		*/

        $sheet->getColumnDimension('A')->setWidth(5);
        $sheet->getColumnDimension('B')->setWidth(10);
        $sheet->getColumnDimension('C')->setWidth(10);
        $sheet->getColumnDimension('D')->setWidth(20);
        $sheet->getColumnDimension('E')->setWidth(20);
        $sheet->getColumnDimension('F')->setWidth(20);
        $sheet->getColumnDimension('G')->setWidth(20);

        $i = 2;
        foreach ($res as $k=>$v) {
        	switch ($v['bankname']) {
                case '1':
                    $bank='中國銀行';
                    break;
                case '2':
                    $bank='中國工商銀行';
                    break;
                case '3':
                    $bank='中國建設銀行';
                    break;
                case '4':
                    $bank='中國農業銀行';
                    break;
                case '5':
                    $bank='中國交通銀行';
                    break;
                case '6':
                    $bank='中國郵政儲蓄';
                    break;
                case '7':
                    $bank='支付寶';
                    break;
                case '8':
                    $bank='財付通';
                    break;
                default:
                    $bank='空';
                    break;
            }

            $sheet->setCellValue('A' . $i, $k+1);
            $sheet->setCellValue('B' . $i, $v['name']);
            $sheet->setCellValue('C' . $i, $v['money']);
            $sheet->setCellValue('D' . $i, $bank);
            $sheet->setCellValue('E' . $i, date('Y-m-d H:i',$v['creattime']));
            $sheet->setCellValue('F' . $i, ' '.$v['mobile'].' ');
            $sheet->setCellValue('G' . $i, $v['status']==1?'待提現':'已提現');
            $i++;
        }
        $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
       // 輸出 Excel
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="提現記錄'.date('Y-m-d H-i-s',time()).'.xls"');
        header('Cache-Control: max-age=0');
        //$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');

    }
相關文章
相關標籤/搜索