Yii2.0 PHPExcel 本地导出一切正常,上服务器后出现问题! [ 新手入门 ]
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();
}`
共 7 条回复
-
给你个创建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 条回复谢谢,已经解决了,这是我解决的方法:http://blog.csdn.net/u014273681/article/details/78893911, 虽然还不知道为什么之前没有出现这个问题,我在研究研究
-
` /**
* 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 条回复谢谢已经解决了,应该是路径问题,虽然不知道之前为什么没有报错,http://blog.csdn.net/u014273681/article/details/78893911
329s
注册时间:2016-05-19
最后登录:2019-03-05
在线时长:6小时35分
最后登录:2019-03-05
在线时长:6小时35分
- 粉丝1
- 金钱50
- 威望0
- 积分110