大批量导出数据
来源:互联网 发布:ipad免费下载软件 编辑:程序博客网 时间:2024/05/21 08:52
做数据导出时经常会出现数据量太大导致最后程序卡死 反正我写不来 公司好同事我辈学习之榜样曹老板写的 我给偷过来啦
<?phpclass ExportCsv{ private $path = ''; private $header = []; private $db = null; private $fp = null; private $total = 0; private $writeRow = 0; private $time ; function __construct($file) { $this->time = time(); set_time_limit(0); $endtitle = local_date("Y-m-d-h-i-s"); $this->path = "{$file}-{$endtitle}.xls"; $this->db = &m('store'); header('Content-Type:text/html;charset=utf-8'); header("Content-Encoding: none\r\n"); ob_implicit_flush(true); } public function setHeader($header) { $this->header = $header; } public function processCsv1($config, $datafunc) { //输出文件头 $this->fp = fopen( ROOT_PATH . '/temp/' .$this->path, 'w'); //echo ROOT_PATH . '/temp/' .$this->path.'<br>'; //echo $this->fp; //exit; if (!empty($this->header)) { $content = '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'; $content .= '<table border="1">'; $content .= '<tr>'; foreach ($this->header as $v) { $content .= '<th>' . $v . '</th>'; } $content .= '</tr>'; fwrite($this->fp, $content); } $where = ''; if(isset($config['where']) && !empty($config['where'])) { $where = 'where '.$config['where']; } $countSql = 'select count(DISTINCT('.$config['countfield'].')) from '.$config['from'].' ' . $where; $this->total = $this->db->getOne($countSql); echo '总共'.$this->total.'条记录<br>'; if($this->total > 0) { $pagecount = 2000; $opage = 0; for($pos = 0; $pos<$this->total; $pos+=$pagecount,++$opage) { $selectSql = 'select ' . $config['fields'] . ' from '.$config['from'].' ' . $where . ' '.$config['group']; $selectSql .= " limit " . ($opage) * $pagecount . ", $pagecount"; $this->save2file($selectSql, $datafunc); } } } private function save2file($sql, $datafunc) { $arr = &$this->db->getAll($sql); foreach ($arr as $key => $value) { $value = $datafunc($value); if(empty($value)) { continue; } $content = '<tr>'; foreach ($this->header as $k => $v2) { $tmp = isset($value[$k]) ? $value[$k] : ''; if (is_numeric($tmp)) { $tmp == 0 && $tmp = ''; } $content .= '<td>' . $tmp . '</td>'; } $content .= '</tr>'; fwrite($this->fp, $content); //fputcsv($this->fp, $datafunc($value)); } $this->writeRow += count($arr); unset($arr); echo str_repeat(' ',1024*64); echo '已经读取'.$this->writeRow.'条记录<br>'; ob_flush(); flush(); } public function exportCsv() { if($this->total <= 0) { echo '没有可导出数据<br>'; return; } fwrite($this->fp, '</table>'); fclose($this->fp); echo '读取完毕<br>'; echo '耗时:' . intval(time()-($this->time)).'秒<br>'; echo '<a href="'.STATIC_URL . '/export/' . $this->path .'" download="'.$this->path.'">点击下载</a>'; exit; } public function exportCsv1() { if($this->total <= 0) { echo '没有可导出数据<br>'; return; } fwrite($this->fp, '</table>'); fclose($this->fp); echo '读取完毕<br>'; echo '耗时:' . intval(time()-($this->time)).'秒<br>'; echo '<a href="'.DOMAIN . '/temp/' .$this->path .'" download="'.$this->path.'">点击下载</a>'; exit; }}
使用方法
if(isset($_GET['excel'])) { $export = new ExportCsv('userview'); $export->setHeader([ "type" => "类型", "title" => "名称", "user_name" => "用户", "store_name" => "店铺", "user_type" => "用户类型", "add_time" => "浏览时间", "referer" => "来源页面" ]); $config = []; $config['countfield'] = 'member_visitor.id'; $config['fields'] = 'member_visitor.*,member_visitor.types as vtypes,m.user_name,m.isbazaar,uv.store_id as priv_store_id,uv.privs,s.store_id'; $config['from'] = 'eihoo_member_visitor as member_visitor lEFT JOIN eihoo_member m ON m.user_id=member_visitor.user_id LEFT JOIN eihoo_user_priv uv on uv.user_id=m.user_id LEFT JOIN eihoo_store s on s.user_id=m.user_id'; //echo $conditions;exit; $config['where'] = '';//筛选条件 $config['group'] = 'group by member_visitor.id'; $export->processCsv1($config, function ($goods) { // 这里返回的是1条数据 大部分情况下要针对这条数据做处理得到用户想要的数据 return $goods; }); $export->exportCsv1();}
阅读全文
0 0
- 大批量导出数据
- ACCESS大批量导入导出数据
- SqlServer大批量导入导出数据
- 大批量数据导出到Excel
- 关于大批量数据的导出_SQL Server2005
- 大批量导入导出数据的一点体会。
- 大批量快速导入导出数据[SqlServer+批处理]
- 分享大批量数据导出到excel
- c# 大批量数据导出到excel
- c# 大批量数据导出到excel[1]
- 大批量数据导出到Excel的实现
- 关于SXSSFWorkbook导出大批量数据的问题
- 大批量数据导出导入的一些经验总结
- sqlyong导出大批量数据 备份数据库
- 运用BCP导出数据库大批量数据以及导入大批量数据到数据库
- 超级恶心的大批量数据导出到Excel
- 解决大批量数据导出Excel产生内存溢出的方案
- 解决大批量数据导出Excel产生内存溢出的方案
- SpringMVC-利用filter实现安全登录
- 关于RESTful一些注意事项,接口开发规范
- 学习记录-操作系统
- Rust初识
- ZooKeeper的典型应用场景之命名空间。
- 大批量导出数据
- Mysql(Linux)root用户密码重置方法
- 月亮和六便士
- Linux配置Android设备USB访问权限
- 如何设置SELinux 策略规则
- cocos2d-lua3.7组件篇(一)-开机淡入淡出logo动画
- 构建Spring Cloud微服务分布式云架构
- 笨方法学习Python 习题21 函数可以返回东西 ---学习记录
- linux 安装jdk