从PHP

时间:2015-06-25 10:15:43

标签: php excel character-encoding

更新:我已经更改了代码以插入BOM,没有可能的结果。这是更新的代码段:

$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):

ws_campmsg:

ws_campmsg

ws_campreplies:

ws_campreplies

我唯一需要的是在Excel上导出它,这几乎已经完成了。此PHP文件创建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”,导出的文件就是这个:

xls

如您所见现在唯一的问题是它没有显示正确的重音,如á或Á。由于我已经搜索了很多关于解决此问题的内容,因此我尝试了一些没有可能结果的尝试,例如尝试在浏览器上直接导出文本格式(导出相同的编码错误):

$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()接受字符串,而不是数组:

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的情况下纠正这个问题,我宁愿这样做。非常感谢。

1 个答案:

答案 0 :(得分:2)

您必须在输出的开头添加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);