How can I encode this? - php

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 :)

Related

I have tried to excel export with the format of xls using PHP. but it didn't support Burmese language

Please find the sample code
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen(content));
header("Content-type: application/vnd.ms-excel");
header("Content-type: application/octet-stream, charset=UTF-8; encoding=UTF-8");
header("Content-Disposition: attachment; filename=".$filename.".xls");
echo $xls_output;
exit;``
Thanks,
I think excel will treat your stream as utf-8 if a bom is found at the begining of the stream. Try to start your output stream with UTF-8 BOM after headers like this :
echo pack("CCC",0xef,0xbb,0xbf);
echo $xls_output;
Hope it helps!
You need to change the encoding to Unicode and maybe charset to UTF-16:
The following link will guide you to proper settings depending on the language:
http://www.unicode.org/faq/myanmar.html
You must also have the font installed on your machine.

Transferring Excel via PHP header()

An Excel file is stored in a database (hexvalues of former binary data). I need to read it and make it available for download.
Here's the code...
$out = hex2bin($out); // the stuff from the database
header("Content-Type: application/vnd.ms-excel");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename='$filename'");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
die($out);
Actually I receive stuff... and it looks like the original file with a slight difference.
When using a hex-editor, it shows
ÐÏࡱá
instead of
ÐÏࡱá
(looks like an additional tab). So Excel isn't able to show the sheet but the raw data.
That's not from the data in $out; the conversion bin2hex (during import in database) and hex2bin (export from database) works without problems. If I write $out to a file via fopen / fwrite, it's exactly the original Excel file (which I can open normally).
How can I accomplish this?
Ensure you don't have this extra tab character before the opening <?php tag or after the closing one ?>. Actually, it is good practice to omit the closing tag exactly for this reason.

PHPEXCEL set UTF-8 encode

I have headers:
header('Content-Type: text/html; charset=UTF-8');
header("Content-type: application/octetstream");
header('Content-Disposition: attachment; filename="export.csv"');
but decoding not work correct if I have word in database "Pagrindinė" in excel show "PagrindinÄ", what is wrong with my headers ?
What is wrong with my headers ?
Nothing, your headers are looking fine.
What is wrong with Excel?
The user who opens the file in Excel needs to tell Excel that the file is in UTF-8 encoding. Direct that user to contact the vendor of the software it uses for her/his support options.
Users of LibreOffice or a derivate of it do not have that problem btw., so one solution is to tell those to install a suite of such, open the CSV file and save it as Excel file for example.
Or you directly create the Excel file on your server.
Maybe if you change the format text encoding of your .php file when you save it, to Unicode (UTF-8). It works for me.
Hope it helps.
try this:
header('Content-Transfer-Encoding: binary');
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Disposition: attachment; filename = "Export '.date("Y-m-d").'.xls"');
header('Pragma: no-cache');
//these characters will make correct encoding to excel
echo chr(255).chr(254).iconv("UTF-8", "UTF-16LE//IGNORE", $out);

PHP Generate CSV Cell are not displaying on windows

I am using Cakephp 2.0. I need to generate a Report on daily basics in an excel format.
So i written a function to generate a CSV File. Its working fine in Linux machine when i downloads the file in OpenOffice but if I try to open the same file in windows Microsoft then there is no cells, but the data is displaying properly. With this I listed my coding
$model.= "-".date('d M,Y');
header ("Expires: Mon, ".date('d M,Y')." GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header ("Content-type: application/vnd.ms-excel");
header ("Content-Disposition: attachment; filename=\"$model.xls" );
header ("Content-Description: Generated Report" );
Any one helps thanx
I think you might doing something wrong here Please check your header. You have mentioned csv in the question and you're writing excel header. That is might be confusing MS Excel
header ("Content-type: application/vnd.ms-excel");
header ("Content-Disposition: attachment; filename=\"$model.xls" );
header ("Content-Description: Generated Report" );
I'm using this kind of header format which is perfectly working for me in windows and as well as in Open office. Please give a try with this.
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: octet/stream");
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-disposition: attachment; filename=" . basename($filename.".csv"));
header("refresh: 0; url= ".$redirect_url);
Excel CSV files should be named SSV files, because the data columns need to be separated by a semicolon instead of a comma. Also the data should be enclosed in quotes and any inner quotes need to be escaped as double quote; e.g. 1;"Some ""string"" with escapes".

Cannot render Excel download using SSRS SDK for PHP

I cannot render an excel file for download correctly using SSRS SDK for PHP. I can for a CSV, and I can render one for PDF inline. The contents of produced file is garbage when viewed in EXCEL. Here is my code:
$renderAs = new RenderAsEXCEL();
$result = $ssrs_report->Render2($renderAs,
PageCountModeEnum::$Estimate,
$Extension,
$MimeType,
$Encoding,
$Warnings,
$StreamIds);
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"".$reportName.".xls\";");
I've tried many different MIME Content-Type values.
A bit late but I had the same problem and it took me a while to figure it out:
There are more files that contain spaces before or after the php-tags than just the RenderAsEXCEL.php file.
Since the main php file of the SSRS library includes almost all files you need to remove all spaces before and after the php-tags from all files.
After you've done that use this code to download the Excel file.
$renderAsExcel = new RenderAsEXCEL();
$result = $ssrsServer->Render2($renderAsExcel,
PageCountModeEnum::$Estimate,
$Extension,
$MimeType,
$Encoding,
$Warnings,
$StreamIds
);
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"report.xls\"");
header("Content-length: ".(string)(strlen($result)));
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"),date("s"),date("m"), date("d"),date("Y")))." GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
echo $result;
i'm probably way to late but for anyone else that stumbles on this problem:
the problem is actually that there is a space in front of the first
<?php
in RenderAsEXCEL.php and there are a few lines after the last
?>
in RenderAsPDF.php
remove these white spaces and you will not have to use an ugly ob_clean() workaround
I've not used that library, but i have had great results from this one: http://www.bettina-attack.de/jonny/view.php/projects/php_writeexcel/
Built it into many apps without having to think very much about it.

Categories