PHP:将MySQL数据导出到Excel文件时出现问题

时间:2017-11-09 18:53:08

标签: php mysql

我将MySQL数据导出到Excel文件中,该文件包含“印地语”数据,但代替印地语निमà¥à¤¨正在出现。

请帮助解决我的问题。我的PHP代码是:

<?php
    include 'connection.php';
    $filename = "currentaffairquestions";  //your_file_name
    $file_ending = "xls";   //file_extention
    mysqli_set_charset($conn,'utf8');
    header("Content-Type: application/vnd-ms-excel");    
    header("Content-Disposition: attachment; filename=$filename.xls");  
    header("Pragma: no-cache"); 
    header("Expires: 0");
    $sep = "\t";
    $sql="SELECT  question_number,question_name,choice1,choice2,choice3,choice4,answer from current_affairs_question_details where id='$_GET[currentaffairid]'"; 
    $result=mysqli_query($conn,$sql);
    while ($property = mysqli_fetch_field($result)) { //fetch table field name
        echo $property->name."\t";
    }

    print("\n");    

    while($row = mysqli_fetch_row($result))  //fetch_table_data
    {
        $schema_insert = "";
        for($j=0; $j< mysqli_num_fields($result);$j++)
        {
            if(!isset($row[$j]))
                $schema_insert .= "NULL".$sep;
            elseif ($row[$j] != "")
                $schema_insert .= "$row[$j]".$sep;
            else
                $schema_insert .= "".$sep;
        }
        $schema_insert = str_replace($sep."$", "", $schema_insert);
        $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
        $schema_insert .= "\t";
        print(trim($schema_insert));
        print "\n";
    }
    ?>

2 个答案:

答案 0 :(得分:0)

当您输出数据时,需要将其传递给mb_convert_encoding

echo mb_convert_encoding($property->name, 'utf8');

答案 1 :(得分:0)

mysqli_set_charset($ CON, “UTF8”);连接后立即