PhpExcel类,在windows系统下能导出Excel文件,但是在linux下就不行,怎么回事?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PhpExcel类,在windows系统下能导出Excel文件,但是在linux下就不行,怎么回事?相关的知识,希望对你有一定的参考价值。
这是导出的界面
以下是代码:
//导出Excel if($_POST['file_format']=="excel") //导出excel Vendor("phpExcel.IOFactory"); $objectPHPExcel = new PHPExcel(); $objectPHPExcel->setActiveSheetIndex(0);
.......... ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); ............ //设置居中 $objectPHPExcel->getActiveSheet()->getStyle('B2:L2') ->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $n = 0; foreach ($AppointmentDataAll as $key=>$ticket) //转换性别 $objectPHPExcel->getActiveSheet()->setCellValue('A'.($n+3) ,$ticket['userCompany']); ...... //设置单元格为text属性 $objectPHPExcel->getActiveSheet()->setCellValueExplicit('F'.($n+3),$ticket['u_card'],PHPExcel_Cell_DataType::TYPE_STRING); $objectPHPExcel->getActiveSheet()->getStyle('F'.($n+3))->getNumberFormat()->setFormatCode("@"); $objectPHPExcel->getActiveSheet()->setCellValue('G'.($n+3) ,$ticket['userWedlockDict']); $objectPHPExcel->getActiveSheet()->setCellValue('H'.($n+3) ,$ticket['centerName']); //公司名称 $objectPHPExcel->getActiveSheet()->setCellValue('M'.($n+3) ,$ticket['appointment_status']); $n++; //设置颜色 $objectPHPExcel->getActiveSheet()->getStyle('A1:L1')->getFill() ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('FF66CCCC'); $objWriter = new PHPExcel_Writer_Excel2007($objectPHPExcel); $objectPHPExcel->getActiveSheet()->setTitle('订单汇总表'); ob_end_clean();//清除缓冲区,避免乱码 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="订单汇总表(' . date('Ymd-His') . ').xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objectPHPExcel, 'Excel5'); //避免乱码 $objWriter->save('php://output');
我跟你一模一样的问题,我注释ob_clean();ob_end_clean();都没用啊。
参考技术B 代码没看,但想说一定是你的路径设置有问题,linux严格区分url的大小写追问我找到原因了
ob_end_clean();
是这个函数导致的问题
这个函数作用是清除缓冲区,避免乱码
但是在linux下就会导致Excel导出不成功
把这代码注释就好了
我也不知道什么原因
soga
本回答被提问者和网友采纳PHP 导出Excel表格
在实际开发中,有很多场景是需要导出Excel表格的,比如后台的统计数据,客户希望能导出为表格的形式,今天介绍一下TP5框架内实现Excel表格的导出功能,首先下载phpexcel.zip,解压后放到vendor第三方类库目录下,直接用函数vendor()引入就可以啦,直接贴代码:
public function pushExcelAll(Request $request) { vendor(\'phpexcel.PHPExcel\'); //引入核心库文件 vendor(\'phpexcel.PHPExcel.Writer.Excel2007\'); //引入excel2007操作类 vendor(\'phpexcel.PHPExcel.IOFactory\'); $param = $request->param(); $id = $param[\'id\']; //要导出的数据id $name = isset($param[\'name\']) ? $param[\'name\'] : \'ExampleMemb\'; //要导出的Excel表名 $data = MeetMemb::query("select platform,ticketType,avatar,realName,phone,company,job,email,ticketRemark,seatCode,id from h_meetingmembs where id = $id"); //从数据库中查到数据 error_reporting(E_ALL); $objPHPExcel = new \\PHPExcel(); $letter = array(\'A\', \'B\', \'C\', \'D\', \'E\', \'F\', \'G\', \'H\', \'I\', \'J\', \'K\'); //表头数组 $tableheader = array(\'购票平台\', \'票类\', \'头像\', \'姓名\', \'手机号码\', \'单位名称\', \'职位\', \'邮箱\', \'备注\', \'坐席\', \'报名号\'); //填充表头信息 for ($i = 0; $i < count($tableheader); $i++) { $objPHPExcel->getActiveSheet()->setCellValue("$letter[$i]1", "$tableheader[$i]"); } /*以下就是对处理Excel里的数据,横着取数据,主要是这一步,其他基本都不要改*/ foreach ($data as $k => $v) { $num = $k + 1 + 1; $objPHPExcel->setActiveSheetIndex(0) ->setCellValue(\'A\' . $num, $v[\'platform\'])//platform ->setCellValue(\'B\' . $num, $v[\'ticketType\'])//ticketType ->setCellValue(\'C\' . $num, $v[\'avatar\'])//avatar ->setCellValue(\'D\' . $num, $v[\'realName\'])//realName ->setCellValue(\'E\' . $num, $v[\'phone\'])//phone ->setCellValue(\'F\' . $num, $v[\'company\'])//company ->setCellValue(\'G\' . $num, $v[\'job\'])//job ->setCellValue(\'H\' . $num, $v[\'email\'])//email ->setCellValue(\'I\' . $num, $v[\'ticketRemark\'])//ticketRemark ->setCellValue(\'J\' . $num, $v[\'seatCode\'])//seatCode ->setCellValue(\'K\' . $num, $v[\'id\']);//id } $objPHPExcel->getActiveSheet()->setTitle(\'signMemb\'); $objPHPExcel->setActiveSheetIndex(0); header(\'Content-Type: application/vnd.ms-excel;charset=UTF-8"\'); header(\'Content-Disposition: attachment;filename="\' . $name . \'.xls"\'); header(\'Cache-Control: max-age=0\'); $objWriter = \\PHPExcel_IOFactory::createWriter($objPHPExcel, \'Excel5\'); $objWriter->save(\'php://output\'); exit; }
相对应的修改几个值,$id,$name,$data,$letter,$tableheader和foreach循环中的$v的属性名,不出意外的话到此你就已经搞定了导出功能。
以上是关于PhpExcel类,在windows系统下能导出Excel文件,但是在linux下就不行,怎么回事?的主要内容,如果未能解决你的问题,请参考以下文章
求助,使用PHPExcel类读取Excel2007,出现错误