標籤:

php導出csv格式文件數據保存為字元串類型

php導出csv格式文件數據保存為字元串類型

// 導出excelfunction excel_do($filename,$list){ $filename = $filename.".csv"; @unlink($filename); $filename = iconv ( "UTF-8" , "gbk" , $filename ); $fp = @fopen($filename, w); // 數據 foreach($list as $ko=>$vo){ $content[$ko] = implode(,,$vo); } // 順序寫入文件 foreach ($content as $line) { $line = iconv ( "UTF-8" , "gbk" , $line ); fputcsv4($fp, @split(,, $line)); } download_file($filename);}//寫入csvfunction fputcsv4($fh, $arr){ $csv = ""; while (list($key, $val) = @each($arr)) { $val = str_replace(", "", " ".$val); $csv .= ".$val.",; } $csv = substr($csv, 0, -1); $csv .= "
";
if (!@fwrite($fh, $csv)) return FALSE;} // 下載文件function download_file($file){ if(is_file($file)){ $length = filesize($file); $magicFile = get_cfg_var(magic_mime.magic); $finfo = new finfo(FILEINFO_MIME, $magicFile); $type = $finfo->file($file); $showname = ltrim(strrchr($file,/),/); header("Content-Description: File Transfer"); header(Content-type: . $type); header(Content-Length: . $length); if (preg_match(/MSIE/, $_SERVER[HTTP_USER_AGENT])) { //for IE header(Content-Disposition: attachment; filename=" . rawurlencode($showname) . "); } else { header(Content-Disposition: attachment; filename=" . $showname . "); } readfile($file); @unlink($file); exit; } else { exit(導出失敗,請重試); }}

備註: $val = str_replace(", "", " ".$val); 在每個數據前面加製表符
推薦閱讀:

在 Lightroom Classic CC 中導出預設、設置和增效工具
前山西領導出獄,為什麼出現「榮回故里」的場面?
OCR文字識別軟體識別結果的文檔怎麼導出

TAG:csv | PHP | 導出 |