Home >Backend Development >PHP Tutorial >phpExcel导出, 在本地正常,在服务器乱码解决办法

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

WBOY
WBOYOriginal
2016-06-13 12:25:441250browse

phpExcel导出, 在本地正常,在服务器乱码
如题,在服务器运行时....

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

<br />            $excel=new \PHPExcel();<br />            do{<br />               $url='http://www.哪个好人来答下.com';<br />               <br />                $content='';<br />                $content=file_get_contents($url); <br />                if($content['status']==0){  <br />                    $data=$content['results'];<br />                  <br />                        $head=array();<br />                        <br />                        //获取表头值<br />                        for($i=0;$i<10;$i++){<br />                        $tmp=array_keys($data[$i]);<br />                        foreach($tmp as $t){<br />                            if(!in_array($t,$head)){<br />                                array_push($head,$t);<br />                            }<br />                        }<br />                        }<br />                      <br />                        $A=65; //A的ASCII值<br />                        $col=array();<br />                        for($i=0;$i<count($head);$i++){   //制作列值<br />                            array_push($col,chr($A++));<br />                        }<br />                        for($i=0;$i<count($head);$i++){  //制作表头<br />                            $excel->getActiveSheet()->setCellValue($col[$i].'1',$head[$i]);<br />                        }<br />                    }         <br />                    foreach($data as $key=>$val){         <br />                        for($i=0;$i<count($head);$i++){<br /><br />                            $excel->getActiveSheet()->setCellValue($col[$i].$i,$row[$head[$i]]);<br />            <br />                        }<br />                    }<br />            <br />                }<br />                $page++;<br />            }while($pageCount>=$page);<br />            $excel->getActiveSheet()->setTitle($title);<br />           $excel->setActiveSheetIndex(0);<br />            <br />            $writer = \PHPExcel_IOFactory::createWriter($excel, 'Excel5');<br />            header('Content-Type: application/vnd.ms-excel');<br />            header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');<br />            header('Cache-Control: max-age=0');<br />          <br />            $writer->save('php://output');<br />            <br />        }<br />

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

打开任何一个 xls 文件都会看到那个文件标识是顶格的,但你的截图中不是这样
请检查是否有 BOM 头或其他输出
另外,那些 后边跟有空心方块的英文单词也是不应该有的,请检查相关的程序文件是否上传齐全了
------解决思路----------------------
header之前先清除缓存区的内容
<br />ob_end_clean();// 就是加这句<br /><br />header('Content-Type: application/vnd.ms-excel');<br />header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');<br />header('Cache-Control: max-age=0');<br />

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn