2013-04-13 23 views
1

嗨即時通訊使用PHP上的下一個代碼導出爲Excel。其實它很好..但我總是得到奇怪的人物?的insted的AEIOU等(UTF8)導出爲Excel時出現奇怪的字符

我發現加入..

Response.Charset = "UTF-8"; 
Response.ContentEncoding = System.Text.Encoding.Default; 

將解決我的問題..

,但我不能設法把它放在這裏,我總是得到500服務器錯誤。

這裏是我的代碼

//insertamos los headers que van a generar el archivo excel 
header('Content-type: application/vnd.ms-excel'); 
//en filename vamos a colocar el nombre con el que el archivo xls sera generado 
header("Content-Disposition: attachment; filename=ventas.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 

//hacemos la conexion al servidor MySql 
$dbhost       = "xxx"; 
$dbuser       = "xxx"; 
$dbpass       = "xxx"; 
$dbname       = "xxx"; 

$conexio = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to database"); 
mysql_select_db($dbname); 
//realizamos la consulta 
$tableName="usuarios"; 
$sql = mysql_query("SELECT id,name,lastname,email,codigo, media, phone, Pcode, birth FROM $tableName",$conexio); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Reporte de ventas</title> 
</head> 

<body><!–Vamos a crear una tabla que será impresa en el archivo excel –> 

<table width="600" border="0"> 
<tr> 
<th width="600"> 

<!-–Imprimimos un titulo -–> 

<div style="color:#003; text-align:center; text-shadow:#666;"><font size="+2">Reporte de Ventas <br />Usuarios</font></div></th> 
</tr> 
</table> 

<!–-creamos la tabla de el reporte con border 1 y los títulos-–> 

<table width="641" border="1"> 
<tr> 
<th width="50%" style="background-color:#006; text-align:center; color:#FFF"><strong>Ventas</strong></th> 
<th width="50%" style="background-color:#006; text-align:center; color:#FFF"><strong>Fecha</strong></th> 
</tr> 
<?php 
// Un proceso repetitivo para imprimir cada uno de los registros. 
while($row = mysql_fetch_array($sql)){ 
echo " 
<tr> 
<td bgcolor=\"#ededed\" align=\"center\">{$row['name']}</td> 
<td bgcolor=\"#ededed\" align=\"center\">{$row['email']}</td> 
</tr>"; 
} 
+0

在哪裏使用'Response.Charset'?我認爲這是爲ASP.NET – Amir

+0

確定...但我怎樣才能修復我的PHP頭,所以我的Excel文件使用正確的字符 –

回答

2

好吧,我找到了答案..

需要申請mb_convert_encoding

while($row = mysql_fetch_array($sql)){ 

    foreach($row as &$value) 
    { 
    $value = mb_convert_encoding($value, "UTF-8", "Windows-1252"); 
    } 

和頭,我用

header("Content-Type: application/vnd.ms-excel; charset=utf-8"); 
header("Content-type: application/x-msexcel; charset=utf-8"); 
header("Content-Disposition: attachment; filename=ventas.xls"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: private",false); 
0

只是把它放在前面Excel :: create

ob_clean();

相關問題