php導出excel。phpexcel

/*
 * 批量導出功能
 * pid  項目id
 * yq   抓取平臺
 * object_title   監測對象名稱
 * keyword  關鍵詞
 * title  評論裏面的標題
 * status 狀態
 * timetype  篩選的時間類型 1 最近1天 2最近7天 3最近30天
 */
 public function CommentExcel(){

        set_time_limit(0);
        ini_set('memory_limit', '3072M');
        $map['pid'] = $pid = I('pid');
//        $pid = 1822;
        $projectName = M('project')->where(array('id' => $pid))->find()['title'];
        //高頻詞獲取
        $high_words_count = M('project')->where(array('id' => $pid))->find()['high_words_count'];
        $high_words_count = unserialize($high_words_count);
//        $high_words_count = [
//            "day"=> [[
//                "keyword"=> "測試7",
//       "num"=> 123
//    ],
//                [
//                    "keyword"=> "測試6",
//                    "num"=> 123
//                ]
//
// ],
//            "week"=> [[
//                "keyword"=> "測試2",
//                "num"=> 123
//            ],
//                [
//                    "keyword"=> "測試3",
//                    "num"=> 123
//                ]
//
//            ],
//            "month"=> [[
//                "keyword"=> "測試4",
//                "num"=> 123
//            ],
//                [
//                    "keyword"=> "測試5",
//                    "num"=> 123
//                ]
//
//            ],
//];

        $word = $high_words_count['day'];
        //時間篩選
        $timeType=I('timetype');
        if(1 == $timeType){
            $startTime = time()-3600*24;
            $endTime = time();
            $word = $high_words_count['day'];
            $map['screen_time'] =  array('between',$startTime.','.$endTime);
        }elseif (2 == $timeType){
            $startTime = time()-3600*7*24;
            $endTime = time();
            $word = $high_words_count['week'];
            $map['screen_time'] =  array('between',$startTime.','.$endTime);
        }elseif (3 == $timeType){
            $startTime = time()-3600*24*30;
            $endTime = time();
            $word = $high_words_count['month'];
            $map['screen_time'] =  array('between',$startTime.','.$endTime);
        }

        //處理高頻詞格式
        $newWord = [];
        foreach ($word as $kkk => $vvv) {
            $newWord[$kkk]['index'] = $kkk+1;
            $newWord[$kkk]['keyword'] = $vvv['keyword'];
            $newWord[$kkk]['num'] = $vvv['num'];
        }
        foreach ($newWord as $kkkk => $vvvv) {
            $newWord[$kkkk] = array_values($vvvv);
        }

        $yq=I('yq');
        if($yq){
            $map['yq'] = $yq;
        }else{
            $map['yq'] = -1;
        }
        $objectTitle=I('object_title');
        if($objectTitle){
            if($objectTitle != '全部'){
                $objectTitle = explode(',',$objectTitle);
                $map['object_title'] = array('in',$objectTitle);
            }
        }else{
            $map['object_title'] = 888888800;
        }
        //關鍵詞選擇全部的時候,不傳值給我
        $keyword=I('keywords');
        if($keyword){
            $map['keyword'] = $keyword;
        }
        //評論篩選
        $title=I('title');
        if($title){
            $map['title'] = array('like',"%{$title}%");
        }
        //狀態篩選,默認傳值1,已經刪除的時候傳值-1
        $status=I('status');
        if($status){
            $map['status'] = $status;
        }


        $list = M('project_info')->field('id,object_title,url,title,keyword,screen_name,fens_count,content,dzs,screen_time')->where($map)->select();

        foreach ($list as $k=>$v){
            $list[$k]['id'] = $v['id'];
            $list[$k]['object_title'] = $v['object_title'];
            $list[$k]['title'] = $v['title'];
            $list[$k]['url'] = $v['url'];
            $list[$k]['keyword'] = $v['keyword'];
            $list[$k]['screen_name'] = $v['screen_name'];
            $list[$k]['fens_count'] = $v['fens_count'];
            $list[$k]['content'] = $v['content'];
            $list[$k]['dzs'] = $v['dzs'];
            $list[$k]['screen_time'] = date('Y-m-d',$v['screen_time']);
        }
        $newList = [];
        foreach ($list as $kk => $vv) {
            $newList[$kk] = array_values($vv);
        }
//        myLog($newList);die;
        $expCellName = array('序號','採集對象', '監測鏈接','微博正文', '關鍵詞', '評論人', '粉絲數', '評論內容', '點贊數', '評論時間');
        $fileName = $projectName.date('mdHis');
        $cellNum = count($expCellName);        //多少列
        $dataNum = count($newList);
        vendor("PHPExcel.PHPExcel");
        vendor("PHPExcel.PHPExcel.Writer.Excel5");

        $allnums = $dataNum+1;
        $objPHPExcel = new \PHPExcel();            //初始化
        $this->objPHPExcel = new \PHPExcel();
        $objPHPExcel->getActiveSheet()->setTitle('評論列表');
        $objPHPExcel->getDefaultStyle()->getFont()->setName('微軟雅黑');
        $this->objActiveSheet=$this->objPHPExcel->getActiveSheet();
        $objActiveSheet=$objPHPExcel->getActiveSheet();

        $objActiveSheet->getDefaultStyle()->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
            ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);

        $objActiveSheet->getDefaultStyle()->getFont()->setSize(10);
        $objPHPExcel->getActiveSheet()->getstyle("F2:H".$allnums)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objActiveSheet->freezePane('B2');

        $cellName = array(
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'
        );
        $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(18);
        //設置第一行爲藍色背景,字體顏色爲白色
        $objPHPExcel->getActiveSheet()->getStyle("A1:J1")->getFont()->setBold(TRUE)
            ->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_WHITE);
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(15);
//        $this->create_header();
        //functions.php裏面的函數cellcolor
        cellColor('A1:J1', '4472C4', $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getStyle("Q38")->getFont()->setBold(TRUE)
            ->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_WHITE);
