导出到Excel时出现奇怪的字符

时间:2021-07-10 00:26:11

Hi im using the next code on php to export to excel. actually it works great.. but i always get strange characters like ? insted of áéíóú etc. (UTF8)

嗨我使用PHP上的下一个代码导出到Excel。实际上它工作得很好..但我总是喜欢奇怪的人物? éíóú等等(UTF8)

I found that adding..

我发现添加..

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

Will fix my problem..

会解决我的问题..

But i cant manage to put it here, i always get 500 Server Error.

但我不能设法把它放在这里,我总是得到500服务器错误。

here is my code

这是我的代码

//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>";
}

2 个解决方案

#1


2  

Ok i found the answer..

好的,我找到了答案..

need to apply mb_convert_encoding

需要应用mb_convert_encoding

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

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

and for the headers i used

以及我使用的标题

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);

#2


0  

just put it before Excel::create

把它放在Excel :: create之前

ob_clean();

ob_clean();

#1


2  

Ok i found the answer..

好的,我找到了答案..

need to apply mb_convert_encoding

需要应用mb_convert_encoding

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

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

and for the headers i used

以及我使用的标题

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);

#2


0  

just put it before Excel::create

把它放在Excel :: create之前

ob_clean();

ob_clean();