21,886
社区成员
发帖
与我相关
我的任务
分享
<?php
header('Content-Type:application/vnd.ms-excel;charset=utf-8');
header('Content-Disposition: attachment;filename="shuju.csv"');
header('Cache-Control:max-age=0');
date_default_timezone_set (PRC);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>
<body>
<?php
$date1=$_POST['date1'];
$date2=$_POST['date2'];
@$con = mysql_connect("localhost","root","mysql");
mysql_query('set names utf8');
if (!$con){
die("数据库链接失败:".mysql_error());
exit;
}
$data = mysql_select_db("BERecord",$con);
if(!$data){
echo mysql_error();
mysql_close($con); //关闭数据库链接
exit;
}
else{
//$sql = "select * from record";
$sql = "select * from record where searchtime between '$date1' and '$date2'";
$result = mysql_query($sql,$con);
$fp = fopen("php://output","a");
$head = array("1","2");
foreach($head as $i => $v){
// $head[$i] = iconv('utf-8','gb2312',$v);
$head[$i] = mb_convert_encoding("$v","UTF-8","gb2312");
}
fputcsv($fp,$head);
while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
fputcsv($fp,$row);
echo "<br>";
}
mysql_close($con);
}
?>
</body>
</html>
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename");
echo $excel_content;
<?php
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=test.xls");
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen("data.txt", 'w+');
foreach ($list as $fields) {
fputcsv($fp, $fields, "\t");
}
fseek($fp, 0, SEEK_SET);
echo fread($fp, filesize('data.txt'));
fclose($fp);
?>