1

Using Symfony2.3.4 and PHP5.6.3 and PHPExcel1.8.0

Is there a way to workaround this?

See, when I call

$objPHPExcel->getProperties()
            ->setCreator($creator)
            ->setLastModifiedBy($creator)
            ->setTitle($name);

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 
        $ext == 'xls' ? 'Excel5' : 'Excel2007');

$objWriter->save('downloads/' . $name . '.' . $ext);

The name of the file has to be in Spanish, therefore I need to use characters like á, é, í, etc.

Both the title and the name end up getting those type of characters replaced with

í- (for í) , ó (for ó), etc.

thanks

Scaramouche
  • 3,188
  • 2
  • 20
  • 46
  • As has already been pointed out, PHPExcel expects UTF-8 for all string values, whether string values in cells, or sheet names – Mark Baker Aug 12 '15 at 23:59

2 Answers2

2

This is a classic UTF-8 issue. For my explanation, I will use the ó character, or LATIN SMALL LETTER O WITH ACUTE, which has a unicode codepoint of U+00F3. When encoded to UTF-8, it takes two bytes: 0xC3 and 0xB3.

So what do we see if we look up characters with codepoints of those two bytes?

https://codepoints.net/U+00C3 (Ã - LATIN CAPITAL LETTER A WITH TILDE) https://codepoints.net/U+00B3 (³ - SUPERSCRIPT THREE)

As far as I know, PHPExcel handles UTF-8 properly. What is likely the problem here is the operating system doesn't support UTF-8. Windows, for example, uses UTF-16 to store filenames. Unix stores them as BLOBs and individual programs determine what encoding to use when decoding filenames.

So, I don't have a clear answer for you as to what's wrong, I just know how to diagnose this specific symptom. But hopefully this gets you on the right track.

Community
  • 1
  • 1
Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
  • I'm in windows8, now, there has to be a way to handle the string **$name** before *giving* it to phpexcel, a way to change the character orrrrr..., well I'm rambling here, I up voted since ur answer was very interesting but u'll understand this doesn't get the green tick just yet, thanks any way, I'll keep looking – Scaramouche Aug 12 '15 at 18:54
1

simple:

$name = $request->get('name');
$cur_encoding = mb_detect_encoding($name);
$is_encoding_ok = mb_check_encoding($name, "UTF-8");
if ($cur_encoding == "UTF-8" && $is_encoding_ok)
    $name = utf8_decode($name);

$objPHPExcel->getProperties()
            ->setCreator($creator)
            ->setLastModifiedBy($creator)
            ->setTitle($name);
$objWriter->save('downloads/' . $name . '.' . $ext);

thanks

Scaramouche
  • 3,188
  • 2
  • 20
  • 46