function downloadCsvData($title, $csv_data = array(), $file_name = null) {
$csv_string = null;
$csv_row = array();
if (empty($file_name)) {
$file_name = date('Y-m-d').".csv";
}
foreach( $csv_data as $key => $csv_item ) {
if ($key === 0) {
$csv_row[] = implode("," , $csv_item);
continue;
}
$current = array();
foreach($csv_item AS $item) {
$current[] = is_numeric($item)?$item:'"'.str_replace('"', '""', $item).'"';
}
$csv_row[] = implode( "," , $current );
}
$csv_string .= implode( "\r\n", $csv_row );
//echo setlocale(LC_CTYPE ,"ko_KR.eucKR");
header("Content-type:application/vnd.ms-excel;charset=utf-8");
header("Content-Disposition:filename=".date('Y-m-d').".csv");
header("Content-Description: PHP4 Generated Data");
// 不好使
//echo '\xEF\xBB\xBF';
// 这样才能正确添加BOM,以免乱码
echo "\xEF\xBB\xBF".$title."\r\n";
echo $csv_string;
}
在java里, 文件内容最前面加"\uFEFF"
excel bom
最新推荐文章于 2025-06-24 19:09:33 发布