加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php生成csv文件下载的问题总结

发布时间:2023-09-15 11:00:26 所属栏目:PHP教程 来源:
导读:csv是一种超级简单的excel表格形式了,我们生成只要按指定格式然后生成.csv文件就可以了,虽然说简单但使用中也会碰到不少的问题,下面小编来为各位总结一下。

例子,生成csv文件并下载

//要生成csv文件的数组
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"); 
//Cuoxin.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,显示正常。

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章