edib
edib

Reputation: 882

exporting php output as excel

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") {
  func1();
} else if ($_GET['i'] == "par2") {
  echo "şşşıııİİİ";
  func2();  
} else if ($_GET['i'] == "par3") {  
  echo "şşşıııİİİ";
  func3();  
} 

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?

Upvotes: 2

Views: 15742

Answers (3)

user2140705
user2140705

Reputation: 11

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();

Upvotes: 1

Benjamin Cremer
Benjamin Cremer

Reputation: 4822

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

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;

Upvotes: 19

wimvds
wimvds

Reputation: 12850

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

Upvotes: 0

Related Questions