473,395 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

export arabic data to csv format

Dear Sir,

I have a PHP script that export CSV with arabic text, for example I download the CSV on the website then save to desktop of my computer and Open it on NOTEPAD... that works fine I could still see all the arabic text OK with no problem, but if I open the CSV file in Microsoft EXCEL, all the arabic text are busted and no longer readable.
arabic text is inserted and stored in database as UTF-8 format.

Do you know a solution for this, how could I make it work OK on MS Excel, because the user want it to view the CSV file in Excel and this is so much urgent for me . please reply.

Regards,
Fauk Chowdhury.
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3. // Connect to database
  4. mysql_connect("localhost", "root", "root123");
  5. mysql_select_db("medgulf");
  6.  
  7.  $fname = date("d-m-Y");
  8. // mention the file name to display in download prompt
  9. $file_name = "$fname.csv";
  10.  
  11. header("Content-Disposition: attachment; filename=\"$file_name\"");
  12. header("Cache-Control: cache, must-revalidate");
  13. header("Pragma: public");
  14. header('Content-Type: text/xml,  charset=UTF-8; encoding=UTF-8');
  15.  
  16. $fd1 = $_POST['fd'];
  17. $fm1 = $_POST['fm'];
  18. $fy1 = $_POST['fy']; 
  19. // Output header
  20. //echo "Name, Code"."\n";
  21. $frmday = $fy1."-".$fm1."-".$fd1;
  22. $ld1 = $_POST['ld'];
  23. $lm1 = $_POST['lm'];
  24. $ly1 = $_POST['ly']; 
  25.  
  26. $today = $ly1."-".$lm1."-".$ld1;
  27. //echo $frmday;
  28. $usrname = $_POST['usname'];
  29. $export_date = date("Y-m-d");
  30.  
  31. $result = mysql_query("SELECT product, agent_code, policy_no, DATE_FORMAT(entry_date, '%d/%m/%Y'), DATE_FORMAT(inception_date_eng, '%d/%m/%Y'), DATE_FORMAT(inception_date_hijri, '%d/%m/%Y'), DATE_FORMAT(expiry_date_eng, '%d/%m/%Y'), DATE_FORMAT(expiry_date_hijri, '%d/%m/%Y'), client_info.first_name, client_info.family_name, client_info.father_name, client_info.grand_father_name, client_info.mobile_no, client_info.dob, client_info.p_o_box, client_info.zip_code, client_info.city, client_info.id_type, client_info.id_no, vehicle_type, license_type, net_premium, brandname, model, reg_type, plate_no, model_year, chassis_no, color, user_name, age_limit  FROM vehicle_info, client_info where vehicle_info.policy_no = client_info.sr AND entry_date BETWEEN '$frmday' AND '$today' AND  user_name='$usrname' AND exported='yes' ");
  32. $str = mb_convert_encoding($result, "UTF-8", "ASCII");
  33. echo "encode $str";
  34.  
  35. if($result) {
  36. while($rs = mysql_fetch_array($result))
  37. {    echo $rs[0].", ";
  38.     echo $rs[1].", ";
  39.     echo $rs[2].", ";
  40.     echo $rs[3].", ";
  41.     echo $rs[4].", ";
  42.     echo $rs[5].", ";
  43.     echo $rs[6].", ";
  44.     echo $rs[7].", ";
  45.     echo $rs[8].", ";
  46.     echo $rs[9].", ";
  47.     echo $rs[10].", ";
  48.     echo $rs[11].", ";
  49.     echo $rs[12].", ";
  50.     echo $rs[13].", ";
  51.     echo $rs[14].", ";
  52.     echo $rs[15].", ";
  53.     echo $rs[16].", ";
  54.     echo $rs[17].", ";
  55.     echo $rs[18].", ";
  56.     echo $rs[19].", ";
  57.     echo $rs[20].", ";
  58.     echo $rs[21].", ";
  59.     echo $rs[22].", ";
  60.     echo $rs[23].", ";
  61.     echo $rs[24].", ";
  62.     echo " ".", ";
  63.     echo $rs[25].", ";
  64.     echo $rs[26].", ";
  65.     echo $rs[27].", ";
  66.     echo $rs[28].", ";
  67.     echo " ".", ";
  68.     echo " ".", ";
  69.     echo $rs[29].", ";
  70.     echo $rs[30].", ";
  71. }
  72.  
  73. $result4 = mysql_query("UPDATE vehicle_info SET exported ='yes', exported_date='$export_date'   WHERE user_name ='$usrname'");
  74.  
  75. if (!$result4) {
  76.            die('Query failed: To Vehicle Info Table ');
  77.             }
  78.  
  79.  
  80. }
  81. else {
  82.  
  83. echo "There Is No Data To Export ";
  84.  
  85. }
  86.  
  87.  
  88. ?>
