Dwz与thinkphp整合下的数据导出到Excel实例
程序员文章站
2022-07-12 07:50:39
本文实例讲述了dwz与thinkphp整合下的数据导出到excel的方法。分享给大家供大家参考。具体方法如下:
这个问题困扰了我很久,终于还是自己写jquery把它搞掂了...
本文实例讲述了dwz与thinkphp整合下的数据导出到excel的方法。分享给大家供大家参考。具体方法如下:
这个问题困扰了我很久,终于还是自己写jquery把它搞掂了,希望能对遇到同样问题的人有所帮助,好吧,下面就是源代码:
jquery代码如下:
复制代码 代码如下:
$(function(){
//_getids方法就是获取已选的要导出的数据的id,设了两个参数selectedids--已选id的input名称,targettype-->碎片模式,默认是navtab,如果是dialog的自行修改
function _getids(selectedids,targettype){
var ids="";
var $box=targettype=="dialog"?$.pdialog.getcurrent():navtab.getcurrentpanel();
$box.find("input:checked").filter("[name='"+selectedids+"']").each(function(i){
var val=$(this).val();
ids+=i==0?val:","+val;});
return ids;
}
//导出excel的按钮 class=iconn,click时触发
$(".iconn").click(
function(){
//因为这个方法直运用到导出到excel 所以这里所需的参数变量我直接写死了
var targettype="navtab";
var selectedids="ids";
var posttype="string";//批量选择的话,以文本的形式获取
var ids=_getids(selectedids,targettype);
if(!ids){
alertmsg.error($(".iconn").attr("warn"));
return false;
//alert("请选择要导出的数据!");
}else{
//将获取到的ids 传给后台处理
window.open("__url__/memberexport/ids/"+ids);
}
});
});
//_getids方法就是获取已选的要导出的数据的id,设了两个参数selectedids--已选id的input名称,targettype-->碎片模式,默认是navtab,如果是dialog的自行修改
function _getids(selectedids,targettype){
var ids="";
var $box=targettype=="dialog"?$.pdialog.getcurrent():navtab.getcurrentpanel();
$box.find("input:checked").filter("[name='"+selectedids+"']").each(function(i){
var val=$(this).val();
ids+=i==0?val:","+val;});
return ids;
}
//导出excel的按钮 class=iconn,click时触发
$(".iconn").click(
function(){
//因为这个方法直运用到导出到excel 所以这里所需的参数变量我直接写死了
var targettype="navtab";
var selectedids="ids";
var posttype="string";//批量选择的话,以文本的形式获取
var ids=_getids(selectedids,targettype);
if(!ids){
alertmsg.error($(".iconn").attr("warn"));
return false;
//alert("请选择要导出的数据!");
}else{
//将获取到的ids 传给后台处理
window.open("__url__/memberexport/ids/"+ids);
}
});
});
php代码如下:
复制代码 代码如下:
//member成员信息导出到excel
public function memberexport(){
$id=$_request['ids'];//获取已选数据的id
//echo $id;
//在这里导出到excel我没有用到phpexcel,是自己写的,很简单,相信大家都明白是什么意思,根据自己需要自行修改就行了
$filename=date('y-m-d');
header("pragma:public");
header("expires:0");
header("cache-control:must-revalidate,post-check=0,pre-check=0");
header("content-type:application/force-download");
header("content-type:application/vnd.ms-execl;charset=gb2312");
header("content-type:application/octet-stream");
header("content-type:application/download");
header('content-disposition:attachment;filename="'.$filename.'.xls"');
header("content-transfer-encoding:binary");
if(!$id){
$this->error('非法操作!');
}else{
$map['id']=array('in',$id);
$title="用户名\t 姓名\t 部门\t 职务\t 身份证\t 职称\t 学历\t 毕业时间\t 录用时间";
$title=iconv('utf-8','gbk',$title);
echo $title;
set_time_limit(0);
$offset= 0;
$length=100;
$member = m('member');
$list=$member->where($map)->order('id desc')->select();
if(!$list){
$this->error('操作错误!');
}else{
foreach($list as $key=>$row){
echo "\n";
echo iconv('utf-8','gbk',$row['username'])."\t";
echo iconv('utf-8','gbk',$row['name'])."\t";
echo iconv('utf-8','gbk',$row['department'])."\t";
echo iconv('utf-8','gbk',$row['zhiwu'])."\t";
echo iconv('utf-8','gbk',$row['sfz'])."\t";
echo iconv('utf-8','gbk',$row['zhicheng'])."\t";
echo iconv('utf-8','gbk',$row['xueli'])."\t";
echo iconv('utf-8','gbk',$row['bytime'])."\t";
echo iconv('utf-8','gbk',$row['lytime'])."\t";
}
$offset+=$length;
}
}
}
public function memberexport(){
$id=$_request['ids'];//获取已选数据的id
//echo $id;
//在这里导出到excel我没有用到phpexcel,是自己写的,很简单,相信大家都明白是什么意思,根据自己需要自行修改就行了
$filename=date('y-m-d');
header("pragma:public");
header("expires:0");
header("cache-control:must-revalidate,post-check=0,pre-check=0");
header("content-type:application/force-download");
header("content-type:application/vnd.ms-execl;charset=gb2312");
header("content-type:application/octet-stream");
header("content-type:application/download");
header('content-disposition:attachment;filename="'.$filename.'.xls"');
header("content-transfer-encoding:binary");
if(!$id){
$this->error('非法操作!');
}else{
$map['id']=array('in',$id);
$title="用户名\t 姓名\t 部门\t 职务\t 身份证\t 职称\t 学历\t 毕业时间\t 录用时间";
$title=iconv('utf-8','gbk',$title);
echo $title;
set_time_limit(0);
$offset= 0;
$length=100;
$member = m('member');
$list=$member->where($map)->order('id desc')->select();
if(!$list){
$this->error('操作错误!');
}else{
foreach($list as $key=>$row){
echo "\n";
echo iconv('utf-8','gbk',$row['username'])."\t";
echo iconv('utf-8','gbk',$row['name'])."\t";
echo iconv('utf-8','gbk',$row['department'])."\t";
echo iconv('utf-8','gbk',$row['zhiwu'])."\t";
echo iconv('utf-8','gbk',$row['sfz'])."\t";
echo iconv('utf-8','gbk',$row['zhicheng'])."\t";
echo iconv('utf-8','gbk',$row['xueli'])."\t";
echo iconv('utf-8','gbk',$row['bytime'])."\t";
echo iconv('utf-8','gbk',$row['lytime'])."\t";
}
$offset+=$length;
}
}
}
希望本文所述对大家基于thinkphp框架的php程序设计有所帮助。