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


Posted in PHP onSeptember 22, 2014

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

$sales = array(
  array('Northeast', '2004-01-01', '2004-02-01', 12.54),
  array('Northwest', '2004-01-01', '2004-02-01', 546.33),
  array('Southeast', '2004-01-01', '2004-02-01', 93.26),
  array('Southwest', '2004-01-01', '2004-02-01', 945.21),
  array('All Regions', '---', '--', 1597.34),
);

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

fclose($fh) or die("Can't close sales.csv");

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

$sales = array(
  array('Northeast', '2004-01-01', '2004-02-01', 12.54),
  array('Northwest', '2004-01-01', '2004-02-01', 546.33),
  array('Southeast', '2004-01-01', '2004-02-01', 93.26),
  array('Southwest', '2004-01-01', '2004-02-01', 945.21),
  array('All Regions', '---', '--', 1597.34),
);

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

fclose($fh);

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

$sales = array(
  array('Northeast', '2004-01-01', '2004-02-01', 12.54),
  array('Northwest', '2004-01-01', '2004-02-01', 546.33),
  array('Southeast', '2004-01-01', '2004-02-01', 93.26),
  array('Southwest', '2004-01-01', '2004-02-01', 945.21),
  array('All Regions', '---', '--', 1597.34),
);

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

fclose($fh) or die("Can't close php://output");
$output = ob_get_contents();
ob_end_clean();

4. 读取csv文件的数据

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

5. 下载CSV文件

$sales = array(
  array('Northeast', '2004-01-01', '2004-02-01', 12.54),
  array('Northwest', '2004-01-01', '2004-02-01', 546.33),
  array('Southeast', '2004-01-01', '2004-02-01', 93.26),
  array('Southwest', '2004-01-01', '2004-02-01', 945.21),
  array('中国', '2004-01-01', '2004-02-01', 945.21),
);

$fh = fopen('php://output', 'w') or die("can't open php://output");
$total = 0;

// 告诉浏览器发送的是一个csv文件
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="sales.csv"');

// 输出表头
fputcsv($output, array('Region', 'Start Date', 'End Date', 'Amount'));
// 输出每一行数据, 并递增$total
foreach($sales as $sales_line){
  if(fputcsv($fh, $sales_line) === false){
    die("Can't write CSV line");  
  }else{
    $total += $sales_line[3];  
  }
}

fputcsv($fh, array('All Regions', '--', '--', $total));

fclose($fh) or die("Can't close php://output");

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

/*****读取CSV文件中的指定行*****/
function get_file_line_a($file_name,$line){
 $n = 0;
 $handle = fopen($file_name,'r');
 if ($handle) {
  while (!feof($handle)) {
    ++$n;
    $out = fgets($handle, 4096);
    if($line==$n) break;
  }
  fclose($handle);
 }
 if( $line==$n) return $out;
 return false;
}

echo get_file_line("windows_2011_s.csv", 10);//输入第10行内容


/*****读取CSV文件中的区间行*****/
function get_file_line_b( $file_name,$line_star, $line_end){
  $n = 0;
  $handle = fopen($file_name,"r");
  if ($handle) {
    while (!feof($handle)) {
      ++$n;
      $out = fgets($handle, 4096);
      if($line_star <= $n){
        $ling[] = $out;
      }
      if ($line_end == $n) break;
    }
    fclose($handle);
  }
  if( $line_end==$n) return $ling;
  return false;
}

//用 get_file_line读取并输出第11行到第20行

$aa = get_file_line("windows_2011_s.csv", 11, 20); //从第11行到第20行
foreach ($aa as $bb){
  echo $bb."<br>";
}
PHP 相关文章推荐
PHP 生成的XML以FLASH获取为乱码终极解决
Aug 07 PHP
把1316这个数表示成两个数的和,其中一个为13的倍数,另一个是11的倍数,求这两个数。
Jun 24 PHP
ajax 的post方法实例(带循环)
Jul 04 PHP
ThinkPHP与PHPExcel冲突解决方法
Aug 08 PHP
第二章 PHP入门基础之php代码写法
Dec 30 PHP
PHP 正则表达式之正则处理函数小结(preg_match,preg_match_all,preg_replace,preg_split)
Oct 05 PHP
解析CodeIgniter自定义配置文件
Jun 18 PHP
测试php函数的方法
Nov 13 PHP
PHP编程实现多维数组按照某个键值排序的方法小结【2种方法】
Apr 27 PHP
关于ThinkPhp 框架表单验证及ajax验证问题
Jul 19 PHP
PHP封装的XML简单操作类完整实例
Nov 13 PHP
PHP实现一个限制实例化次数的类示例
Sep 16 PHP
PHP提示Cannot modify header information - headers already sent by解决方法
Sep 22 #PHP
php过滤HTML标签、属性等正则表达式汇总
Sep 22 #PHP
PHP中exec与system用法区别分析
Sep 22 #PHP
PHP-Java-Bridge使用笔记
Sep 22 #PHP
11个PHPer必须要了解的编程规范
Sep 22 #PHP
PHP正则替换函数preg_replace和preg_replace_callback使用总结
Sep 22 #PHP
php分页函数完整实例代码
Sep 22 #PHP
You might like
php中将网址转换为超链接的函数
2011/09/02 PHP
PHP图像处理之使用imagecolorallocate()函数设置颜色例子
2014/11/19 PHP
Laravel 5.0 发布 新版本特性详解
2015/02/10 PHP
laravel 5 实现模板主题功能
2015/03/02 PHP
PHP函数func_num_args用法实例分析
2015/12/07 PHP
实例分析10个PHP常见安全问题
2019/07/09 PHP
文本框中禁止非数字字符输入比如手机号码、邮编
2013/08/19 Javascript
Jquery 分页插件之Jquery Pagination
2015/08/25 Javascript
JS模拟bootstrap下拉菜单效果实例
2016/06/17 Javascript
JS for循环中i++ 和 ++i的区别介绍
2016/07/20 Javascript
jQuery实现磁力图片跟随效果完整示例
2016/09/16 Javascript
jQuery插件HighCharts绘制的基本折线图效果示例【附demo源码下载】
2017/03/07 Javascript
Vue 滚动行为的具体使用方法
2017/09/13 Javascript
微信小程序实现多选框全选与取消全选功能示例
2019/05/14 Javascript
layui弹出框Tab选项卡的示例代码
2019/09/04 Javascript
vue开发中遇到的问题总结
2020/04/07 Javascript
在Vue中使用HOC模式的实现
2020/08/23 Javascript
微信小程序实现日历签到
2020/09/21 Javascript
three.js显示中文字体与tween应用详析
2021/01/04 Javascript
[02:47]DOTA2亚洲邀请赛 HR战队出场宣传片
2015/02/07 DOTA
python八大排序算法速度实例对比
2017/12/06 Python
手把手教你python实现SVM算法
2017/12/27 Python
Python pygorithm模块用法示例【常见算法测试】
2018/08/16 Python
python二维码操作:对QRCode和MyQR入门详解
2019/06/24 Python
详解python websocket获取实时数据的几种常见链接方式
2019/07/01 Python
Python:二维列表下标互换方式(矩阵转置)
2019/12/02 Python
python的slice notation的特殊用法详解
2019/12/27 Python
优秀公益广告词大全
2014/03/19 职场文书
企业党建工作汇报材料
2014/08/19 职场文书
会计电算化实训报告
2014/11/04 职场文书
2015年餐厅服务员工作总结
2015/04/23 职场文书
欢送领导祝酒词
2015/08/12 职场文书
2019脱贫攻坚工作总结报告范本!
2019/08/06 职场文书
继承Win10缺点!教你关闭Win11烦人的网络搜索
2021/11/23 数码科技
《巫师》是美食游戏?CDPR10月将推出《巫师》官方食谱
2022/04/03 其他游戏
django项目、vue项目部署云服务器的详细过程
2022/07/23 Servers