简体   繁体   中英

PHP output Issues with utf-8 characters

Please I have a problem in my PHP code, I try to convert all sheet of an excel document to CSV, Knowing that document includes french characters like "é, è, à ç", after executing the PHP code I obtain several CSV documents but with other charaters instead of french like "élé, é..).

I use xampp (Apache) as web server an I changed several parameters like "default_charset = "UTF-8, AddDefaultCharset UTF-8..".

There is my code:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="utf-8">
<?php
    header('content-type: text/html; charset: utf-8');
require_once 'Classes\PHPExcel\IOFactory.php';
$inFile = 'parc.xlsx';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($inFile);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');    

$index = 0;
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {

    $objPHPExcel->setActiveSheetIndex($index);

    $outFile = str_replace(array("-"," "), "_", $worksheet->getTitle()) .".csv";

    $objWriter->setSheetIndex($index);
    $objWriter->save($outFile);

    $index++;
}
?>

Thanks,

You should check the character encoding of the xlsx file. If the file was created on windows then it may have the Windows-1252 (CP1252) character encoding. If so then it needs to be converted to UTF-8. See the documentation on how to handle the character encoding of excel files. Following should be useful:

https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/10-Reading-and-Writing.md#reading-a-csv-file and

https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/10-Reading-and-Writing.md#writing-utf-8-csv-files

https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/10-Reading-and-Writing.md#writing-utf-8-html-files

Also see this related issue: How can I output a UTF-8 CSV in PHP that Excel will read properly?

The problem, unfortunately, is that there is no reliable way of encoding Unicode characters in a CSV. Using UTF-8 should work with most software, but notably does not work when you open that CSV with Microsoft Excel, which just assumes the default encoding (eg. CP-1252) and all your non-ascii characters will look like garbage.

Now, you can get it to interpret your UTF-8 correctly by putting the Unicode Byte Order Mark at the start of the document, which in UTF-8 is expressed as "\\xEF\\xBB\\xBF" .

However, I find that if you then modify and save the file as CSV again in Excel, this is stripped and subsequent attempts to open the file result in garbage.

An alternative solution is to covert to Latin-1 using utf8_decode (or to CP-1252, using one of the multi-byte encoding extensions/libraries). But this will strip out most Unicode characters apart from a selection of European ones.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM