phpexcel 导出不好使的问题

Unable to export UTF-8 characters to Excel5 XLS file

Topics: Developer Forum, User Forum
Dec 10, 2010 at 2:24 AM

Hi,

I have a strange problem.

If I unzip the PHPExcel soucre file named 1.7.4.zip on my computer (Windows XP) and I run the Tests/01simple-download-xls.php it works fine. However, as soon as  modify this file, it stop working (I mean I still can download the file but Excel do not regognize it. I tried with 2 different versions of MS Excel). When I modify the file, I save it in UTF-8 format (using Windows Notepad) since the file seams already in UTF-8 format.

If I save the file in ANSI format, it will start back to work except that I do not see miscellaneous glyphs characters ('éàèùâêîôûëïüÿäöüç') anymore in the output Excel file.

Is somebody could help me since I need to export UTF-8 characters to an Excel5 XLS file and for the moment, nothing seams to work... :-(

Thanks in advance anyway.

Guylain Plante

 

 

 

Dec 10, 2010 at 2:35 PM

OK, I finally found a work arround. I put the following 2 lines of code before redirecting output to the client browser and it looks that it fixed the problem. Probably that strange characters were sent to the browser. So, calling ob_end_clean() seams to clean the output buffer before sending the excel document. ob_start() is also important to reopen the output buffer.

...

ob_end_clean(); // Added by me
ob_start(); // Added by me

// Redirect output to a clients web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
...

 

Dec 10, 2010 at 5:47 PM

Finally, this work arround seams to work only when the web server is on Linux.

I still have the problem under Windows...

 

 

Coordinator
Dec 10, 2010 at 6:54 PM

When you save the file, ensure that you save it as UTF-8 without a BOM marker. Your modifications are failing because PHP will treat the BOM marker as output, which will then trigger an error when your script tries to send the headers, and this error display gets included in the Excel file that is generated. Using output buffering is preventing this output from occuring before the headers, so there won't be any error, but the BOM marker itself will still be included in the Excel file that is generated.

Dec 10, 2010 at 7:37 PM

Hi Mark,

Thank you for your answer. I better understand now.

1) I will try that. Do I have to use a special editor to be able to save my php file without BOM information since my Windows NotePad editor does not seam to have that option.

2) Just for your information I edited the corrupted excel file and I saw 6 BOM back to back. Here is the beginning of the file:

EF BB BF EF BB BFEF BB BFEF BB BFEF BB BFEF BB BF D0 CF 11

After I removed the red part using an Hex File Editor, my excel fine worked just fine. So, I hope that saving my php file without BOM will correct the problem. I will let you know.

Thanks again.

 

Coordinator
Dec 10, 2010 at 8:43 PM

If you're on Windows, then WordPad should allow you to save a file as UTF-8 without a BOM. Alternatively, most PHP IDEs will offer this option as well.

Dec 10, 2010 at 8:46 PM

Ok, I downloaded the editor Notepad++ and saved the file without BOM but unfortunately, I still had several BOM at the beginning of the excel file. So, to fix it, now I'm removing BOM directly into PHP and it seams to work just fine. Here is my code:

function removeAllBOM($str="")
{
 $tmpstr = $str; 
 while ((substr($tmpstr, 0,3) == pack("CCC",0xef,0xbb,0xbf)))
 {
  $tmpstr=substr($tmpstr, 3);
 } 
 return $tmpstr;
}

...

$objWriter->save('php://output');
$out1 = ob_get_contents();
$out1 = removeAllBOM($out1);
ob_end_clean();
ob_start();
echo $out1;

...

That time I got something that seam to work each time.

Thanks for you help.

 

 

Coordinator
Dec 10, 2010 at 8:53 PM

Check any other files that you might be including to see if they contain a BOM or not

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值