Here is the method of using fputcsv write a CSV file, directly to the browser output Excel file.

//output Excel file header, but the user. The CSV for the file name you want
header (' the content-type: application/VND. Ms Excel - ');
header (' Content - Disposition: attachment; filename="user. CSV" ');
header (" cache-control: Max - age=0 ');

//retrieve data from the database, in order to save memory, don't put the data at once read into memory, relative clauses, handle the line can be read
$SQL='select * from TBL where... ';
$STMT=$db - >The query ($SQL);

//open the PHP file handle, PHP://output said direct output to the browser
$fp=fopen (' PHP://output ', 'a');

$//output Excel column information head=array (' name ', 'gender', 'age' and 'Email' and 'phone', '... ');
foreach ($head as $I=>$v) {
//CSV Excel support GBK code, must transform, otherwise the code
$head [$I]=iconv (' utf-8 ', 'GBK, $v);

//the data through the fputcsv write file handle
fputcsv ($fp, $head);

//counter $CNT=0;
//every $limit line, refresh the output buffer, not too big, not too small.

//remove the data line by line, not a waste of memory
the while ($row=$STMT - >the fetch (Zend_Db: : FETCH_NUM)) {

$CNT + +;
if $limit==$(CNT) {//refresh the output buffer, to prevent the problem caused by too much data
ob_flush ();
flush ();

foreach ($row as $I=>$v) {
$row ($I)=iconv (' utf-8 ', 'GBK, $v);

} fputcsv ($fp, $row);

advantages of simple and easy to use, very save memory, do not rely on third-party libraries.


  • 通过table标签,PHP输出EXCEL的实现方法
  • 基于php导出到Excel或CSV的详解(附utf8、gbk 编码转换)
  • 解析PHPExcel使用的常用说明以及把PHPExcel整合进CI框架的介绍
  • php 备份数据库代码(生成word,excel,json,xml,sql)
  • 基于PHPExcel的常用方法总结
  • 基于PHP导出Excel的小经验 完美解决乱码问题
  • PHP导出EXCEL快速开发指南--PHPEXCEL的使用详解
  • PHP处理excel cvs表格的方法实例介绍
  • phpExcel导出大量数据出现内存溢出错误的解决方法
  • php中导出数据到excel时数字变为科学计数的解决方法
  • php判断上传的Excel文件中是否有图片及PHPExcel库认识
  • PHPEXCEL 使用小记
  • phpexcel导出excel的颜色和网页中的颜色显示不一致
  • php读取EXCEL文件 php excelreader读取excel文件
  • php excel reader读取excel内容存入数据库实现代码
  • PHPExcel读取Excel文件的实现代码
  • ThinkPHP与PHPExcel冲突解决方法
  • 基于php中使用excel的简单介绍

This concludes the body part