Jump to content

[SOLVED] Special character problem when exporting a MySQL database to Excel


egillsigurdur

Recommended Posts

I've got this MySQL table that I'm trying to make people able to export to excel through PHP, but the characters are all messed up.  When I print the table out in HTML with encoding UTF-8 everything is okay, but when I put it into Excel ð turns to ð, ú becomes ú, ó becomes ó and so on.  They do look like this in phpMyAdmin, also when I export them there.

 

 

How can I fix characters in Excel export?  I have no knowledge at all of how .xls files are structured.

Link to comment
Share on other sites

You need to store the UTF-8 encoded values in Excel as well.

 

You don't say how you're exporting to Excel though, and there's no simple solution to to writing an Excel file - giving you details of the structure of an xls file wouldn't particularly help because it is a fairly complex binary format - unless you're using one of the libraries that have been developed for that specific purpose.

Link to comment
Share on other sites

I wasn't using a library, so I tried that now.  I'm now using PEAR’s ‘Excel Writer’, which is by the way sort of neat.

 

But still, when I do a loop that prints out info from the table, all special characters are wacky, exactly the way they look in phpMyAdmin.

 

Do you think I should do some loop that would alter all "ð"s and turn them into "ð"s?  Not completely how I'd do it though.

Link to comment
Share on other sites

I wasn't using a library, so I tried that now. I'm now using PEAR’s ‘Excel Writer’, which is by the way sort of neat.

It's OK, though it wouldn't be my personal recommendation; but any writer should simplify the task and handle all the complexities of Excel's binary format.

 

 

But still, when I do a loop that prints out info from the table, all special characters are wacky, exactly the way they look in phpMyAdmin.

If the database data isn't UTF-8, then, you need to ensure that the characters are converted from whatever charset you might be using in your database to UTF-8 before you populate the Excel cell.

$cellValue = iconv('ISO-8859-1,'UTF-8',$cellValue);

using ISO-8859-1 or whatever character set you're using for your table data.

I'm not sure how good the PEAR Excel Writer is for handling charsets.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.