exporting php output as excel - php

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?

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;

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

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

Related

Encoding CSV File With arabic content

I've tried to import csv file into mysql table and everything is Ok but I get a problem with Encoding the content of the file, the content is in "arabic Content."
The following image explains my problem:
1- this is my CSV FILE
2-My PHP code:
$c=1;
while($data=fgetcsv($file,1000,","))
{
if($c==1)
{
$c++;
continue;
}
$row=explode(";",$data[0]);
foreach($row as $val)
{
echo $val." ";
}
echo "<br>";
$c++;
}
3- The Result:
I try to insert data in mysql table
4- If I insert these data into Mysql Table, it will be :
I'm thankful for any help,
Thanks
You need to convert your CSV file in UTF-8 or process your string using utf8_encode.
Make sure your MySQL table is also in UTF-8
/open file pointer to standard output
$fp = fopen('php://output', 'w');
//add BOM to fix UTF-8 in Excel
fputs($fp, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
Thanks for all answers but the solution is:
open CVS file in any editor
re-save it with UTF-8 encoding and CVS extension
and upload it by an script

File encoding commande

I have 1200 files encoded ANSI. I need to convert them into UTF-8. It is not reasonable to convert each file using the simple solution file/save as!
Is there a commande in php which convert files from ANSI to UTF-8?
You can do this with the iconv library, which has a PHP binding (https://secure.php.net/manual/en/function.iconv.php). Consider using the command-line program to convert your source files instead, and keeping everything in utf8 instead of juggling encodings.
I have found the solution using PHP.
This is the code used:
<?php
set_time_limit ( 30000 );
$k=0;
while ($k<1232)
{
$fres="contenu_url".$k.".txt";
$inF = fopen($fres,"r");
$fres1="contenu_utf".$k.".txt";
$OutF = fopen($fres1,"w+");
$k=$k+1;
if($inF == false)
echo "<p>Impossible d'ouvrir le fichier</p>.\n";
$contenu_ancien="";
while (!feof($inF))
$contenu_ancien .= fgets($inF, 4096);
$contenu_utf8 = utf8_encode ($contenu_ancien);
fputs($OutF,$contenu_utf8);
fclose($OutF); fclose($inF);
}
?>

Exporting CSV with UTF8 to Excel using PHP

my client asked me to build an export system that export the whole SQL database into csv file that works on excel. I found PHPexcel and it's great, but I thought I can do stuff way more easier and faster using my own functions.
After struggling with Excel encoding, I finally succeeded to export the CSV that will work on Excel using the following code:
<?php
$data = showData("price"); //Function that loads all the SQL database into an array.
function array2csv(array &$array)
{
if (count($array) == 0) {
return null;
}
ob_start();
$df = fopen("php://output", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
function download_send_headers($filename) {
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
header ( 'HTTP/1.1 200 OK' );
header ( 'Date: ' . date ( 'D M j G:i:s T Y' ) );
header ( 'Content-Type: application/vnd.ms-excel') ;
header ( 'Content-Disposition: attachment;filename=export.csv' );
}
download_send_headers("export.csv");
$final = array2csv($data);
print chr(255) . chr(254) . mb_convert_encoding($final, 'UTF-16LE', 'UTF-8');
die();
?>
The problem is that when I try to open the file in Excel there are no columns. Each row is a large column that contains the whole data of the specific row, separated by a comma.
I figured out that I would need to replace those commas with something that Excel can read as a "new column". But I still need to keep my CSV to work as it is.
I searched SO and Google with no luck whatsoever finding a solution that will keep my CSV intact and yet split the data into columns Excel. If there is no way to do both, I think the more important thing to my client is that the Excel version will work as it should (each row separated into columns).
This is a picture of how it looks on CSV (using numbers on Mac)
And this is a picture of how it looks on Excel 2007 on Windows
It's all in the fputcsv() function:
http://php.net/manual/en/function.fputcsv.php
Try to choose the correct $delimiter, $enclosure and $escape_char.
The default character that is used as the field-separator in excel is set by the locale settings. That means: Importing CVS files can be language dependent. Export to CSV from excel to see what it does on your system, and check that your client system has the same settings.
It's better to export to XML instead of CSV, because that will circumvent this problem:
http://en.wikipedia.org/wiki/Microsoft_Office_XML_formats

PHP: Creating csv file if data text contains "," data will be splitted into parts and put it in other column?

I got problems in creating a csv file because if my data contains , it will be splitted into parts and put it in other columns that destroys my csv file.
Here is the sample output:
Sample output link
As what you can see the first row in TITLE column should have this data "Acer Aspire AS5250-0639 15.5" Laptop (1.65 GHz AMD Dual-Core Processor E-450, 4 GB RAM, 500 GB Hard Drive, DVD+/-RW Optical Drive, Windows 7 Home Premium 64-bit)" starting in 4 GB RAM it is splitted and put it in other columns because before it it has , and more.
As of now here is my PHP script:
<?php
include 'database.php';
$db = new database();
$data = $db->exportdatabase();
$out = '';
$out .= 'GSEQ,BRAND,TITLE,SIMAGE,SPRICE,WEIG,DIMENSIONS,ASIN,CATEGORYNAME,MODELNO';
$out .="\n";
foreach($data as $items){
$out .=' '.$items['GSEQ'].', '.$items['BRAND'].', '.$items['TITLE'].', '.$items['SIMAGE'].', '.$items['SPRICE'].', '.$items['WEIG'].', '.$items['DIMENSIONS'].'
'.$items['ASIN'].', '.$items['CATEGORYNAME'].', '.$items['MODELNO'].'
';
$out .="\n";
//var_dump($items);
}
header('Content-Description: File Transfer');
header("Content-Type: application/csv") ;
header("Content-Disposition: attachment; filename=Data.csv");
header("Expires: 0");
echo $out;
exit;
?>
How can I possibly fix this issue?
For starters I would stop trying to do something yourself that PHP can do for you. Use fputcsv instead of doing it yourself: http://php.net/manual/en/function.fputcsv.php. This method also deals with escaping your delimiter ( your comma ).
On top of that.. dont use ',' as your csv seperator. You are probably better off using '|' as a seperater, since that character is way less common.
One method is to wrap each field using quotes (")
Another is to replace the troublesome "," with another character using str_replace.

PHPWord: Creating an Arabic right to left word document

I'm trying to use PHPWord to create a word document that will include dynamic data pulled out from a MySQL database. The database has MySQL charset: UTF-8 Unicode (utf8)
MySQL connection collation: utf8_unicode_ci and so does the table fields.
Data is stored and previewed fine in HTML, however when creating the document with the arabic variables, the output in Word looks like أحÙد Ùبار٠اÙÙرÙ.
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('templates/.../wtvr.docx');
$document->setValue('name', $name);
$document->setValue('overall_percent_100', $overall_percent_100);
$document->save('Individual Report - ' . $name . '.docx');
Is there anyway to fix that?
Well, yes. But you must unfortunately modify the library. The author of the library uses utf8_encode/utf8_decode obviously without understanding what they do at all.
On line 150, of Shared/String.php:
Replace
public static function IsUTF8($value = '') {
return utf8_encode(utf8_decode($value)) === $value;
}
With
public static function IsUTF8($value = '') {
return mb_check_encoding($value, "UTF-8");
}
Then, if you do
$ grep -rn "utf8_encode" .
On the project root, you will find all lines where utf8_encode is used. You will see lines like
$linkSrc = utf8_encode($linkSrc); //$linkSrc = $linkSrc;
$givenText = utf8_encode($text); //$givenText = $text;
You can simply remove the utf8_encode as shown in the comments.
Why is utf8_encode/utf8_decode wrong? First of all, because that's not what they do. They do from_iso88591_to_utf8 and from_utf8_to_iso88591. Secondly, ISO-8859-1 is almost never used, and usually when someone claims they use it, they are actually using Windows-1252. ISO-8859-1 is a very tiny character set, not even capable of encoding €, let alone arabic letters.
You can do fast reviews of a library by doing:
$ grep -rn "utf8_\(en\|de\)code" .
If you get matches, you should move on and look for some other library. These functions simply do the wrong thing every time, and even if someone needed some edge case to use these functions, it's far better to be explicit about it when you really need ISO-8859-1, because you normally never do.
Please find the following points to write all types of utf-8 right to left data insertion in phpword template.
In setValue function (line #95) in Template.php please comment the following portion of code
//if(!is_array($replace)) {
// $replace = utf8_encode($replace);
//}
If you have problem with right to left which in some language the text mix up with left to right text add the following code in the same setValue function.
$replace = "<w:rPr><w:rtl/></w:rPr>".$replace;
//==== here is a working example of how the word data can be write inside the word template
//--- load phpword libraries ----
$this->load->library("phpword/PHPWord");
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('./forms/data.docx');
$document->setValue('NAME', 'شراف الدين');
$document->setValue('SURNAME', 'مشرف');
$document->setValue('FNAME', 'ظهرالدين');
$document->setValue('MYVALUE', '15 / سنبله / 1363');
$document->setValue('PROVINCE', 'سمنگان');
$document->setValue('DNAME', 'عبدالله');
$document->setValue('DMOBILE', '0775060701');
$document->setValue('BOX','<w:sym w:font="Wingdings" w:char="F06F"/>');
$document->setValue('NO','<w:sym w:font="Wingdings" w:char="F06F"/>');
//$document->setValue('BOX2','<w:sectPr w:rsidR="00000000"><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720" w:gutter="0"/><w:cols w:space="720"/><w:docGrid w:linePitch="360"/>');
$document->setValue('YES','<w:sym w:font="Wingdings" w:char="F0FE"/>');
$document->setValue('CLASS1','<w:sym w:font="Wingdings" w:char="F06F"/>');
$document->setValue('CLASS2','<w:sym w:font="Wingdings" w:char="F0FE"/>');
$document->setValue('DNAME','يما شاه رخي');
$document->setValue('TEL','0799852369');
$document->setValue('ENTITY','مشاور حقوقي و نهادي');
$document->setValue('ENTITY','مشاور حقوقي و نهادي');
$document->setValue('REMARKS','در مسابقات سال 2012 میلادی در میدان Judo بر علاوه به تعداد 39 نفر در تاریخ 4/میزان/ سال 1391 قرار ذیل اند.');
$file = "./forms/data2.docx";
$document->save($file);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=data2.docx");
header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("Content-Transfer-Encoding: binary");
ob_clean();
flush();
readfile($file);
//need how design can change the looking.
colr #E4EDF9
Find
$objWriter->startElement('w:t');
$objWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text
$objWriter->writeRaw($strText);
$objWriter->endElement();
In Writer/Word2007/Base.php
replace with
$objWriter->startElement('w:textDirection');
$objWriter->writeAttribute('w:val', 'rlTb');
$objWriter->startElement('w:t');
$objWriter->writeAttribute('xml:space', 'preserve'); // needed because of drawing spaces before and after text
$objWriter->writeRaw($strText);
$objWriter->endElement();
$objWriter->endElement();
Also, make sure you don't use any styles to make it work, or else you will have to repeat this step in every function you use.
I had to fix it in two place different than Nasers's way:
1- in Section.php addText function:
I did this:
//$givenText = utf8_encode($text);
$givenText = $text;
2- in cell.php addText function
I did this:
// $text = utf8_encode($text);
now your word file will display unicode characters in right way.
And then i had a problem in texts directions.
i found the solution by using this code
$section->addText($val['notetitle'],array('textDirection'=>PHPWord_Style_Cell::TEXT_DIR_TBRL));
u can see the two constants in the cell.php file
const TEXT_DIR_TBRL = 'tbRl';
const TEXT_DIR_BTLR = 'btLr';
note that u can not apply other array combined styles like Paragraph before than 'textDirection' , because whose styles make 'textDirection' disabled.
Open PHPWord\Template.php
Change in setValue function (line no 89.) as below.
Change $replace = utf8_encode($replace);
to
$replace = $replace;

Categories