Csv读写类
<?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读写类的更多相关文章
- EpPlus读取生成Excel帮助类+读取csv帮助类+Aspose.Cells生成Excel帮助类
大部分功能逻辑都在,少量自定义异常类和扩展方法 ,可用类似代码自己替换 //EpPlus读取生成Excel帮助类+读取csv帮助类,epplus只支持开放的Excel文件格式:xlsx,不支持 xls ...
- C#操作CSV存取类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 实用的WPF Xml的简易读写类以及用法示例
转自:http://www.silverlightchina.net/html/study/WPF/2012/0808/17980.html 最近真是写博客写的不可收拾,今天再来一篇. 因为做一些程序 ...
- 利用Spring.Net技术打造可切换的分布式缓存读写类
利用Spring.Net技术打造可切换的Memcached分布式缓存读写类 Memcached是一个高性能的分布式内存对象缓存系统,因为工作在内存,读写速率比数据库高的不是一般的多,和Radis一样具 ...
- [IO] C# INI文件读写类与源码下载 (转载)
/// <summary> /// 类说明:INI文件读写类. /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url]http://www.sufei ...
- php阅读csv文件类
php处理csv文件类: http://www.php100.com/cover/php/540.html <?php define("CSV_Start", 0); def ...
- CSV工具类
分享自己昨天写的CSV工具类, 主要实现解析CSV格式, 直接上代码 #region private /// <summary> /// 从sr当前位置解析一个栏位 /// </su ...
- influxDB 0.9 C# 读写类
influxDB 0.9 C# 读写类 目前influxdb官网推荐的C#读写类是针对0.8版本的,截至本文写作之前,尚未发现有针对0.9的读写类. 我使用influxdb的是用于保存服务器的运行 ...
- QT学习之文件系统读写类
#QT学习之文件系统读写类 QIODevice QFileDevice QBuffer QProcess 和 QProcessEnvironment QFileDevice QFile QFileIn ...
随机推荐
- 一:使用maven构建项目
一般情况下:使用maven构建项目有两种情况: 1:用maven构建java项目: 2:用maven构建javaweb项目: 还有一种经常需要使用到的就是用maven构建项目模块:如:一个父项目用来作 ...
- python pip安装模块提示错误failed to create process
python pip安装模块提示错误failed to create process 原因: 报这个错误的原因,是因为python的目录名称或位置发生改动. 解决办法: 1.找到修改python所在的 ...
- JAVA是是如何处理字符的。
String s = "fs123fdsa";//String变量 byte b[] = s.getBytes();//String转换为byte[] String t = new ...
- rabbitmq系列三 之发布/订阅
1.发布/订阅 在上篇教程中,我们搭建了一个工作队列,每个任务只分发给一个工作者(worker).在本篇教程中,我们要做的跟之前完全不一样 —— 分发一个消息给多个消费者(consumers).这种模 ...
- Yum Proxy
$ cat /etc/yum.conf[main]cachedir=/var/cache/yum/$basearch/$releaseverkeepcache=0debuglevel=2logfile ...
- DOM时钟
使用JS使时钟运动 DOM运动,主要操作css3中transform:rotate(): 计时器setInterval(),setTimeout(),如何防止时钟偷停; 时钟的时针.分针.秒针的运动的 ...
- Windows10下简单搭建zookeeper
转载请注明源出处:http://www.cnblogs.com/lighten/p/6798669.html 1 简介 zookeeper是Apache的一个开源项目,致力于开发和维护一个开源的服务器 ...
- 解决waveInOpen录音编译x64程序出错的问题
1.之前也碰到过x86程序升级为x64程序,关键点是类型大小的使用. 之前同事碰到过一个用int表示指针的程序,程序改为x64会出错,找原因找了半天. 2.今天我也碰到了,使用aveInOpen录音, ...
- Filter应用之-自动登录
自动登录,是为了帮助用户多次使用这个网页时,不用再次输入用户名和密码就可以登录. 是指用户将用户的登录信息,人,保存到本地的文件中Cookie中. Name,value – 声明时 new Cooki ...
- 【链表】Partition List
题目: Given a linked list and a value x, partition it such that all nodes less than x come before node ...