I have a huge query with a lot of relationships, joins and DB functions involved. Here an extract example of the query:
SELECT * FROM (
SELECT DISTINCT
CASE WHEN objeto.objeto_clave IS NULL THEN objeto.objeto_numero ELSE objeto.objeto_clave END AS "Número",
CASE WHEN objeto.objeto_estatus = 'V' THEN 'Válido' ELSE 'No válido' END AS "Válido"
FROM objeto
WHERE objeto.objeto_id IN (1, 2, 3)
GROUP BY objeto.objeto_id, objeto.objeto_numero, objeto.objeto_clave, objeto.objeto_estatus
) AS "Excel" ORDER BY (CASE WHEN "Número" IS NULL THEN substring("Número",3) ELSE substring("Número",6) END) ASC
Im using cakephp 2.x to generate an excel report through a method which allow you to download an excel file taking data from the database connection (db_name, user, etc...), the sql query and doing a system command to create the file filled with the data given by the query result using a -H option for HTML tabular output and -c to excecute the sql as a command:
$command = <<<CMMD
psql -h $host -p $port -d $db -U $login -H -c "$sql" > $file
CMMD;
system($command);
//Cleaning content
if (is_file($file)) {
$content = file_get_contents($file);
$content = substr($content, 0, strpos($content, '<p>'));
$content = utf8_decode($content);
file_put_contents($file, $content);
}
Everything works fine in Windows platform. The problem comes when you download the file using a Mac, the file is created but when you open it, column names and data in cells with accents like: "Número" and "Válido" are converted into "N_mero" and "V_lido".
I would like to know where the problem is related (Code, [Mac Excel] special encoding, something with utf8_decode function, another thing ?) and how can i handle it.
It doesn't show any problem if I use openOffice in the same Mac.