DDR爱好者之家 Design By 杰米

本文实例讲述了PHP实现CSV文件的导入和导出类。分享给大家供大家参考。具体如下:

<"n";
    unset($param_arr['nav']);
    //组装数据
    foreach($param_arr as $k=>$v){
      foreach($v as $k1=>$v1){
        $export_str .= implode(',',$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('Content-Type:APPLICATION/OCTET-STREAM');
    ob_start();   
   // $file_str= iconv("utf-8",'gbk',$export_str);
    ob_end_clean();
    echo $export_str;
  }
  /**
   * 导入
   * */
  public function import($path,$column = 3){
    $flag = flase;
    $code = 0;
    $msg = '未处理';
    $filesize = 1; //1MB
    $maxsize = $filesize * 1024 * 1024;
    $max_column = 1000;
 
    //检测文件是否存在
    if($flag === flase){
      if(!file_exists($path)){
        $msg = '文件不存在';
        $flag = true;
      }
    }
    //检测文件格式
    if($flag === flase){
      $ext = preg_replace("/.*.([^.]+)/","$1",$path);
      if($ext != 'csv'){
        $msg = '只能导入CSV格式文件';
        $flag = true;
      }
    }
    //检测文件大小
    if($flag === flase){
      if(filesize($path)>$maxsize){
        $msg = '导入的文件不得超过'.$maxsize.'B文件';
        $flag = true;
      }
    }
    //读取文件
    if($flag == flase){
      $row = 0;
      $handle = fopen($path,'r');
      $dataArray = array();
      while($data = fgetcsv($handle,$max_column,",")){
        $num = count($data);
        if($num < $column){
          $msg = '文件不符合规格真实有:'.$num.'列数据';
          $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(
'nav'=>array('用户名','密码','邮箱'),
array(0=>array('xiaohai1','123456','xiaohai1@zhongsou.com'),
   1=>array('xiaohai2','213456','xiaohai2@zhongsou.com'),
   2=>array('xiaohai3','123456','xiaohai3@zhongsou.com')
));
$column = 3;
$csv = new Csv($param_arr, $column);
//$csv->export();
$path = 'C:\Documents and Settings\Administrator\Temp\txxx.csv';
$import_arr = $csv->import($path,3);
var_dump($import_arr);
?>

希望本文所述对大家的php程序设计有所帮助。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米