Dec 10 '07 #1
2 16354
pbmods
5,821 Expert 4TB
Heya, Fauk.

Make sure you're setting your MySQL connection encoding:
Expand|Select|Wrap|Line Numbers
  1. mysql_query("SET NAMES 'utf-8'", $conn);
  2. mysql_query("SET CHARACTER SET 'utf-8'", $conn);
  3.  
Where $conn is your MySQL connection resource.
Dec 23 '07 #2
unus29
1
Dear Faruk,

You can use phpexcel library for this purpose. Its works great. You can download the library from here:
http://code.google.com/p/php-excel/

The sample code is as follows (I used that in joomla):
function export_contact_messages() {
$model = $this->getModel('usercontacts');

$rows = $model->getContactMessages();
global $mosConfig_absolute_path;

if( !file_exists( $mosConfig_absolute_path . '/libraries/phpexcel/PHPExcel.php') ) {
die('Phpexcel library not found');
}
$phpexcel_path = $mosConfig_absolute_path . '/libraries/phpexcel/PHPExcel.php';
include_once($phpexcel_path);

//die($phpexcel_path);

$objPHPExcel = new PHPExcel();

$objPHPExcel->getProperties()->setCreator("Title")
->setLastModifiedBy("Title")
->setTitle("Contact Message")
->setSubject("Contact Message");



$objPHPExcel->setActiveSheetIndex(0)
->setCellValueByColumnAndRow(0, 1, "Name")
->setCellValueByColumnAndRow(1, 1, "Email")
->setCellValueByColumnAndRow(2, 1, "Message")
->setCellValueByColumnAndRow(3, 1, "Posted");

$row_count = 2;
foreach ($rows as $row) {
$objPHPExcel->setActiveSheetIndex(0)
->setCellValueByColumnAndRow(0, $row_count, $row->name)
->setCellValueByColumnAndRow(1, $row_count, $row->email)
->setCellValueByColumnAndRow(2, $row_count, $row->message)
->setCellValueByColumnAndRow(3, $row_count, $row->created_at);
$row_count++;
}

// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('contact_message');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

ob_clean();
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="contract_messages.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
Jun 20 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: zahid | last post by:
Hello, Dear Friends, I have to make such type of interface,you can say it a html form. in which a admin can write arabic data and can insert that data or recordsin database in ASP. and i have to...
1
by: Do Park via SQLMonster.com | last post by:
Hello all, I don?t often export data from a table. I am wondering how you export data from a table. I?d like to know how you export in real world. Do you export data from a table to a flat...
1
by: cab2 | last post by:
We currently have an application built in Access that takes in a csv file using docmd.transfertext. We allow users to export their data to a file for later use (csv format). Users are also...
5
by: Igor | last post by:
Hi everyone! I have a question about exporting data to Microsoft Excel. I am writing program in C# and have to export some data to excel workbook. It works very good using OLE technology, but...
4
by: Arif | last post by:
My programs searches the header of input barcode in index file. Get the record position next to Barcode header. Then moves the file pointer of products file to reach that record. My products...
1
by: samsadi | last post by:
Hi every one , I am using ASP to export database data to excel file. However, the database table contains special charecters like arabic charecters which is not being exported to excel properly. Is...
4
by: Max2006 | last post by:
Hi, We are developing a SQL server based asp.net application. As part of requirement we should allow users import/export some relational data through web user interface. We are investigation...
1
by: hash4sp | last post by:
Hello ! DBF file consists of a field i.e."districts" which holds data in Arabic language. DBF file is in ANSI format. How to convert this ANSI format so that it displays actual Arabic text on the...
1
by: farukcse | last post by:
Dear Sir, I am facing a problem with exporting data . when i exported data the arabic characters comes with a invalid character set . i used utf8 encoding systeme in both of php files. so can any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.