php导出数据为CSV文件DEMO
代码示例:
private function _download_send_headers($filename) {
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
private function _array2csv($array) {
if (count($array) == 0) {
return null;
}
$keys = array_keys(reset($array));
echo implode(',', $keys) . PHP_EOL;
for ($i = 0, $j = count($array); $i < $j; $i++) {
echo implode(',', $array[$i]) . PHP_EOL;
}
}
public function saveAsCsv() {
$this -> _download_send_headers("data_export_" . date("Y-m-d") . ".csv");
$ret = array(
array(
'id' => 1,
'name' => 'hello'
),
array(
'id' => 2,
'name' => 'world'
),
array(
'id' => 3,
'name' => 'good'
),
);
$this -> _array2csv($ret);
die();
}
当然还有用fputcsv的,但我试了一下效果不太好。
php导出数据为CSV文件DEMO的更多相关文章
- PHP导出数据到CSV文件函数 csv_export()
后台往往需要导出各种数据到 Excel文档中.通常我们是导出 .csv文件格式,PHP导出函数参考代码如下: /** * 导出数据到CSV文件 * * @param array $data 二维数组( ...
- PHP导出数据到CSV文件函数/方法
如果不清楚什么是CSV文件,可看如下文章介绍 CSV格式的是什么文件?CSV是什么的缩写? /** * 导出数据到CSV文件 * @param array $data 数据 * @param arr ...
- PHP导出数据到CSV文件
后台往往需要导出各种数据到 Excel文档中.通常我们是导出 .csv文件格式,PHP导出函数参考代码如下: /** * 导出数据到CSV文件 * * @param array $data 二维数组( ...
- mysql导出数据到csv文件
在日常工作中经常会遇见导出表中的数据到csv文件的操作,这里就简单总结一下导出的操作. 下面对csv文件的描述是摘录: 据RFC4180文档设置的,该文档全称Common Format and MIM ...
- mysql 导出数据到csv文件的命令
1.导出本地数据库数据到本地文件 mysql -A service_db -h your_host -utest -ptest mysql> select * from t_apps where ...
- 从数据库中导出数据到.csv文件
考虑到csv文件比xls文件格式容易控制,所以在这次导出中用的是.csv格式. protected function exportInfo($arr, &$err){ $nameInfo = ...
- Oracle SQLPlus导出数据到csv文件
时不时地我们需要导出一些数据用作备份.查看报表等,如果用Sql Developer导出会非常慢.而用SqlPlus,则速度非常快. 准备SQL执行文件export.sql: set colsep , ...
- SQL SERVER利用BCP命令在命令行下导出数据到csv文件中
bcp "select * from (DBNAME).dbo.qt_trace where User_1 is not null" queryout c:\%date:~6,4% ...
- php导出数据到csv
序言 php导出数据到csv是一种很常见的功能,且csv相比于excel文件有其一定的优势,首先csv对数据的行数没有限制,但是excel对数据的行数有一定的限制,因此,csv文件对于导出大量的数据来 ...
随机推荐
- leetcode: 贪心
1. jump game Given an array of non-negative integers, you are initially positioned at the first inde ...
- IOS NSThread 线程间通信
@interface HMViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @im ...
- 动态数组第k小,Poj(1442)
题目链接:http://poj.org/problem?id=1442 本来想复制一下,然后直接sort,结果T了. 在网上看了一下,有用两个队列做的,想了半天,没看懂什么意思.后来模拟一边,总算是懂 ...
- 模拟网页的浏览Stack(POJ1028)
题目链接:http://poj.org/problem?id=1028 注意: 1.用两个栈来模拟,一个用来存可以返回的,一个用来存可以前进的. 2.visit方法,就要将可以前进的栈清空. 3.ba ...
- app上线
不管第一次还是第二次APP上线都需要三样东西:开发者证书,appID,描述文件
- 继承FileInputFormat类来理解 FileInputFormat类
import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.had ...
- _default_ VirtualHost overlap on port 80, the first has precedence
去掉#NameVirtualHost *:80,然后重启httpd
- LeetCode(Two Sum)
一.题目要求 Given an array of integers, return indices of the two numbers such that they add up to a spec ...
- Unexpected token o in JSON at position 1
ajax返回的数据已经是object格式,无需再使用“var newjsonObj = JSON.parse(jsonObj)” 进行转换.
- Linux更改ssh端口号,很easy!
因为公司业务需求,可能涉及到更改ssh远程的端口号,用下面方法轻松解决,废话不多说! 1.打开ssh端口配置文件:vim /etc/ssh/sshd_config,找到如下图所示的端口,改为自己想改的 ...