1

嗨,我正在使用 php 上的下一个代码导出到 excel。实际上它很好用..但我总是得到奇怪的字符,比如?插入 áéíóú 等 (UTF8)

我发现添加..

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 –&gt;

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

<!-–Imprimimos un titulo -–&gt;

<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-–&gt;

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

2 回答 2

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);
于 2013-04-13T06:39:23.597 回答
0

把它放在 Excel::create 之前

ob_clean();

于 2017-06-30T01:15:17.400 回答