PHP with UTF8 output in PHPExcel?

I had a problem exporting data from mysql utf8 database to excel sheet with PHPExcel and saving Chinese characters. My mysql db is utf8 and contains a lot of Chinese characters in it, and I export this data to a multi-page xls file (excel5), and every export of Chinese characters turns into a "?". I tried utf8_encode () but this does not work for me. I also tried changing the output to excel2007, thinking that this would be a problem with excel5.

Is there a way to properly export Chinese characters? Should I make the whole utf8 php file? and if so, how can I do this?

Here is the part I come across:

$res2 = mysql_query("SHOW COLUMNS FROM ".$sheetnametemp); while($row2 = mysql_fetch_array($res2, MYSQL_NUM)) { $counter = 2; $cell = $coltemp; $cell .= $counter; $objPHPExcel->getActiveSheet()->SetCellValue($cell, $row2[0]); $result = mysql_query("SELECT * FROM ".$sheetnametemp); while($row = mysql_fetch_array($result)) { $counter++; $cell2 = $coltemp; $cell2 .= $counter; utf8_encode($row[$row2[0]]); echo "<br />"; $objPHPExcel->getActiveSheet()->SetCellValue($cell2,utf8_encode($row[$row2[0]])); } 

I need to use these Chinese characters as this is a multilingual db directory, so changing it to English would not help. I am also currently on a Mac with Xampp, if this information is anyway useful.

+4
source share
1 answer

Setting execution "SET NAMES utf8" or mysql_set_charset ("UTF8", $ conn); so that your database connection uses UTF-8.

You can test the connection using echo mysql_client_encoding ($ conn);

Make sure you are not trying to call utf8_encode () yet.

Note. An Excel file uses a code page to identify the character set used in a file, but PHPExcel sets this code page to a strictly encoded UTF-8 value internally, so all string contents in a file must be UTF-8, and Excel knows how to interpret worksheets in MS Excel GUI as UTF-8. If you do not see Chinese characters when viewing a spreadsheet in Excel itself, check the language settings in MS Excel to see if it is configured to handle Chinese

Credit: @Mark Baker . Thanks again.

+4
source

Source: https://habr.com/ru/post/1437682/


All Articles