温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在php中对csv文件进行操作

发布时间:2021-03-05 15:50:40 阅读:181 作者:Leah 栏目:开发技术
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这期内容当中小编将会给大家带来有关怎么在php中对csv文件进行操作,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

php有什么用

php是一个嵌套的缩写名称,是英文超级文本预处理语言,它的语法混合了C、Java、Perl以及php自创新的语法,主要用来做网站开发,许多小型网站都用php开发,因为php是开源的,从而使得php经久不衰。

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($fhor 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($fhor 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($outputarray('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($fharray('All Regions''--''--'$total));

fclose($fhor 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($handle4096);
    if($line==$nbreak;
  }
  fclose($handle);
 }
 if$line==$nreturn $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($handle4096);
      if($line_star <= $n){
        $ling[] = $out;
      }
      if ($line_end == $nbreak;
    }
    fclose($handle);
  }
  if$line_end==$nreturn $ling;
  return false;
}

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

$aa get_file_line("windows_2011_s.csv"1120); //从第11行到第20行
foreach ($aa as $bb){
  echo $bb."<br>";
}

上述就是小编为大家分享的怎么在php中对csv文件进行操作了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×