<?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. 01-Python的基础知识1

    基础即常识. - Python的对象模型 - Python中一切皆对象. - 内置对象:数字,字符串,列表,元组,集合等等. - 基本输入 - 基本模式    变量 = input("提示字 ...

  2. SnapKit 类图

     如图上图所示,居于中心的是ConstraintDescription,它用来生成Constraint,最后再转换成系统的NSLayoutConstraint. ConstraintDescript ...

  3. 扩展中国剩余定理(扩展CRT)详解

    今天在$xsy$上翻题翻到了一道扩展CRT的题,就顺便重温了下(扩展CRT模板也在里面) 中国剩余定理是用于求一个最小的$x$,满足$x\equiv c_i \pmod{m_i}$. 正常的$CRT$ ...

  4. POJ 1018

    #include<iostream> #define MAXN 105 #define inf 10000000 #include<vector> #include<io ...

  5. Top Leaders社区发现算法(top leaders community detection approach in information networks)

    一.概念 复杂网络:现实生活中各种系统都可以看做成复杂网络,复杂网络构成包括节点和边,节点是网络中的基本组成单元,节点之间的联系或者关系是网络中的边.例如 电力网络:基站代表节点,基站之间是否互通表示 ...

  6. 给easyui datebox时间框控件扩展一个清空的实例

    给easyui datebox扩展一个清空的实例 步骤一:拓展插件 /** * 给时间框控件扩展一个清除的按钮 */ $.fn.datebox.defaults.cleanText = '清空'; ( ...

  7. (转)Mysql常用命令行

    原文:http://www.cnblogs.com/TsengYuen/archive/2012/01/11/2319034.html Mysql常用命令行 Mysql经常使用号令行大全 熬头招.my ...

  8. JavaMail之-通过邮件激活账号

    关键点就在于: 根据用户的给出的email,给这个email发送一个邮件.这个邮件中应该带有一个激活码?(32位UUID,64位UUID). 大概步骤: 1,  注册功能 - 只要用户注册成功,就给他 ...

  9. Android 开发服务类 01_ServletForXML

    Servlet implementation class NewsListServlet package com.wangjialin.server.xml; import java.io.IOExc ...

  10. Unable to handle kernel NULL pointer dereference at virtual address 00000000问题的解决

    今天在编译好内核模块后,安装内核模块memdev.ko的时候,出现了Unable to handle kernel NULL pointer dereference at virtual addres ...