// 输出Excel文件头,可把news.csv换成你要的文件名 |
header( 'Content-Type: application/vnd.ms-excel' ); |
header( 'Content-Disposition: attachment;filename="news.csv"' ); |
header( 'Cache-Control: max-age=0' ); |
// 打开PHP文件句柄,php://output 表示直接输出到浏览器 |
$newfp = fopen ( 'php://output' , 'a' ); |
// 输出Excel列名信息 |
$head = array ( 'ID' , '标题' , '描述' , '名字' ); |
$arr = array (); |
foreach ( $head as $i => $v ){ |
// CSV的Excel支持GBK编码,一定要转换,否则乱码 |
$head [ $i ] = iconv( 'utf-8' , 'gbk' , $v ); |
$arr []= $v ; |
} |
// 将数据通过fputcsv写到文件句柄 |
fputcsv ( $newfp , $arr ); |
// 计数器 |
$cnt = 0; |
// 每隔$limit行,刷新一下输出buffer |
$limit = 100000; |
// 逐行取出数据,不浪费内存$row = $spf_db->fetch_next() |
$sql = 'select id,title,description,username from `v9_news`' ; |
$res = $this ->news_db->query( $sql ); |
$rowinfo = array (); |
while ( $row = $this ->news_db->fetch_next()){ |
$cnt ++; |
if ( $limit == $cnt ) { |
ob_flush(); |
flush (); |
$cnt = 0; |
} |
// foreach($row as $i => $v){ |
// $row[$i] = iconv('utf-8','gbk',$v); |
// } |
fputcsv ( $newfp , $row ); |
} |