免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2450 | 回复: 1
打印 上一主题 下一主题

PHP 读写 CSV [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-02-13 14:55 |只看该作者 |倒序浏览
PHP 读写 CSV







1. 读取csv数据, 输出到sales.csv文件中
Php代码
  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('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");  

  17. $sales = array(
  18.         array('Northeast', '2004-01-01', '2004-02-01', 12.54),
  19.         array('Northwest', '2004-01-01', '2004-02-01', 546.33),
  20.         array('Southeast', '2004-01-01', '2004-02-01', 93.26),
  21.         array('Southwest', '2004-01-01', '2004-02-01', 945.21),
  22.         array('All Regions', '---', '--', 1597.34),
  23. );

  24. $fh = fopen('sales.csv', 'w') or die("Can't open sales.csv");
  25. foreach($sales as $sales_line){
  26.         if(fputcsv($fh, $sales_line) === false){
  27.                 die("Can't write CSV line");       
  28.         }
  29. }

  30. fclose($fh) or die("Can't close sales.csv");
复制代码
2. 读取csv数据, 使用特殊的流输出
Php代码
  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);  

  17. $sales = array(
  18.         array('Northeast', '2004-01-01', '2004-02-01', 12.54),
  19.         array('Northwest', '2004-01-01', '2004-02-01', 546.33),
  20.         array('Southeast', '2004-01-01', '2004-02-01', 93.26),
  21.         array('Southwest', '2004-01-01', '2004-02-01', 945.21),
  22.         array('All Regions', '---', '--', 1597.34),
  23. );

  24. $fh = fopen('php://output', 'w');
  25. foreach($sales as $sales_line){
  26.         if(fputcsv($fh, $sales_line) === false){
  27.                 die("Can't write CSV line");       
  28.         }
  29. }

  30. fclose($fh);
复制代码
3. 读取csv数据, 输出到缓冲中
Php代码
  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();  

  20. $sales = array(
  21.         array('Northeast', '2004-01-01', '2004-02-01', 12.54),
  22.         array('Northwest', '2004-01-01', '2004-02-01', 546.33),
  23.         array('Southeast', '2004-01-01', '2004-02-01', 93.26),
  24.         array('Southwest', '2004-01-01', '2004-02-01', 945.21),
  25.         array('All Regions', '---', '--', 1597.34),
  26. );

  27. ob_start();
  28. $fh = fopen('php://output', 'w') or die("Can't open php://output");
  29. foreach($sales as $sales_line){
  30.         if(fputcsv($fh, $sales_line) === false){
  31.                 die("Can't write CSV line");       
  32.         }
  33. }

  34. fclose($fh) or die("Can't close php://output");
  35. $output = ob_get_contents();
  36. ob_end_clean();
复制代码
4. 读取csv文件的数据
Php代码
  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");  

  13. $fp = fopen('sample3.csv', 'r') or die("can't open file");
  14. print "<table>\n";
  15. while($csv_line = fgetcsv($fp)){
  16.         print '<tr>';
  17.         for($i=0, $j=count($csv_line); $i<$j; $i++){
  18.                 // print '<td>'.htmlentities($csv_line[$i]).'</td>';       
  19.                 print '<td>'.htmlentities(iconv("gb2312","utf-8",$csv_line[$i])).'</td>';
  20.         }
  21.         print "</tr>\n";
  22. }
  23. print "</table>\n";
  24. fclose($fp) or die("can't close file");
复制代码
5. 下载的CSV文件
Php代码
  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");  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-02-13 14:56 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP