当前位置: 代码迷 >> PHP >> phpExcel导出, 在本地正常,在服务器乱码解决办法
  详细解决方案

phpExcel导出, 在本地正常,在服务器乱码解决办法

热度:37   发布时间:2016-04-28 16:56:06.0
phpExcel导出, 在本地正常,在服务器乱码
如题,在服务器运行时....

重复一下,在本地运行是正常的,放上服务就成这样了,代码如下。。。

            $excel=new \PHPExcel();
            do{
               $url='http://www.哪个好人来答下.com';
               
                $content='';
                $content=file_get_contents($url); 
                if($content['status']==0){  
                    $data=$content['results'];
                  
                        $head=array();
                        
                        //获取表头值
                        for($i=0;$i<10;$i++){
                        $tmp=array_keys($data[$i]);
                        foreach($tmp as $t){
                            if(!in_array($t,$head)){
                                array_push($head,$t);
                            }
                        }
                        }
                      
                        $A=65; //A的ASCII值
                        $col=array();
                        for($i=0;$i<count($head);$i++){   //制作列值
                            array_push($col,chr($A++));
                        }
                        for($i=0;$i<count($head);$i++){  //制作表头
                            $excel->getActiveSheet()->setCellValue($col[$i].'1',$head[$i]);
                        }
                    }         
                    foreach($data as $key=>$val){         
                        for($i=0;$i<count($head);$i++){

                            $excel->getActiveSheet()->setCellValue($col[$i].$i,$row[$head[$i]]);
            
                        }
                    }
            
                }
                $page++;
            }while($pageCount>=$page);
            $excel->getActiveSheet()->setTitle($title);
           $excel->setActiveSheetIndex(0);
            
            $writer = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');
            header('Cache-Control: max-age=0');
          
            $writer->save('php://output');
            
        }

------解决思路----------------------

打开任何一个 xls 文件都会看到那个文件标识是顶格的,但你的截图中不是这样
请检查是否有 BOM 头或其他输出
另外,那些 后边跟有空心方块的英文单词也是不应该有的,请检查相关的程序文件是否上传齐全了
------解决思路----------------------
header之前先清除缓存区的内容

ob_end_clean();// 就是加这句

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');
header('Cache-Control: max-age=0');

  相关解决方案