Export to CSV file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$row[] = 'Column 1';
$row[] = 'Column 2';
$row[] = 'Column 3';
$data .= implode(',', $row)."n"; // Join all values without any trailing commas and add a new line 
 
$row = ''; // We must clear the previous values
$row[] = 'Value of column 1';
$row[] = 'Value of column 2';
$row[] = 'Value of column 3';
$data .= implode(',', $row)."n"; 
 
// Output the headers to download the file
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo $data;

Đại khái cú pháp để có thể export ra file csv là:
- Dòng đầu tiên là tên các cột, đặt trong dấu “…” và phân cách nhau bởi dấu phẩy.
- Các dòng còn lại là các giá trị của cột tương ứng, khi gặp ký tự kết thúc hàng thì sẽ tự động (?) tạo hàng mới.
- Cuối cùng gọi header tương ứng.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

0 Responses to “Export to CSV file”


  1. No Comments

Leave a Reply

CAPTCHA image