0

Salaam guys,

I want to export a report to excel via php, I wrote this code :

header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header( "Content-type: application/vnd.ms-excel; charset=UTF-8" ); 
header("Content-Disposition: attachment; filename=test.xls");

$data = html_entity_decode( $data ,ENT_NOQUOTES,'utf-8');
$data = chr(255).chr(254).iconv("UTF-8","UTF-16LE",$data);

print $data;
exit();  

but it is not working well, my project is arabic and I must encode all characters, so kindly inform me how can I handle it?

Best Regards,

Mike

Freeman
  • 9,464
  • 7
  • 35
  • 58
  • please tell us what exactly is not working! how do input and output look like? – markus Jan 12 '11 at 07:14
  • my input is : سلام for example and my output is : ÿþ – Freeman Jan 12 '11 at 07:16
  • 1
    What is your data? You write headers for XLS file, but XLS file is a binary format, it can not have just encoded text. What are you trying to do? – StasM Jan 12 '11 at 07:19
  • @StasM : no problem, my data is Arabic characters ! – Freeman Jan 12 '11 at 07:23
  • @Mike just arabic text? Why you output it as XLS under XLS type then? – StasM Jan 12 '11 at 07:24
  • Are you creating an actual Excel file somewhere? If so, how? – deceze Jan 12 '11 at 07:25
  • @StasM : because their data must be in excel page ! – Freeman Jan 12 '11 at 07:26
  • If you're actually writing a CSV rather than a real Excel BIFF file, then you'll certainly run into problems. Excel has rather a complex understanding of a UTF-8 CSV file that isn't straightforward to write. If you want to give your client an Excel file, you're better off creating a **real** Excel file using one of the many libraries available (like my own PHPExcel class), rather than trying to convince your system that a CSV file is a BIFF file. Then you can even add other spreadsheet features such as formatting. – Mark Baker Jan 12 '11 at 09:06

2 Answers2

3

application/vnd.ms-excel does not have a charset= parameter, because it is not a text format. You are certianly not writing a valid Excel file. Your code is presumely outputting a CSV file or something. So change the headers accordingly and give it a try:

header("Content-type: text/csv; charset=UTF-8" );
header("Content-Disposition: attachment; filename=test.csv");

Skip the BOM and reencoding stuff. Well, okay: you might need the BOM according to this How can I output a UTF-8 CSV in PHP that Excel will read properly?

If you want to write an excel file, then read up on the BIFF format (which it is actually supposed to use). I'm not sure how and if it supports international chars. But you should use an Excel writer library anyway:

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
  • BOM may be necessary for some versions of Excel to understand UTF-8 on import, see: http://stackoverflow.com/questions/155097/microsoft-excel-mangles-diacritics-in-csv-files – StasM Jan 12 '11 at 07:28
  • @StasM: You're right, just added another question link about that. – mario Jan 12 '11 at 07:29
  • 2
    The *charset* parameter does not depend on whether it’s a text format or not. It’s rather because that information is embedded in the document itself. – Gumbo Jan 12 '11 at 07:44
  • To clarify further, a charset= parameter is honored on textual files mostly. Binary files seldomly carry it, because proxies would be allowed to transform it unless there is a C-C: no-transform flag. The text/* or application/* major type doesn't mean much for that (beause application/ became a catch-all dump anyway). – mario Jan 12 '11 at 08:43
0

it is important that these lines of code are executed before any output. this means that if you have any print/echo-commands prior to this, it will fail.

also, html or even white spaces in a php file, outside of

<?php

tags will ruin it for you :)

davogotland
  • 2,718
  • 1
  • 15
  • 19