PHP with UTF8 output into PHPExcel?

14,196

Setting your executing "SET NAMES utf8" or mysql_set_charset("UTF8", $conn); to ensure that your connection to the database is using UTF-8.

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

Make sure that you're not trying to call utf8_encode() anymore either.

Note: An Excel file uses a codepage to identify the character set being used within the file, but PHPExcel sets that codepage to a hard-coded value of UTF-8 internally, so all string content in the file should be UTF-8, and Excel knows to interpret the worksheets in the MS Excel GUI as UTF-8 as well. If you're not seeing chinese characters when you look at the spreadsheet in Excel itself, then check the language settings in MS Excel to see if it's configured to handle chinese

Credit: @Mark Baker. Thanks again.

Share:
14,196
Aaron Nguyen
Author by

Aaron Nguyen

Updated on June 04, 2022

Comments

  • Aaron Nguyen
    Aaron Nguyen almost 2 years

    I'm having an issue with exporting data from a utf8 mysql database into an excel sheet with PHPExcel and retaining chinese characters. My mysql db is utf8 and has numerous chinese characters in it, and I'm exporting that data into a multi-sheet xls file (excel5) and every export the chinese characters turn into "?". I've tried utf8_encode() but it doesn't work for me. I also tried changing the output to excel2007 thinking it would be an excel5 writer issue.

    Is there a way to get Chinese characters to export correctly? Do I have to make the entire php file utf8? and if so how would I go about doing so?

    Here is the portion I'm having issue with:

    $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 those chinese characters as it is a multi-lingual catalog db so changing it to english wouldn't help. Also I am currently on a Mac with Xampp if that info is helpful in anyway.