csv是一種超級簡單的excel表格形式了,我們生成只要按指定格式然后生成.csv文件就可以了,雖然說簡單但使用中也會碰到不少的問題,下面小編來為各位總結一下。
例子,生成csv文件并下載
- //要生成csv文件的數組
- $csvArr=array();
- $csvArr[]=array('用戶編號1','上班日期1','簽到時間1','簽退時間1');
- $csvArr[]=array('用戶編號2','上班日期2','簽到時間2','簽退時間2')
- download_send_headers("data_export_" . date("Y-m-d") . ".csv");
- $head=array('用戶編號','上班日期','簽到時間','簽退時間');
- echo array2csv($csvArr,$head);
- unset($csvArr);
- die();
- function array2csv(array &$array,$head)
- {
- if (count($array) == 0) {
- return null;
- }
- ob_start();
- $df = fopen("php://output", 'w');
- if(!$head){
- $head=array_keys(reset($array));
- }
- fputcsv($df,$head);
- foreach ($array as $row) {
- fputcsv($df, $row);
- }
- fclose($df);
- return ob_get_clean();
- }
- function download_send_headers($filename) {
- // disable caching
- $now = gmdate("D, d M Y H:i:s");
- header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
- header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
- header("Last-Modified: {$now} GMT");
- //Vevb.com
- // force download
- header("Content-Type: application/force-download");
- header("Content-Type: application/octet-stream");
- header("Content-Type: application/download");
- // disposition / encoding on response body
- header("Content-Disposition: attachment;filename={$filename}");
- header("Content-Transfer-Encoding: binary");
- }
php array生成csv文件
- <?php
- $data = array(
- array( 'row_1_col_1', 'row_1_col_2', 'row_1_col_3' ),
- array( 'row_2_col_1', 'row_2_col_2', 'row_2_col_3' ),
- array( 'row_3_col_1', 'row_3_col_2', 'row_3_col_3' ),
- );
- $filename = "example";
- header("Content-type: text/csv");
- header("Content-Disposition: attachment; filename={$filename}.csv");
- header("Pragma: no-cache");
- header("Expires: 0");
- outputCSV($data);
- function outputCSV($data) {
- $outputBuffer = fopen("php://output", 'w');
- foreach($data as $val) {
- foreach ($val as $key => $val2) {
- $val[$key] = iconv('utf-8', 'gbk', $val2);
- // CSV的Excel支持GBK編碼,一定要轉換,否則亂碼
- }
- fputcsv($outputBuffer, $val);
- }
- fclose($outputBuffer);
- }
- ?>
解決 fgetcsv函數在php5.2.8 中的bug
環境linux
問題解析出來的數據不完整,有為空的字段
網上查了下說是在php5.2.8 中存在bug
解決辦法是使用自定義函數
- function __fgetcsv(& $handle, $length = null, $d = ',', $e = '"') {
- $d = preg_quote($d);
- $e = preg_quote($e);
- $_line = "";
- $eof=false;
- while ($eof != true) {
- $_line .= (emptyempty ($length) ? fgets($handle) : fgets($handle, $length));
- $itemcnt = preg_match_all('/' . $e . '/', $_line, $dummy);
- if ($itemcnt % 2 == 0)
- $eof = true;
- }
- $_csv_line = preg_replace('/(?: |[ ])?$/', $d, trim($_line));
- $_csv_pattern = '/(' . $e . '[^' . $e . ']*(?:' . $e . $e . '[^' . $e . ']*)*' . $e . '|[^' . $d . ']*)' . $d . '/';
- preg_match_all($_csv_pattern, $_csv_line, $_csv_matches);
- $_csv_data = $_csv_matches[1];
- for ($_csv_i = 0; $_csv_i < count($_csv_data); $_csv_i++) {
- $_csv_data[$_csv_i] = preg_replace('/^' . $e . '(.*)' . $e . '$/s', '$1' , $_csv_data[$_csv_i]);
- $_csv_data[$_csv_i] = str_replace($e . $e, $e, $_csv_data[$_csv_i]);
- }
- return emptyempty ($_line) ? false : $_csv_data;
- }
excel無法正確讀取長度超過32K的CSV域問題
php 導出csv文件用excel打開后,產品表述字段分兩行顯示。
查看了下這個字段發現這個字段超過32K的字符,excel會把字符串打斷成兩行,如果小于32K,顯示正常。
這是EXCEL的限制,目前還沒有找到解決辦法。
excel一個單元格最多是32767個字符。
解決PHP生成UTF-8編碼的CSV文件用Excel打開亂碼的問題
PHP生成UTF-8編碼的CSV文件用Excel打開中文顯示亂碼,是由于輸出的CSV文件中沒有BOM。
- <?php
- $now = gmdate("D, d M Y H:i:s");
- header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
- header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
- header("Last-Modified: {$now} GMT");
- // force download
- header("Content-Type: application/force-download");
- header("Content-Type: application/octet-stream");
- header("Content-Type: application/download");
- // disposition / encoding on response body
- header("Content-Disposition: attachment;filename={$filename}");
- header("Content-Transfer-Encoding: binary");
- $items_data=array(
- '0'=>array('title'=>'test test test1'),
- '1'=>array('title'=>'test test test2'),
- '2'=>array('title'=>'test test test3')
- )
- print(chr(0xEF).chr(0xBB).chr(0xBF));
- //設置utf-8 + bom ,處理漢字顯示的亂碼
- echo array2csv($items_data);
- function array2csv(array &$array)
- {
- if (count($array) == 0) {
- return null;
- }
- ob_start();
- $df = fopen("php://output", 'w');
- fputcsv($df, array_keys(reset($array)));
- foreach ($array as $row) {
- fputcsv($df, $row);
- }
- fclose($df);
- return ob_get_clean();
- }
- ?>
導出csv文件數字會自動變科學計數法的解決方法
用程序導出的csv文件,當字段中有比較長的數字字段存在時,在用excel軟甲查看csv文件時就會變成科學技術法的表現形式。
其實這個問題跟用什么語言導出csv文件沒有關系。Excel顯示數字時,如果數字大于12位,它會自動轉化為科學計數法;如果數字大于15位,它不僅用于科學技術費表示,還會只保留高15位,其他位都變0。
解決這個問題
只要把數字字段后面加上顯示上看不見的字符即可,字符串結尾加上制表符"/t".
php 程序可以這樣判斷,注意一定是"/t",不是'/t'.
- is_numeric($val)?$val."/t":$val;
新聞熱點
疑難解答