329s 2017-12-22 19:19:54 6512次浏览 7条回复 0 0 0

PHPExcel 本地导出一切正常,上服务器后出现问题!最后问题发现在save的时候报错,网上找了好久没有解决,请问各位兄弟们能给点指导吗? `public function writeFile($sheet)

{
	if (!isset($this->format))
		$this->format = 'Excel2007';
	$objectwriter = \PHPExcel_IOFactory::createWriter($sheet, $this->format);
	$path = 'php://output';
	if (isset($this->savePath) && $this->savePath != null) {
		$path = $this->savePath . '/' . $this->getFileName();
	}
	$objectwriter->save($path);
	exit();
}`
  • 回复于 2017-12-23 16:25 举报

    报错信息是啥?

    1 条回复
    回复于 2017-12-25 14:30 回复

    报错信息就是无法访问此网站,网页可能暂时无法连接,或者它已永久性地移动到了新网址。
    ERR_INVALID_RESPONSE

  • 回复于 2017-12-25 10:35 举报

    是不是线上数据量太大了?

    1 条回复
    回复于 2017-12-25 14:30 回复

    不是,线上不大的

  • 回复于 2017-12-25 14:17 举报

    给你个创建excel的方法, 肯定好用

    public static function createExcel(PHPExcel $objPHPExcel = NULL,$fileName = 'example.xlsx',$download = false)
    {
        $objPHPExcel = $objPHPExcel ? $objPHPExcel : new PHPExcel;
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        if($download){
            $fileName = basename($fileName);
            $fileName = iconv("utf-8", "gb2312", $fileName);
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename='.$fileName);
            header('Cache-Control: max-age=0');
            $objWriter->save('php://output'); //文件通过浏览器下载
        }else{
            $dirname = dirname($fileName);
            if ($dirname != '.') {
                //文件路径如果不存在则递归创健
                CommonFun::recursionMkDir($dirname);
            }
            $objWriter->save($fileName); //脚本方式运行,保存在指定目录
            if(!file_exists($fileName)) {
                return false;
            }
            return true;
        }
    }
    
    self::createExcel($objPHPExcel,$fileName);
    
    3 条回复
    回复于 2017-12-25 14:31 回复

    thank you 我试试

    回复于 2017-12-25 14:41 回复

    CommonFun::recursionMkDir($dirname); 这个可以用Yii 自带的FileHelper::createDirectory($dirname)

    回复于 2017-12-26 11:09 回复

    谢谢,已经解决了,这是我解决的方法:http://blog.csdn.net/u014273681/article/details/78893911, 虽然还不知道为什么之前没有出现这个问题,我在研究研究

  • 回复于 2017-12-25 17:13 举报

    ` /**

     * actionExecl 导出Execl
     */
    public function actionExecl($execlString)
    {
        $sql = yii::$app->security->decryptByPassword(base64_decode($execlString), Yii::$app->params['cryptSecretKey']);
    
        if(!$sql){
            Yii::$app->session->setFlash('error', '参数错误');
            return $this->redirect(['/search-record/index']);
        }
    
        $result = Yii::$app->db->createCommand($sql)->queryAll();
    
        if(!$result){
            Yii::$app->session->setFlash('error', '没有导出的数据');
            return $this->redirect(['/search-record/index']);
        }
    
        $objectPHPExcel = new \PHPExcel();
        $objectPHPExcel->setActiveSheetIndex(0);
        //报表头的输出
        $objectPHPExcel->getActiveSheet()->mergeCells('A1:J1');
        $objectPHPExcel->getActiveSheet()->setCellValue('A1','搜索统计');
        $objectPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getFont()->setSize(24);
        $objectPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    
        //表格头的输出
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('A2','序号');
        $objectPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(25);
    
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('B2','关键词');
        $objectPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
    
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('C2','搜索频次');
        $objectPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
    
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('D2','更新时间');
        $objectPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
    
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('E2','是否为热搜');
        $objectPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
    
        $objectPHPExcel->setActiveSheetIndex(0)->setCellValue('F2','关键词来源');
        $objectPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
        //设置居中
        $objectPHPExcel->getActiveSheet()->getStyle('A2:J2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objectPHPExcel->getActiveSheet()->getStyle('A:J')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    
        $i=3;
    
        $sourceLabel = SearchRecord::$sourceLabel;
    
        foreach($result as $key=>$vo)
        {
            $objectPHPExcel->getActiveSheet()->setCellValue('A'.$i ,$key+1);
            $objectPHPExcel->getActiveSheet()->setCellValue('B'.$i ,$vo['name']);
            $objectPHPExcel->getActiveSheet()->setCellValue('C'.$i ,$vo['times']);
            $objectPHPExcel->getActiveSheet()->setCellValue('D'.$i ,$vo['updateTime']);
            $objectPHPExcel->getActiveSheet()->setCellValue('E'.$i ,$vo['isHot']?'是':'否');
            $objectPHPExcel->getActiveSheet()->setCellValue('F'.$i ,$sourceLabel[$vo['source']]??'');
            $i++;
        }
    
        $objectPHPExcel->getActiveSheet()->getPageSetup()->setHorizontalCentered(true);
        $objectPHPExcel->getActiveSheet()->getPageSetup()->setVerticalCentered(false);
        ob_end_clean();
        ob_start();
        header('Content-Type : application/vnd.ms-excel');
        header('Content-Disposition:attachment;filename="'.'搜索统计-'.date("Y年m月j日").'.xls"');
        $objWriter= \PHPExcel_IOFactory::createWriter($objectPHPExcel,'Excel2007');
        $objWriter->save('php://output');
    }`
    
    1 条回复
    回复于 2017-12-26 11:11 回复

    谢谢已经解决了,应该是路径问题,虽然不知道之前为什么没有报错,http://blog.csdn.net/u014273681/article/details/78893911

  • 回复于 2019-01-03 14:39 举报

    怎么解决的啊?

  • 回复于 2019-01-05 11:52 举报

    改 Excel5 没解决问题

  • 回复于 2019-01-05 11:52 举报

    改 Excel5 没解决问题

您需要登录后才可以回复。登录 | 立即注册