//        $objPHPExcel->getDefaultStyle()->getFont()->setColor(new \PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_WHITE));
        for ($i = 0; $i < $cellNum; $i++) {
            //表頭(列名)
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . '1', $expCellName[$i]);
        }


        // Miscellaneous glyphs, UTF-8
        for ($i = 0; $i < $dataNum; $i++) {
            for ($j = 0; $j < $cellNum; $j++) {
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 2),$newList[$i][$j]);
            }
        }

        //第二個sheet
        $sheet2 = $objPHPExcel->createSheet();//創建第二個sheet
        $objPHPExcel->setactivesheetindex(1);//區別於第一個sheet,
        $objPHPExcel->getActiveSheet(1)->setTitle('高頻詞');
        $objPHPExcel->getActiveSheet(1)->getColumnDimension('A')->setWidth(5);
        $objPHPExcel->getActiveSheet(1)->getColumnDimension('B')->setWidth(12);
        $objPHPExcel->getActiveSheet(1)->getColumnDimension('C')->setWidth(12);

        $cellName2 = array(
            'A', 'B', 'C'
        );
        $expCellName2 = array('序號','高頻詞', '數量');
        //設置第一行爲藍色背景,字體顏色爲白色
        $objPHPExcel->getActiveSheet(1)->getStyle("A1:C1")->getFont()->setBold(TRUE)
            ->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_WHITE);
        cellColor1('A1:C1', '4472C4', $objPHPExcel);
        $cellNum2 = count($expCellName2);        //多少列
        $dataNum2 = count($newWord);


        for ($i = 0; $i < $cellNum2; $i++) {
            //表頭(列名)
            $objPHPExcel->setActiveSheetIndex(1)->setCellValue($cellName2[$i] . '1', $expCellName2[$i]);
        }


        // Miscellaneous glyphs, UTF-8
        for ($i = 0; $i < $dataNum2; $i++) {
            for ($j = 0; $j < $cellNum2; $j++) {
                $objPHPExcel->getActiveSheet(1)->setCellValue($cellName2[$j] . ($i + 2),$newWord[$i][$j]);
            }
        }
        $objPHPExcel->setActiveSheetIndex(0);

        if(ob_get_length() > 0) {
            ob_clean();
        } //清除緩衝區,避免亂碼
        //直接頁面輸出xlsx文件
        header('Content-Type: application/vnd.ms-excel');
        header('pragma:public');
        //attachment新窗口打印inline本窗口打印
        header("Content-Disposition:attachment;filename=$fileName.xlsx");
        $objWriter = new \Excel5($objPHPExcel);
        $objWriter->save('php://output');
        exit;


    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章