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 ...
随机推荐
- JVM中的对象生命周期
在JVM运行空间中,对象的整个生命周期大致可以分为七个阶段:创建阶段(Creation).应用阶段(Using).不可视阶段(Invisible).不可到达阶段( Unreachable).可收集阶段 ...
- request.getParameter("name")获取参数为null和空字符串的区别
1.获取到的值为空字符串 当url里有name属性,但是没有值的时候,后台用request.getParameter("name")获取到的是空字符串 2.获取到的值为null 当 ...
- 【JXOI2018】排序问题 贪心
我们令$sum_i$表示数字i在加完数字的数列中出现的次数,那么答案显然为$\dfrac{(n+m)!}{\sum_{i=0}^{\infty}sum_i!}$ 不难发现,当每次添加的数为$[l,r] ...
- js02--对象、函数、switch、for、异常、表单验证
现在我们接着来继续学习有关js的一些基础. 1.undefined与null undefined:当变量声明但尚未赋值时,它的类型就是undefined null:表示一个不存在的对象,它 ...
- (转)DB2 HADR 监控详解
原文:https://www.ibm.com/developerworks/cn/data/library/techarticles/dm-1010baosf/ HADR 简介 HADR( 高可用性灾 ...
- Spring Security构建Rest服务-0800-Spring Security图片验证码
验证码逻辑 以前在项目中也做过验证码,生成验证码的代码网上有很多,也有一些第三方的jar包也可以生成漂亮的验证码.验证码逻辑很简单,就是在登录页放一个image标签,src指向一个controller ...
- 【优化】Filddler用于移动端
Fiddler是一个非常强大的Web调试工具,它能捕获所有客户端和服务器的http和https请求,我们可以对请求监视.设置断点,也能修改输入输出数据,这些特性使得Fiddler成为广大web开发者的 ...
- android学习-Activity和Service的生命周期
详细请跳转原网页Activity和Service的生命周期(图) 不解释,不懂算我输 Activity的生命周期(图) Service的声明周期
- asterisk与freepbx常用的命令
asterisk 常用命令: 通过asterisk -r 连接我们的asterisk. 在CLI中常用的命令: sip show peers 显示所有的SIP peers(包括friends) sip ...
- 彻底解决springMVC中文乱码
一.页面编码 <%@ page contentType="text/html;charset=UTF-8" language="java" %> & ...