Excel上的字符编码从PHP导出
更新:我已经改变了代码,以便插入物料清单,没有正确的结果。 这是最新的一段代码:
$output = fopen($file, 'w'); $utf8_output_with_bom = chr(239) . chr(187) . chr(191) . $output; fputcsv($output, array('NOMBRE','APELLIDO','TELEFONO', 'RESPUESTA', 'DIA', 'HORA'), "\t"); if ($key1!="No definida"){ // fetch the data $rows1 = mysqli_query($con, "SELECT cm_name, cm_surname, cr_phone, cr_message, DATE_FORMAT(cr_time,'%d/%m/%Y'), DATE_FORMAT(cr_time,'%H:%i') FROM ws_campreplies JOIN ws_campmsg WHERE cr_phone=cm_phone AND cr_fk_us_id='$id' AND cr_message='$key1' AND cm_fk_ca_id='$ca_id'"); // loop over the rows, outputting them while ($row1 = mysqli_fetch_assoc($rows1)) { fputcsv($output, $row1, "\t"); } }
我有一个utf8_general_ci数据库,这两个相关的表(所有utf8_general_ci)logging的这个例子:
表ws_campmsg:
表ws_campreplies:
我唯一需要的是在Excel上导出这个,这几乎完成了。 这个PHP文件创build一个SQL并导出一个正确的XLS文件:
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME); mysqli_set_charset($con, 'utf8'); [....] $ca_id = $_POST['ca_id']; $key1 = $_POST['key1']; $file='../uploads/TemporalCsv_'.time().'.xls'; header("Content-Type: application/xls"); header("Content-Disposition: attachment; filename=$file"); header("Pragma: no-cache"); header("Expires: 0"); $output = fopen($file, 'w'); fputcsv($output, array('NOMBRE','APELLIDO','TELEFONO', 'RESPUESTA', 'DIA', 'HORA'), "\t"); if ($key1!="No definida"){ // fetch the data $rows1 = mysqli_query($con, "SELECT cm_name, cm_surname, cr_phone, cr_message, DATE_FORMAT(cr_time,'%d/%m/%Y'), DATE_FORMAT(cr_time,'%H:%i') FROM ws_campreplies JOIN ws_campmsg WHERE cr_phone=cm_phone AND cr_fk_us_id='$id' AND cr_message='$key1' AND cm_fk_ca_id='$ca_id'"); // loop over the rows, outputting them while ($row1 = mysqli_fetch_assoc($rows1)) { fputcsv($output, $row1, "\t"); } } fclose($output); echo $file;
如果我传递ca_id =“438” , key1 =“CLAVE7” ,导出的文件就是这样的:
正如你现在看到的唯一的问题是,它不显示正确的口音像á或Á 。 正如我已经search了很多关于解决这个问题,有一些尝试,我没有正面的结果,如试图导出文本格式dfirectly在浏览器(导出相同的编码错误):
$file='../uploads/TemporalCsv_'.time().'.txt'; header("Content-Type: text/tab-separated-values");
还尝试更改标题(导出相同的编码错误)
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);
在最后一次尝试中,我发现可能使用iconv()
进行转换将会很有效,但是通过这种修改会出现错误,因为html_entity_decode()
接受string,而不是数组:
if ($key1!="No definida"){ // fetch the data $rows1 = mysqli_query($con, "SELECT cm_name, cm_surname, cr_phone, cr_message, DATE_FORMAT(cr_time,'%d/%m/%Y'), DATE_FORMAT(cr_time,'%H:%i') FROM ws_campreplies JOIN ws_campmsg WHERE cr_phone=cm_phone AND cr_fk_us_id='$id' AND cr_message='$key1' AND cm_fk_ca_id='$ca_id'"); // loop over the rows, outputting them while ($row1 = mysqli_fetch_assoc($rows1)) { iconv("UTF-8","WINDOWS-1255",html_entity_decode( $row1 ,ENT_COMPAT,'utf-8')); fputcsv($output, $row1, "\t"); } }
我希望我自己解释清楚。 PHPExcel的使用是我最后的机会,如果有任何可能性纠正这个问题,而不使用PHPExcel,我更喜欢它。 非常感谢。
您必须在输出开始处添加BOM 。
$utf8_output_with_bom = chr(239) . chr(187) . chr(191) . $utf8_output;
UTF8中不需要BOM。
例:
$output = fopen($file, 'w'); fwrite($output, chr(239) . chr(187) . chr(191)); // Write the BOM to the beginning of the file fputcsv($output, <...>); fclose($output);
编写CSV的代码:
$output = fopen($file, 'w'); fwrite($output, chr(239) . chr(187) . chr(191)); fputcsv($output, array('NOMBRE', 'APELLIDO', 'TELEFONO', 'RESPUESTA', 'DIA', 'HORA'), "\t"); if ($key1 != "No definida") { // fetch the data $rows1 = mysqli_query($con, "SELECT cm_name, cm_surname, cr_phone, cr_message, DATE_FORMAT(cr_time,'%d/%m/%Y'), DATE_FORMAT(cr_time,'%H:%i') FROM ws_campreplies JOIN ws_campmsg WHERE cr_phone=cm_phone AND cr_fk_us_id='$id' AND cr_message='$key1' AND cm_fk_ca_id='$ca_id'"); // loop over the rows, outputting them while ($row1 = mysqli_fetch_assoc($rows1)) { fputcsv($output, $row1, "\t"); } } fclose($output);