You are on page 1of 3

Export ra CSV file vi Unicode

Th hai, 18 Thng 8 2008 14:05 Nguyn Vn Hng PHP

Bi ton: export d liu ting Vit UTF-8 thnh file CSV c th hin th ng khi m bng Excel. 3 im dn n thnh cng: + Dng TAB (\t) thay cho COMMA (,) phn tch cc ct + Convert Encoding ca d liu cn output bng UTF-16LE + Gn chr(255)chr(254) vo u ca kt qu cui cng trc khi output /** * EXPORT ORDER LIST TO CSV FILE * @author khanhdn */ function exportCSV() { global $user; member_access(); $memberInfo = member_info(); $result = drupal_query("SELECT order_id ,order_code ,bill_firstname ,bill_lastname ,order_modify_date ,gand_total ,order_status FROM {order} WHERE shop_code = '". $memberInfo['shop_code']."' ORDER BY order_creation_date DESC"); $status_options = array(

'1' => 'Neu' ,'2' => 'In Bearbeitung' ,'3' => 'Auf der Post' ,'4' => 'Ausgefhrt' ,'5' => 'Zurck' ); $csv = "Order code\tCustomer Name\tModify Date\tOrder Total\tOrder Status\r\n"; if(count($result['data'])) { foreach($result['data'] as $row) { $order_list = array( 'order_code' => "$row->order_code" me' ,'customer_na => $row->bill_firstname. ' ' .$row->bill_lastname

,'modify_date ' => mysqlTimestamp(strtotime($row>order_modify_date),'d.m.Y') ,'order_total ' s' => 'CHF '.$row->gand_total ,'order_statu => $status_options[$row->order_status] ); $csv .= join("\t", $order_list)."\r\n"; } } $csv = chr(255).chr(254).mb_convert_encoding($csv, "UTF-16LE", "UTF-8"); header("Content-type: application/x-msdownload");

header("Content-disposition: csv; filename=" . date("Y-m-d") . "_order_list.csv; size=".strlen($csv)); echo $csv; exit(); } Source: Nguyn Vn Hng weblog

You might also like