我有这个代码,这基本上创建并保存了一个HTML表,然后使用PHPexcel我试图转换,但是XLSX完全错误且HTML没问题,这是我第一次使用PHPExcel,我不知道如果我做错了什么。
<?php
session_start();
include('includes/connect.php');
$query = $_SESSION['query'];
$qry = $mysqli->query($query);
//echo $mysqli->error;
$str_html = "
<body>
<table>
<tr>
<th>
Apellido
</th>
<th>
Nombre
</th>
<th>
E-Mail
</th>
<th>
Teléfono Celular
</th>
<th>
CV
</th>
<th>
Foto
</th>";
$query1 = "SELECT * FROM ind_exp ORDER BY nombre ASC";
$qry1 = $mysqli->query($query1);
while($f1 = $qry1->fetch_assoc())
{
$str_html = $str_html."
<th>
Experiencia en Industrias de: ".mb_convert_encoding($f1['nombre'],'utf-16','utf-8').
'</th>';
}
$query2 = "SELECT * FROM proy_exp ORDER BY nombre ASC";
$qry2 = $mysqli->query($query2);
while($f2 = $qry2->fetch_assoc())
{
$str_html = $str_html."<th>
Experiencia en Proyectos de: ".mb_convert_encoding($f2['nombre'],'utf-16','utf-8').
'</th>';
}
$query3 = "SELECT * FROM rol_exp ORDER BY nombre ASC";
$qry3 = $mysqli->query($query3);
while($f3 = $qry3->fetch_assoc())
{
$str_html = $str_html."<th>
Experiencia en Roles de: ".mb_convert_encoding($f3['nombre'],'utf-16','utf-8').
'</th>';
}
$str_html = $str_html."</tr>";
while($f = $qry->fetch_assoc())
{
$mail = $f['email'];
foreach($f as $key=>$value)
{
$f[$key] = mb_convert_encoding($value,'utf-16','utf-8');
}
$str_html = $str_html."
<tr>
<td>
".$f['apellido']."
</td>
<td>
".$f['nombre']."
</td>
<td>
".$f['email']."
</td>
<td>
".$f['cel']."
</td>
<td>
http://litup.cl/registro/".$f['cv']."
</td>
<td>
http://litup.cl/registro/".$f['foto']."
</td>";
$query1 = "SELECT * FROM ind_exp ORDER BY nombre ASC";
$qry1 = $mysqli->query($query1);
while($f1 = $qry1->fetch_assoc())
{
$str_html = $str_html."<td>";
$query3 = "SELECT * FROM rel WHERE id_reg = '$mail' AND tipo_rel = 'iec'";
$qry3 = $mysqli->query($query3);
//echo $qry3->num_rows;
//echo $query3;
while($f3 = $qry3->fetch_assoc())
{
//echo $f1['nombre'].' - '.$f3['nombre_ext'];
if($f1['nombre'] == $f3['nombre_ext'])
{
$aux = $f1['nombre'];
$cosa = mb_convert_encoding($aux,'utf-16','utf-8');
$str_html = $str_html." ".$cosa." ";
}
if($f3['otro']=='si' && $f1['nombre']== 'Otra Industria')
{
$aux = $f3['nombre_ext'];
$cosa = mb_convert_encoding($aux,'utf-16','utf-8');
$str_html = $str_html." ".$cosa." ";
}
}
//echo 'Industria de apoyo: '.$f1['nombre'];
$str_html = $str_html."</td>";
}
$query1 = "SELECT * FROM proy_exp ORDER BY nombre ASC";
$qry1 = $mysqli->query($query1);
while($f1 = $qry1->fetch_assoc())
{
$str_html = $str_html."</td>";
$query3 = "SELECT * FROM rel WHERE id_reg = '$mail' AND tipo_rel = 'pec'";
$qry3 = $mysqli->query($query3);
//echo $qry3->num_rows;
//echo $query3;
while($f3 = $qry3->fetch_assoc())
{
//echo $f1['nombre'].' - '.$f3['nombre_ext'];
if($f1['nombre'] == $f3['nombre_ext'])
{
$aux = $f1['nombre'];
$cosa = mb_convert_encoding($aux,'utf-16','utf-8');
$str_html = $str_html." ".$cosa." ";
}
if($f3['otro']=='si' && $f1['nombre']== 'Otro Proyecto')
{
$aux = $f3['nombre_ext'];
$cosa = mb_convert_encoding($aux,'utf-16','utf-8');
$str_html = $str_html." ".$cosa." ";
}
}
//echo 'Industria de apoyo: '.$f1['nombre'];
$str_html = $str_html."</td>";
}
$query1 = "SELECT * FROM rol_exp ORDER BY nombre ASC";
$qry1 = $mysqli->query($query1);
while($f1 = $qry1->fetch_assoc())
{
$str_html = $str_html."</td>";
$query3 = "SELECT * FROM rel WHERE id_reg = '$mail' AND tipo_rel = 'rec'";
$qry3 = $mysqli->query($query3);
//echo $qry3->num_rows;
//echo $query3;
while($f3 = $qry3->fetch_assoc())
{
//echo $f1['nombre'].' - '.$f3['nombre_ext'];
if($f1['nombre'] == $f3['nombre_ext'])
{
$aux = $f1['nombre'];
$cosa = mb_convert_encoding($aux,'utf-16','utf-8');
$str_html = $str_html." ".$cosa." ";
}
}
//echo 'Industria de apoyo: '.$f1['nombre'];
$str_html = $str_html."</td>";
}
$str_html = $str_html."</tr>";
}
$str_html = $str_html."</table></body>";
//echo $mysqli->error;
echo $str_html;
//Save File
$today = date('j-m-y');
file_put_contents('./cxls/consultores'.$today.'.html', $str_html);
/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
$objReader = PHPExcel_IOFactory::createReader('HTML');
$objPHPExcel = $objReader->load('./cxls/consultores'.$today.'.html');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('./cxls/consultores'.$today.'.xlsx');
?>
希望你能帮帮我。
答案 0 :(得分:0)
正如Mark在评论中所述,我的问题是UTF-16,我完全删除了mb_convert_encoding($string,'utf-16','utf-8');
,一切正常。