php使用utf8导出到excel

时间:2016-02-02 13:57:27

标签: php mysql excel utf-8 persian

我有一段PHP代码,可以将数据从mysql导出到excel。甚至使用 字符串的utf-8,波斯语(阿拉伯语)保存得很奇怪,而且它们无法识别。

如果你能帮助我,我将不胜感激。

<?php

$con=mysqli_connect("localhost","root","","myDB");
$SQL = mysqli_query($con,"SELECT * FROM view1");

$header = '';
$result ='';
$exportData = mysql_query ($SQL ) or die ( "Sql error : " . mysql_error( ) );

$fields = mysql_num_fields ( $exportData );

for ( $i = 0; $i < $fields; $i++ )
{
    $header .= mysql_field_name( $exportData , $i ) . "\t";
}

while( $row = mysql_fetch_row( $exportData ) )
{
    $line = '';
    foreach( $row as $value )
    {
        if ( ( !isset( $value ) ) || ( $value == "" ) )
        {
            $value = "\t";
        }
        else
        {
            $value = str_replace( '"' , '""' , $value );
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $result .= trim( $line ) . "\n";
}
$result = str_replace( "\r" , "" , $result );

if ( $result == "" )
{
    $result = "\nNo Record(s) Found!\n";
}

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=export.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$result";

?>

1 个答案:

答案 0 :(得分:1)

Php原生支持对此并不是那么好。

您是否尝试过php-gd-farsi


    include('php-gd-farsi-master/FarsiGD.php');

    $gd = new FarsiGD();

    // Create a 300x100 image
    $im = imagecreatetruecolor(300, 100);
    $red = imagecolorallocate($im, 0xFF, 0x00, 0x00);
    $black = imagecolorallocate($im, 0x00, 0x00, 0x00);

    // Make the background red
    imagefilledrectangle($im, 0, 0, 299, 99, $red);

    // Path to our ttf font file
    //$font_file = './Vera.ttf';
    $font_file = './cour.ttf';

    // Draw the text 'PHP Manual' using font size 13
    $text = imagecreatetruecolor(200, 60);
    imagefilledrectangle($text, 0, 0, 200, 60, $red);
    $str = '**ماه**';
    $tx = $gd->persianText($str, 'fa', 'normal');
    imagefttext($text, 24, 10, 10, 50, $black, $font_file,$tx );
    $im = $text;

    // Output image to the browser
    header('Content-Type: image/png');

    imagepng($im);
    imagedestroy($im);