PHP输出excel CSV等文件

<?php
$fileName = "fileName";
header("Content-Type:text/csv;charset=UTF-8"); //'text/csv'是CSV文件,'application/vnd.ms-excel'是excel文件
header("Content-Disposition: attachment; filename=" . $fileName . ".csv"); //$fileName文件名,“.csv”文件扩展名也可是".xls"excel文件
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');

echo "\n" . iconv("utf-8", "gbk", "大连,") . "\t" . iconv("utf-8", "gbk", "大连2");
//echo就是输出内容到文件中,"\n"是换行,"\t"在excel文件里是换格,","在CSV文件里是换格

//生成或补充CSV文件也可用如下方法
$fileName = "test.csv";
$file = fopen($fileName, 'a');//打开指定csv文件,没有则创建
$fields = array( "txt11" ,  "txt21" , "txt31");
fputcsv ( $file ,  $fields );//在打开的csv文件内容末尾另起一行,添加数组中内容,数组一个元素代表一个单元格
$fields = array( "txt12" ,  "txt22" , "txt32");
fputcsv ( $file ,  $fields );//再另起一行添加数组内容
fclose ( $file );//关闭文件句柄

 

你可能感兴趣的:(Excel)