1. 程式人生 > >PHPExcel之將獲取到的資料 匯出excel

PHPExcel之將獲取到的資料 匯出excel

tp3.2(注意:project\ThinkPHP\Library\Org\Util\PHPExcel    引用方法: use Org\Util;       )

public function export_nj()
    {
        $data = M('admin')->alias('a')
            ->join('right join oa_ren_newwork n on n.uid=a.id')
            ->field('n.id,n.uid,n.username,n.tztime,n.njtime')->where(['a.status'=>1])->select();
        foreach($data as $k=>$v){
            $wherek['user_id']         =$v['uid'];
            $wherek['renshi_build_id'] =3;//請假型別
            $wherek['save_sub_statue'] =3;//請假批准
            $wherek['leave_type']     =1; //年假型別
            $njyytime=M('oa_ren_work')->where($wherek)->sum('leavetime');
            $data[$k]['njyytime']=round($njyytime/8, 2);
            $data[$k]['njsytime']=$data[$k]['njtime']-$data[$k]['njyytime'];
            if($data[$k]['njyytime']==''){
                $data[$k]['njyytime']=" ";
            }
            unset($data[$k]['njyytime']);
            unset($data[$k]['id']);
        }
//        dump($data);die;
//        $title=array('使用者','花名','調休剩餘時間','年假時間','年假剩餘天數');
//      if(!empty($result)) {
        $objPHPExcel = new Util\PHPExcel();
        // Set document properties
        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
            ->setLastModifiedBy("Maarten Balliauw")
            ->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");

        // Add some data
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '使用者id')
            ->setCellValue('B1', '花名')
            ->setCellValue('C1', '調休剩餘時間/h')
            ->setCellValue('D1', '年假總天數')
            ->setCellValue('E1', '年假剩餘天數/天');
        $rowIndex=2;

        foreach ($data as $dataRow) {
            $objPHPExcel->getActiveSheet()
                ->setCellValue('A'.$rowIndex, $dataRow['uid'])
                ->setCellValue('B'.$rowIndex, $dataRow['username'])
                ->setCellValue('C'.$rowIndex, $dataRow['tztime'])
                ->setCellValue('D'.$rowIndex, $dataRow['njtime'])
                ->setCellValue('E'.$rowIndex,$dataRow['njsytime']);

            $rowIndex++;
        }

        $urlMonth = I('get.month');
        $tableTitle = "oa年假";
        $objPHPExcel->getActiveSheet()->setTitle($tableTitle);
        $objPHPExcel->setActiveSheetIndex(0);

        $fileName = "{$tableTitle}.xlsx";

        //清空緩衝區,否則匯出的檔案可能打不開
        ob_end_clean();
        ob_start();

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'.$fileName.'"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');
        exit;
    }