How can I encode this?
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
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:
- PHP to Excel, bold font
- MySQL to Excel generation using PHP
- Format text in Excel file via PHP
- Output Excel file in PHP after echo
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 :)
精彩评论