**
* 导出
*/
function download_exit() {
$mod=new Mysqli_model();
$mod->linkmysql(DB_K_WEIXIN_WAP);
//获取当前时间的18岁
$sql='SELECT uid,mreal_idcard,YEAR (NOW()) - substring(idcard, 7, 4) as n,substring(idcard, 11,2) as month FROM ims_mc_members where mreal_mobile != "" and mreal_idcard != ""'
. ' HAVING n < 18';
$dataResult=$mod->query($sql);
if (empty($dataResult)) {
$this->load->view('page/ok', '没有数据可导出');
die;
}
foreach ($dataResult as $key => $val) {
}
$title = '导出页';
$headtitle='导出页';
// $headTitle = '导出页';
// $headtitle = "<tr><th colspan='7'>{$headTitle}</th></tr>";
$titlename = "<tr>
<th>UID</th>
<th>身份证号</th>
<th>年龄</th>
<th>月份</th>
</tr>";
$filename = $title . ".xls";
$this->excelData($dataResult, $titlename, $headtitle, $filename);
}
/*
* 处理Excel导出
* @param $datas array 设置表格数据
* @param $titlename string 设置head
* @param $title string 设置表头
*/
public function excelData($datas, $titlename, $title, $filename) {
$str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>";
$str .="<table border=1>" . $titlename . "";
$str .= $title;
foreach ($datas as $key => $rt) {
$str .= "<tr>";
foreach ($rt as $k => $v) {
$str .= "<td>{$v}</td>";
}
$str .= "</tr>\n";
}
$str .= "</table></body></html>
function Uploads() {
$data = I();
$mod = M("label");
$dataResult = array(); //todo:导出数据(自行设置)
$res = M($this->mod)->find($data['logid']);
if (empty($res)) {
$this->error("没有数据可导出");
die;
}
$dataResult = $mod->where("logid = {$data['logid']}")->field("id,addtime,lname,lcode,money,starttime,endtime,state,employ")->select();
foreach ($dataResult as $key => $val) {
$dataResult[$key]['addtime']=date("Y-m-d H:i",$val['addtime']);
$dataResult[$key]['starttime']=date("Y-m-d H:i",$val['starttime']);
$dataResult[$key]['endtime']=date("Y-m-d H:i",$val['endtime']);
} $headTitle = $res['lname'];
$title = $res['lname'];
$headtitle='';
// $headtitle = "<tr><th colspan='7'>{$headTitle}</th></tr>";
$titlename = "<tr>
<th>ID</th>
<th>添加时间</th>
<th>标签名称</th>
<th>标签代码</th>
<th>代表金额</th>
<th>有效期开始时间</th>
<th>有效期结束时间</th>
<th>是否启用</th>
<th>是否使用</th>
</tr>";
$filename = $title . ".xls";
$this->excelData($dataResult, $titlename, $headtitle, $filename);
}
/*
* 处理Excel导出
* @param $datas array 设置表格数据
* @param $titlename string 设置head
* @param $title string 设置表头
*/
public function excelData($datas, $titlename, $title, $filename) {
$str = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\nxmlns:x=\"urn:schemas-microsoft-com:office:excel\"\r\nxmlns=\"http://www.w3.org/TR/REC-html40\">\r\n<head>\r\n<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">\r\n</head>\r\n<body>";
$str .="<table border=1>" . $titlename . "";
$str .= $title;
foreach ($datas as $key => $rt) {
$str .= "<tr>";
foreach ($rt as $k => $v) {
$str .= "<td>{$v}</td>";
}
$str .= "</tr>\n";
}
$str .= "</table></body></html>";
header("Content-Type: application/vnd.ms-excel; name='excel'");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . $filename);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Expires: 0");
exit($str);
}
总结:
1.查找数据
2.设置表头列
3.调用方法浏览器输出