Php export เป น excel ม เง อนไข

I hope somebody here can help me I am getting the following warning in PHP when I export a report to excel. The moodle version is 1.9.9.

Warning: Cannot modify header information - headers already sent by (output started at /lib/weblib.php:2762) in /usr/www/users/lib/pear/Spreadsheet/Excel/Writer.php on line 67

On the other server runnning moodle 1.9.11 this report seems to run fine and the excel file is generated.Could it be that the PEAR HTML quickform package is outdated on the Moodle 1.9.9 server?

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?

Php export เป น excel ม เง อนไข

Kangkan

15.4k11 gold badges73 silver badges113 bronze badges

asked Aug 12, 2010 at 8:05

Php export เป น excel ม เง อนไข

1

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:

  • Microsoft Excel mangles Diacritics in .csv files?
  • Exporting data to CSV and Excel in your Rails apps

Instead I would use one of the existing libraries

  • PHP Excel Extension PECL extension by Ilia Alshanetsky (Core PHP Developer & Release Master)
  • Spreadsheet_Excel_Writer PEAR Package
  • PHPExcel

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

<?php
$output = <<<EOT
<table>
    <tr>
        <td>Foo</td>
        <td>IñtërnâtiônàlizætiøöäÄn</td>
    </tr>
    <tr>
        <td>Bar</td>
        <td>Перевод русского текста в транслит</td>
    </tr>
</table>
EOT;
// 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;

answered Aug 12, 2010 at 8:57

Php export เป น excel ม เง อนไข

Benjamin CremerBenjamin Cremer

4,8421 gold badge24 silver badges30 bronze badges

4

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
$table->setup();
$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');
$ex->add_data($title);
// end heading
$ex->output_headers( array_keys($table->columns) );
**foreach($data as $row){
        $string="'".trim($row->resname,"'")."'";
        $row->resname=$string;
        $ex->add_data( $table->get_row_from_keyed($row) );
}**
$ex->finish_table();
$ex->finish_document();

answered Mar 6, 2013 at 15:57

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...