class ExcelAction extends Action {
public function index(){
$this->export_csv();
}
function export_csv() {
$filename = date('YmdHis').".csv";
header("Content-type:text/csv");
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Content-Disposition:attachment;filename=".$filename);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo $this->array_to_string($this->get_export_data());
}
//excel中显示的数据
function array_to_string($result) {
if(empty($result)) {
return "没有符合您要求的数据!^_^";
}
$data = $this->strinput('客户姓名,联系电话,送货地址,电子邮箱,留言信息'."\r\n");
foreach ($result as $v){
$data .= $this->strinput($v['name'].','.$v['tel'].','.$v['address'].','.$v['zipcode'].','.$v['message']."\r\n");
}
return $data;
}
//查询数据
function get_export_data() {
$Dao = M('orders');
//获取当前月有多少天
$moth = date('t');
var_dump(strtotime(2014-09-07));
//一周内数据
if($_GET['monthtime']==1){
$time =time()-604800;
$where = 'createtime >'.$time;
}
//一个月内数据
elseif($_GET['monthtime']==2){
if($moth ==28){
$time =time()-2419200;
$where = 'createtime >'.$time;
}
if($moth ==29){
$time =time()-2505600;
$where = 'createtime >'.$time;
}
if($moth ==30){
$time =time()-2592000;
$where = 'createtime >'.$time;
}
if($moth ==31){
$time =time()-2678400;
$where = 'createtime >'.$time;
}
}
//三个月内的数据
elseif($_GET['monthtime']==3){
// $starttime=mktime(0,0,0,date("m")-3,1,date("Y"));
// $time = (date('m',time())-$starttime)*24*60*60;
$time = time()- 7776000;
$where = 'createtime>'.$time;
}
//半年内的数据
elseif ($_GET['monthtime']==4){
// $starttime=mktime(0,0,0,date("m")-6,1,date("Y"));
// $time = (date('m',time())-$starttime)*24*60*60;
$time = time()- 15552000;
$where = 'createtime>'.$time;
}
//自定义时间段
elseif ($_GET['monthtime']==5){
$begintime = strtotime($_GET['begintime']);
$lasttime = strtotime($_GET['lasttime']);
$where = "createtime between ' ".$begintime."'and ' ".$lasttime." ' ";
}
$res = $Dao->where($where)->select();
return $res;
}
function strinput($strInput) {
// return $strInput;
return iconv("UTF-8","GB2312",$strInput) ;//页面编码为utf-8时使用,否则导出的中文为乱码
}
}
====================================================================================================
上面是ExcelAction方法
下面是页面中导出和单选按钮
====================================================================================================
页面导出按钮
<form action ="__APP__/Excel/index" name="form2">
<input type="radio" name="monthtime" id="export_time" value =1 checked="checked" />一周内
<input type="radio" name="monthtime" id="export_oy" value =2 />一个月内
<input type="radio" name="monthtime" id="export_ty" value =3 />三个月内
<input type="radio" name="monthtime" id="export_yy" value =4 />半年内
<input type="radio" name="monthtime" id = "custom" value =5 />自定义
<input class="Wdate" type="text" name="begintime" id="begintime" onClick="WdatePicker()"> <span id="spand" style=display:none >到</span>
<input class="Wdate" type="text" name="lasttime" id="lasttime" onClick="WdatePicker()">
<input type='submit' name ="sub" id="sub" value='导出'/>
</form>