php导出到excel使用utf8。

时间:2022-09-26 08:47:01

I have a piece of php code that exports data from mysql to excel. even using utf-8 for charset, Persian(arabic) are saved weird and they are not recognizable.

我有一段php代码,它将数据从mysql导出到excel。即使使用utf-8作为字符集,波斯语(阿拉伯语)也被保存得很奇怪,而且无法识别。

If you can help me I will be grateful.

如果你能帮助我,我会很感激。

<?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 个解决方案

#1


1  

Php native support for this is not that good.

Php本机对此的支持不是很好。

Have you tried php-gd-farsi?

你有试过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);

#1


1  

Php native support for this is not that good.

Php本机对此的支持不是很好。

Have you tried php-gd-farsi?

你有试过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);