<?php

 /**
* CSV 文件读写类
*
* 示例: $header = array('name' => '名字', 'age' =>'年龄', 'idcard' => '身份证号'); $data = array(
array('age' => 12, 'name' => '李四', 'idcard' => '42088751564616131312'),
(object) array('idcard' => '72888751564616131312', 'age' => 17, 'name' => '张三'),
); Csv::instance()->open('e:/abc1.csv', true)->header($heaer)->create($data)->close();
Csv::instance()->open()->header($header)->create($data)->download('用户数据.csv'); */ class Csv { public $header = array(); public $fp = ''; public static function instance(){ return new static();
} /**
* 打开/创建文件
* @param string $file
* @param string $mode
* @return $this
*/
public function open($file = '', $mode = 'a'){ if(empty($file)){ $this->fp = tmpfile();
}else{
if($mode === true) $mode = 'w+';
$this->fp = fopen(iconv('UTF-8', 'GBK', $file), $mode);
} return $this; } /**
* 写入表头
* @param $value
* @return $this
*/
public function header($value){ $this->header = is_array($value)? $value : (array) $value;
fputcsv($this->fp, array_values($this->header)); return $this;
} /**
* 写入一行
* @param $data
* @return $this
*/
public function write($data){ if(is_string($data)){
$data = json_decode($data, true);
}else if(!is_array($data)){
$data = json_decode(json_encode($data), true);
} if(empty($data)) return $this; if($this->header){
$column = array();
foreach($this->header as $key => $value){
$column[$key] = isset($data[$key])? $data[$key] : 'NULL';
}
$data = &$column;
} foreach($data as $key => &$value){
if(is_numeric($value) && strlen($value) > 10){
$value .= "\t";
}else if(!is_scalar($value)){
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
} } fputcsv($this->fp, $data); return $this;
} /**
* 读取一行
* @param int $length
* @param string $delimiter
* @param string $enclosure
* @param string $escape
* @return array|false|null
*/
public function read($length = 0, $delimiter = ',', $enclosure = '"', $escape = '\\'){ return fgetcsv($this->fp, $length, $delimiter, $enclosure, $escape);
} /**
* 获取文件内容
* @param null $callback
* @param int $start
* @param int $length
* @return array
*/
public function content($callback = null, $start = 1, $length = 0){ if($start > 1){
for($start; $start > 1; $start--){
$this->read(1);
}
} $data = array();
$key = 0;
while(($row = $this->read()) !== false){ if($length < 0) break;
if($length > 0) $length--; if(is_callable($callback)){
$row = $callback($row);
if($row === null) continue;
}
$data[$key] = $row;
$key++; } return $data;
} /**
* 下载
* @param $file
*/
public function download($file){ $file = iconv('UTF-8', 'GBK', $file);
header('Content-Type:application/application/octet-stream');
header("Content-Disposition:attachment;filename=$file"); fseek($this->fp, 0);
echo stream_get_contents($this->fp); $this->close(); } /**
* 快速写入文件
* @param $data
* @return $this
*/
public function create($data){ if(is_string($data)) $data = json_decode($data, true);
foreach($data as $row){
$this->write($row);
} return $this; } public function close(){ fclose($this->fp);
} }

Csv读写类的更多相关文章

  1. EpPlus读取生成Excel帮助类+读取csv帮助类+Aspose.Cells生成Excel帮助类

    大部分功能逻辑都在,少量自定义异常类和扩展方法 ,可用类似代码自己替换 //EpPlus读取生成Excel帮助类+读取csv帮助类,epplus只支持开放的Excel文件格式:xlsx,不支持 xls ...

  2. C#操作CSV存取类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  3. 实用的WPF Xml的简易读写类以及用法示例

    转自:http://www.silverlightchina.net/html/study/WPF/2012/0808/17980.html 最近真是写博客写的不可收拾,今天再来一篇. 因为做一些程序 ...

  4. 利用Spring.Net技术打造可切换的分布式缓存读写类

    利用Spring.Net技术打造可切换的Memcached分布式缓存读写类 Memcached是一个高性能的分布式内存对象缓存系统,因为工作在内存,读写速率比数据库高的不是一般的多,和Radis一样具 ...

  5. [IO] C# INI文件读写类与源码下载 (转载)

    /// <summary> /// 类说明:INI文件读写类. /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url]http://www.sufei ...

  6. php阅读csv文件类

    php处理csv文件类: http://www.php100.com/cover/php/540.html <?php define("CSV_Start", 0); def ...

  7. CSV工具类

    分享自己昨天写的CSV工具类, 主要实现解析CSV格式, 直接上代码 #region private /// <summary> /// 从sr当前位置解析一个栏位 /// </su ...

  8. influxDB 0.9 C# 读写类

    influxDB 0.9 C# 读写类   目前influxdb官网推荐的C#读写类是针对0.8版本的,截至本文写作之前,尚未发现有针对0.9的读写类. 我使用influxdb的是用于保存服务器的运行 ...

  9. QT学习之文件系统读写类

    #QT学习之文件系统读写类 QIODevice QFileDevice QBuffer QProcess 和 QProcessEnvironment QFileDevice QFile QFileIn ...

随机推荐

  1. webpack快速入门——Json配置文件使用

    在实际工作中,我们的项目都会配置一个Json的文件或者说API文件,作为项目的配置文件. 有时候你也会从后台读取到一个json的文件,这节课就学习如何在webpack环境中使用Json. 如果你会we ...

  2. 双向一对一映射@OneToOne

    双向一对一的实例我已经上传到GitHub,entrance项目上了,感兴趣的可以下载下来跑跑,这里讲两个在运行过程中遇到的问题. 问题一:上一篇博客的最后我留下了问题.一对一关联查询注解@OneToO ...

  3. Nginx+Tomcat搭建负载均衡

    一.       工具 nginx-1.8.0 apache-tomcat-6.0.33 二.    目标 实现高性能负载均衡的Tomcat集群: 三.    步骤 1.首先下载Nginx,要下载稳定 ...

  4. 深入理解Java接口和抽象类

    对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的时候会以为它们可以随意互换使用, ...

  5. Centos7 nginx安装过程

    一.Centos版本: cat /etc/centos-release CentOS Linux release 7.1.1503 (Core) 二.nginx下载 官网:http://nginx.o ...

  6. php如何判断字符串是否是字母和数字的组合

    转载自百度 /其实判断是否是字母和数字或字母数字的组合还可以用PHP ctype_alnum函数 if(!ctype_alnum($vipurl)){ echo '只能是字母或数字的组合';exit; ...

  7. 【树】Path Sum II(递归)

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...

  8. mac 系统安装VM虚拟机打开时报错,提示不是虚拟磁盘的解决方式。

    最近刚买的苹果系统,不太会用,装了个虚拟机vmware fusion,好不容易把需要的软件装好,然后不知道是我操作了哪里,今天再次打开虚拟机的时候打不开了,报错提示找不到磁盘文件(虚拟磁盘-00000 ...

  9. tomcat启动(Ⅶ)请求处理--Processor.process(SocketWrapper<S> socketWrapper)

    tomcat启动(六)Catalina分析-StandardServer.start() 上一篇分析到:Http11NioProcessor.process(SocketWrapper<S> ...

  10. php中接收参数,不论是来自GET还是POST方法

    不多说,直接上代码, 其实也就是先用GET的方法去获取,如果值为空,在用POST方法去获取 写下来是为了方便和备忘 function getParam($str){       if ( isset( ...