Export to CSV file with Unicode
Khanh is a full stack web developer with over 15 years of experience developing for the web
Search for a command to run...
Khanh is a full stack web developer with over 15 years of experience developing for the web
No comments yet. Be the first to comment.
Elevate your bus reservation experience (or even movie, theater, train, ship, plane tickets) with the React Bus Seat Map Picker – a sophisticated and meticulously crafted script designed exclusively for bus seat bookings. Engineered with strict TypeS...

In previous blog, I migrated all posts from my WordPress to Hashnode at https://blog.donamkhanh.com. Now I create a simple landing page for my main domain and hosted on Github Page. You can access at https://donamkhanh.com. In feature, I'm going to h...

I just finished migrating my blog from Wordpress (self hosted on DigitalOcean) to Hashnode. How did I migrate? I don't have too many articles on Wordpress but I don't like doing it manually so the first thing I did was look to see if there was an aut...

Dự án mình đang làm có yêu cầu sử dụng Azure Active Directory (AAD) để lưu trữ thông tin người dùng & xác thực đăng nhập các kiểu. Ngoài các trường thông tin cơ bản mà AAD hỗ trợ ra thì dự án mình cần lưu thêm 1 số thông tùy chọn khác cho người dùng....

Introduction This is a concise front-end application developed with Angular. You can create a beautiful form with drag and drop visualization. You can get inspiration from it to make a draggable form; at the same time, you can also modify it to suit ...

Ở bài trước tôi có đề cập đến việc export dữ liệu ra file CSV. Tuy nhiên, nếu export dữ liệu có chứa các ký tự Unicode thì sẽ không hiển thị được mặc dù khi mở bằng Notepad thì vẫn hiển thị đúng, tuy nhiên khi mở bằng Excel thì không hiển thị chính xác. Search trên mạng thấy giải pháp của anh Nguyễn Văn Hùng (Hưng?) đã giải quyết được (tôi mới test với German characters trong dự án Shop24 - rất okie).
Bài toán: export dữ liệu tiếng Việt UTF-8 thành file CSV có thể hiển thị đúng khi mở bằng Excel. 3 điểm dẫn đến thành công: + Dùng TAB (\t) thay cho COMMA (,) để phân tách các cột + Convert Encoding của dữ liệu cần output bằng UTF-16LE + Gắn chr(255)chr(254) vào đầu của kết quả cuối cùng trước khi output
PHP code đầy đủ (export order list):
/** * 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' => 'Ausgeführt' ,'5' => 'Zurück' );
$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" ,'customer_name' => $row->bill_firstname. ' ' .$row->bill_lastname ,'modify_date' => mysqlTimestamp(strtotime($row->order_modify_date),'d.m.Y') ,'order_total' => 'CHF '.$row->gand_total ,'order_status' => $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: Nguyễn Văn Hùng weblog