当前位置:首页 > PHP教程 > php应用 > 列表

php操作csv文件代码实例汇总

发布:smiling 来源: PHP粉丝网  添加日期:2021-04-13 21:27:39 浏览: 评论:0 

这篇文章主要介绍了php操作csv文件代码实例汇总,本文给出6个操作CSV文件的代码实例,包括读取、写入、读取指定区间等,需要的朋友可以参考下

1.读取csv数据, 输出到sales.csv文件中:

  1. $sales = array( 
  2.   array('Northeast', '2004-01-01', '2004-02-01', 12.54), 
  3.   array('Northwest', '2004-01-01', '2004-02-01', 546.33), 
  4.   array('Southeast', '2004-01-01', '2004-02-01', 93.26), 
  5.   array('Southwest', '2004-01-01', '2004-02-01', 945.21), 
  6.   array('All Regions', '---', '--', 1597.34), 
  7. ); //www.phpfensi.com 
  8.  
  9. $fh = fopen('sales.csv', 'w') or die("Can't open sales.csv"); 
  10. foreach($sales as $sales_line){ 
  11.   if(fputcsv($fh, $sales_line) === false){ 
  12.     die("Can't write CSV line");   
  13.   } 
  14. } 
  15.  
  16. fclose($fh) or die("Can't close sales.csv"); 

2. 读取csv数据, 使用特殊的流输出

  1. $sales = array( 
  2.   array('Northeast', '2004-01-01', '2004-02-01', 12.54), 
  3.   array('Northwest', '2004-01-01', '2004-02-01', 546.33), 
  4.   array('Southeast', '2004-01-01', '2004-02-01', 93.26), 
  5.   array('Southwest', '2004-01-01', '2004-02-01', 945.21), 
  6.   array('All Regions', '---', '--', 1597.34), 
  7. ); 
  8.  
  9. $fh = fopen('php://output', 'w'); 
  10. foreach($sales as $sales_line){ 
  11.   if(fputcsv($fh, $sales_line) === false){ 
  12.     die("Can't write CSV line");   
  13.   } 
  14. } 
  15.  
  16. fclose($fh); 

3. 读取csv数据, 输出到缓冲中

  1. $sales = array( 
  2.   array('Northeast', '2004-01-01', '2004-02-01', 12.54), 
  3.   array('Northwest', '2004-01-01', '2004-02-01', 546.33), 
  4.   array('Southeast', '2004-01-01', '2004-02-01', 93.26), 
  5.   array('Southwest', '2004-01-01', '2004-02-01', 945.21), 
  6.   array('All Regions', '---', '--', 1597.34), 
  7. ); 
  8.  
  9. ob_start(); 
  10. $fh = fopen('php://output', 'w') or die("Can't open php://output"); 
  11. foreach($sales as $sales_line){ 
  12.   if(fputcsv($fh, $sales_line) === false){ 
  13.     die("Can't write CSV line");   
  14.   } 
  15. } 
  16.  
  17. fclose($fh) or die("Can't close php://output"); 
  18. $output = ob_get_contents(); 
  19. ob_end_clean(); 

4. 读取csv文件的数据

  1. $fp = fopen('sample3.csv', 'r') or die("can't open file"); 
  2. print "<table>\n"; 
  3. while($csv_line = fgetcsv($fp)){ 
  4.   print '<tr>'; 
  5.   for($i=0, $j=count($csv_line); $i<$j; $i++){ 
  6.     // print '<td>'.htmlentities($csv_line[$i]).'</td>';   
  7.     print '<td>'.htmlentities(iconv("gb2312","utf-8",$csv_line[$i])).'</td>'; 
  8.   } 
  9.   print "</tr>\n"; 
  10. } 
  11. print "</table>\n"; 
  12. fclose($fp) or die("can't close file"); 

5. 下载CSV文件

  1. $sales = array( 
  2.   array('Northeast', '2004-01-01', '2004-02-01', 12.54), 
  3.   array('Northwest', '2004-01-01', '2004-02-01', 546.33), 
  4.   array('Southeast', '2004-01-01', '2004-02-01', 93.26), 
  5.   array('Southwest', '2004-01-01', '2004-02-01', 945.21), 
  6.   array('中国', '2004-01-01', '2004-02-01', 945.21), 
  7. ); 
  8.  
  9. $fh = fopen('php://output', 'w') or die("can't open php://output"); 
  10. $total = 0; 
  11.  
  12. // 告诉浏览器发送的是一个csv文件 
  13. header('Content-Type: application/csv'); 
  14. header('Content-Disposition: attachment; filename="sales.csv"'); 
  15.  
  16. // 输出表头 
  17. fputcsv($output, array('Region', 'Start Date', 'End Date', 'Amount')); 
  18. // 输出每一行数据, 并递增$total 
  19. foreach($sales as $sales_line){ 
  20.   if(fputcsv($fh, $sales_line) === false){ 
  21.     die("Can't write CSV line");   
  22.   }else{ 
  23.     $total += $sales_line[3];   
  24.   } 
  25. } 
  26.  
  27. fputcsv($fh, array('All Regions', '--', '--', $total)); 
  28.  
  29. fclose($fh) or die("Can't close php://output"); 

6.读取CSV文件指定行和区间行

  1. /*****读取CSV文件中的指定行*****/ 
  2. function get_file_line_a($file_name,$line){ 
  3.  $n = 0; 
  4.  $handle = fopen($file_name,'r'); 
  5.  if ($handle) { 
  6.   while (!feof($handle)) { 
  7.     ++$n; 
  8.     $out = fgets($handle, 4096); 
  9.     if($line==$n) break; 
  10.   } 
  11.   fclose($handle); 
  12.  } 
  13.  if( $line==$n) return $out; 
  14.  return false; 
  15. } 
  16.  
  17. echo get_file_line("windows_2011_s.csv", 10);//输入第10行内容 
  18.  
  19.  
  20. /*****读取CSV文件中的区间行*****/ 
  21. function get_file_line_b( $file_name,$line_star, $line_end){ 
  22.   $n = 0; 
  23.   $handle = fopen($file_name,"r"); 
  24.   if ($handle) { 
  25.     while (!feof($handle)) { 
  26.       ++$n; 
  27.       $out = fgets($handle, 4096); 
  28.       if($line_star <= $n){ 
  29.         $ling[] = $out; 
  30.       } 
  31.       if ($line_end == $n) break; 
  32.     } 
  33.     fclose($handle); 
  34.   } 
  35.   if( $line_end==$n) return $ling; 
  36.   return false; 
  37. } 
  38.  
  39. //用 get_file_line读取并输出第11行到第20行 
  40.  
  41. $aa = get_file_line("windows_2011_s.csv", 11, 20); //从第11行到第20行 
  42. foreach ($aa as $bb){ 
  43.   echo $bb."<br>"; 
  44. } 

 

Tags: php操作csv

分享到: