The role of CSV file processing class in PHP

墨辰丷
Release: 2023-03-31 17:28:01
Original
1829 people have browsed it

这篇文章主要介绍了PHP实现CSV文件的导入和导出类,实例分析了php针对csv文件的操作技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了PHP实现CSV文件的导入和导出类。具体如下:

<?php
/**
 * CSV 文件处理类
 */
class Csv{
  public $csv_array; //csv数组数据
  public $csv_str; //csv文件数据
  public function __construct($param_arr, $column){
    $this->csv_array = $param_arr;
    $this->path = $path;
    $this->column = $column;
  }
  /**
   * 导出
   * */
  public function export(){
    if(empty($this->csv_array) || empty($this->column)){
      return false;
    }
    $param_arr = $this->csv_array;
    unset($this->csv_array);
    $export_str = implode(&#39;,&#39;,$param_arr[&#39;nav&#39;])."n";
    unset($param_arr[&#39;nav&#39;]);
    //组装数据
    foreach($param_arr as $k=>$v){
      foreach($v as $k1=>$v1){
        $export_str .= implode(&#39;,&#39;,$v1)."n";
      }
    }
    //将$export_str导出
    header( "Cache-Control: public" );
    header( "Pragma: public" );
    header("Content-type:application/vnd.ms-excel");
    header("Content-Disposition:attachment;filename=txxx.csv");
    header(&#39;Content-Type:APPLICATION/OCTET-STREAM&#39;);
    ob_start();   
   // $file_str= iconv("utf-8",&#39;gbk&#39;,$export_str);
    ob_end_clean();
    echo $export_str;
  }
  /**
   * 导入
   * */
  public function import($path,$column = 3){
    $flag = flase;
    $code = 0;
    $msg = &#39;未处理&#39;;
    $filesize = 1; //1MB
    $maxsize = $filesize * 1024 * 1024;
    $max_column = 1000;
 
    //检测文件是否存在
    if($flag === flase){
      if(!file_exists($path)){
        $msg = &#39;文件不存在&#39;;
        $flag = true;
      }
    }
    //检测文件格式
    if($flag === flase){
      $ext = preg_replace("/.*.([^.]+)/","$1",$path);
      if($ext != &#39;csv&#39;){
        $msg = &#39;只能导入CSV格式文件&#39;;
        $flag = true;
      }
    }
    //检测文件大小
    if($flag === flase){
      if(filesize($path)>$maxsize){
        $msg = &#39;导入的文件不得超过&#39;.$maxsize.&#39;B文件&#39;;
        $flag = true;
      }
    }
    //读取文件
    if($flag == flase){
      $row = 0;
      $handle = fopen($path,&#39;r&#39;);
      $dataArray = array();
      while($data = fgetcsv($handle,$max_column,",")){
        $num = count($data);
        if($num < $column){
          $msg = &#39;文件不符合规格真实有:&#39;.$num.&#39;列数据&#39;;
          $flag = true;
          break;
        }
        if($flag === flase){
          for($i=0;$i<3;$i++){
            if($row == 0){
              break;
            }
            //组建数据
            $dataArray[$row][$i] = $data[$i];
          }
        }
        $row++;
      }
    }
    return $dataArray;
  }
}
$param_arr = array(
&#39;nav&#39;=>array(&#39;用户名&#39;,&#39;密码&#39;,&#39;邮箱&#39;),
array(0=>array(&#39;xiaohai1&#39;,&#39;123456&#39;,&#39;xiaohai1@zhongsou.com&#39;),
   1=>array(&#39;xiaohai2&#39;,&#39;213456&#39;,&#39;xiaohai2@zhongsou.com&#39;),
   2=>array(&#39;xiaohai3&#39;,&#39;123456&#39;,&#39;xiaohai3@zhongsou.com&#39;)
));
$column = 3;
$csv = new Csv($param_arr, $column);
//$csv->export();
$path = &#39;C:\Documents and Settings\Administrator\Temp\txxx.csv&#39;;
$import_arr = $csv->import($path,3);
var_dump($import_arr);
?>
Copy after login

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

php基于seoreport类针对网站SEO信息检查与获取

php使用curl获取Compete统计网站信息的方法

php实现在限定的区域里自动调整字体大小的功能

The above is the detailed content of The role of CSV file processing class in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!