2010-08-12 08:05


include_once 'mysqlconn.php';
include_once "functions.php";
$filename = $_GET['par'].".xls";
header("Content-type: application/x-msexcel"); 
header('Content-Disposition: attachment; filename="'.basename($filename).'"'); 
if ($_GET['i'] == "par1") {
} else if ($_GET['i'] == "par2") {
  echo "şşşıııİİİ";
} else if ($_GET['i'] == "par3") {  
  echo "şşşıııİİİ";

this is my export2excel.php file and func1,2,3 are in functions.php file and produces table output all work well except character encoding in a strange way. I am using utf-8 encoding for all my files. 2nd else if statement above produces healthy encoded output but rest 2 are encodes my output with strange characters like "BÃœTÇE İÇİ". it is "BÜTÇE İÇİ" in turkish.

in short. same files, same encoding, same database but different results.

any idea?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dongliqin6939 dongliqin6939 11年前

    Excel uses UTF-16LE + BOM as default Unicode encoding.
    So you have to convert your output to UTF-16LE and prepend the UTF-16LE-BOM "\xFF\xFE".

    Some further information:

    Instead I would use one of the existing libraries

    Some code that could help if you really not want to use an existing library

    $output = <<<EOT
            <td>Перевод русского текста в транслит</td>
    // Convert to UTF-16LE
    $output = mb_convert_encoding($output, 'UTF-16LE', 'UTF-8'); 
    // Prepend BOM
    $output = "\xFF\xFE" . $output;
    header('Pragma: public');
    header("Content-type: application/x-msexcel"); 
    header('Content-Disposition: attachment;  filename="utf8_bom.xls"');
    echo $output;
    点赞 评论 复制链接分享
  • dsb0003795 dsb0003795 11年前

    Excel uses UTF-16LE as the default encoding. So you should either convert UTF-8 to UTF-16LE yourself or use one of the tried and tested Excel PHP libs instead of trying to reinvent the wheel. I would recommend using PHPExcel...

    点赞 评论 复制链接分享
  • doumu5662 doumu5662 8年前

    if anyone is trying to use the excel_writer in moodle and is getting encoding issues with output - say if you're developing a report that has a url as data in a field - then in this instance to simply fix this issue I wrapped the data in quotes so it at least opened up in excel here's my example:

    // Moodles using the PEAR excel_writer export
    $ex=new table_excel_export_format($table);
    $ex->start_document( {string} );
    $ex->start_table( {string} );
    // heading on the spreadsheet
    $title = array('Report Title'=>'Report 1');
    // end heading
    $ex->output_headers( array_keys($table->columns) );
    **foreach($data as $row){
            $ex->add_data( $table->get_row_from_keyed($row) );
    点赞 评论 复制链接分享