Dwz与thinkphp整合下的数据导出到Excel实例_PHP
这个问题困扰了我很久,终于还是自己写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);
}
});
});
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;
}
}
}
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。