background image

$title

 = 

array

("First_Name","Last_Name","Contact_Email","Telephone"); 

//第一行数据

$data_1

 = 

array

("42343","423432","4234","4234"); 

$data_2

 = 

array

("4234","Last_Name","Contact_Email","Telephone"); 

$title

 = implode(",",

$title

); 

//

 

用 ' 分割成字符串

$data_1

 = implode(",",

$data_1

); 

// 

 

用 ' 分割成字符串

$data_2

 = implode(",",

$data_2

); 

// 

 

用 ' 分割成字符串

$data_str

 =

$title

."/r/n".

$data_1

."/r/n".

$data_2

."/r/n"; 

//加入换行符

fwrite(

$fp

,

$data_str

); 

// 写入数据

fclose(

$fp

); 

//关闭文件句柄

echo

 "生成成功";

}

echo

 "<br>";

echo

 "<a href='?action=make'>生成 csv 文件</a>";

?>
 
也可以做一个封闭函数:

 

封闭函数一:
代码如下:
 

function

 exportToCsv(

$csv_data

$filename

 = 'export.csv') {

$csv_terminated

 = "/n";

$csv_separator

 = ",";

$csv_enclosed

 = '"';

$csv_escaped

 = "//";

// Gets the data from the database

$schema_insert

 = '';

$out

 = '';

// Format the data
foreach

 (

$csv_data

 

as

 

$row

)

{

$schema_insert

 = '';

$fields_cnt

 = 

count

(

$row

);

//printr($row);

$tmp_str

 = '';

foreach

(

$row

 

as

 

$v

)

{

$tmp_str

 .= 

$csv_enclosed

.

str_replace

(

$csv_enclosed

$csv_escaped

 . 

$csv_enclosed

$v

).

$csv_enclosed

.

$csv_separator

;

// end for

 

$tmp_str

 = 

substr

(

$tmp_str

, 0, -1);