导出csv文件代码示例
//当数据量达到一定级别后(大概60000以上),excel会溢出,不能全部显示,而新版的csv好像不会出现这个问题。为什么用好像,我也是听别人说,暂时没有去验证。
<?php
$sql = " ";
$link = mysqli_connect('127.0.0.1', '账号', '密码','库');
mysqli_query($link,"SET NAMES 'UTF8'");
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysqli_query($link,$sql);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$arr = array(
'A' => '姓名',
'B' => '年龄',
);
$data = "name";
foreach($arr as $value)
{
$data .= ",".$value;
}
$data .= "\n";
//$filename = DOCROOT."download/detail/".date('Y-m-d')."-".User::active_user()->id."detail.csv";
$i=0;
while($row = mysqli_fetch_row($result))
{
$i++;
$data .= $i.",".$row[1]",".$row[2].","."\n";
}
mysqli_close($link);
ob_end_flush();
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=".date('Y-m-d')."文件名.csv"); //“生成文件名称”=自定义
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo $data;
exit;