本文實例講述了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);
}
});
});
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程序設計有所幫助。