Jump to content

php query export to excel with wrong encoding


vfragk

Recommended Posts

im having problem as i  exporting results from query to excel.

from MYSQL database set at utf_general_si and encoding of php file at utf8

Data includes greek characters that dont look as they should.

 

Here is the code that i use

 

<?php

header('Content-type: text/html; charset=utf-8');

header("Content-Type: application/vnd.ms-excel; name='excel'");

header("Content-Type: application/octet-stream");

header("Content-Disposition: attachment; filename=E6R8.xls");

header("Pragma: no-cache");

header("Expires: 0");

 

$tmima = $_GET['tmima'];

$data = $_GET['data'];

$xronos = $_GET['years'];

 

$tmima=$_POST['tmima'];

$data=$_POST['data'];

$xronos=$_POST['years'];

 

include '../config.php';

include '../opendb.php';

 

// combine dynamic the query from user's selected fields

$query = "SELECT ΟΝΟΜΑ_ΤΜΗΜΑΤΟΣ,ΕΤΟΣ " ;

$data=$_POST['data'];

if ($data){

foreach ($data as $d){$query = $query . ", $d"; }

$query = $query . " FROM  tmimata1";

}

$query = $query . " WHERE (";

$tmima=$_POST['tmima'];

if ($tmima){

foreach ($tmima as $t){$query = $query . "ΟΝΟΜΑ_ΤΜΗΜΑΤΟΣ='$t' OR ";}

$query = substr_replace($query,"",-3);

}//afairei ta 3 last

$query = $query . " ) AND ( ";

if ($xronos){

foreach ($xronos as $x){$query = $query . "ΕΤΟΣ=$x OR ";}

$query = substr_replace($query,"",-3);

}

$query = $query . ") ";

 

$result = mysql_query($query);

$count = mysql_num_fields($result);

 

for ($i = 0; $i < $count; $i++){

$header .= mysql_field_name($result, $i)."\t";

}

while($row = mysql_fetch_row($result)){

$line = '';

foreach($row as $value){

if(!isset($value) || $value == ""){

$value = "\t";

}else{

$value = str_replace('"', '""', $value);

$value = '"' . $value . '"' . "\t";

}

$line .= $value;

}

$data2 .= trim($line)."\n";

}

 

$data2 = str_replace("\r", "", $data2);

if ($data2 == "") {

$data2 = "\nno matching records found\n";

}

echo $header."\n".$data2;

include '../closedb.php';

?>

 

 

i attached  a screen shot of excel file that is exporting with results headers and name witch are at greek are not shown as they should.

also i checked again the php.ini file is sets utf-8 encoding and the same character set is been at mysql (collation utf8_general_ci).

What is wrong when it goes to excel?

Because when i'm just echoing the results of the query with the following code is shown up fine at screen all my data.

 

 

<?php

.......

$result = mysql_query($query);

$count = mysql_num_rows($result3);

echo "ΣΥΝΟΛΟ ΕΓΓΡΑΦΩΝ : ".$count." <br>" ;

while($row3 = mysql_fetch_array($result, MYSQL_ASSOC))

  {

  if ($row3 ['ΟΝΟΜΑ_ΤΜΗΜΑΤΟΣ']!=="" ){echo "ΤΜΗΜΑ : ".$row3 ['ΟΝΟΜΑ_ΤΜΗΜΑΤΟΣ']." <br>" ;}

if ($row3 ['ΕΤΟΣ']!=="0" & $row3 ['ΕΤΟΣ']!=="" ){echo "ΕΤΟΣ  :".$row3 ['ΕΤΟΣ']." <br>" ;}

if ($row3 ['ΑΡ_Citations']!=="0" & $row3 ['ΑΡ_Citations']!==""){echo "ΑΡ_Citations :".$row3 ['ΑΡ_Citations']." <br>" ; }

......

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.