織夢(mèng)dedecms后臺(tái)文章數(shù)據(jù)導(dǎo)出到excel這個(gè)功能比較實(shí)用的,因?yàn)?,很多公司雖然有網(wǎng)站,但是,公司其它部門(mén)可能還要把公司數(shù)據(jù)導(dǎo)出到紙上面,以便研究公司數(shù)據(jù)之用,所以,很多的公司對(duì)這個(gè)功能就要求使用。
導(dǎo)出全部勾選的文章原理是:
第一步:批量獲取選中的id,這個(gè)是由織夢(mèng)的里面封裝的js實(shí)現(xiàn)的。
第二步:把獲取到的文章id 進(jìn)行處理轉(zhuǎn)換成 字符串 ,例如 1,2,3,5,6
實(shí)現(xiàn)代碼是:
if( !empty($aid) && empty($qstr) ) $qstr = $aid;
if($qstr=='')
{
ShowMsg('參數(shù)無(wú)效!',$ENV_GOBACK_URL);
exit();
}
$qstrs = explode('`',$qstr);
$idstrs = implode(',', $qstrs);
第三步:查詢(xún)要導(dǎo)出的數(shù)據(jù):
sql語(yǔ)句是:Select * From `dede_archives` where id in($idstrs)
也就是說(shuō)這里使用了 sql語(yǔ)言里面 in 來(lái)查詢(xún)表中的某些id。
只要你把上面的三步弄明白了,那么,這個(gè)功能就實(shí)現(xiàn)了。
===============實(shí)現(xiàn)方法=======================
上面那三步只是分析如何實(shí)現(xiàn)的,那里面的代碼不用管,下面是具體的實(shí)現(xiàn)代碼,請(qǐng)跟著一步一步操作。
下載phpexcel類(lèi)庫(kù),官方已經(jīng)搬到這里了:https://github.com/PHPOffice/PHPExcel
1)下載后把Classes文件夾放到 /dede/目錄里面。
2)復(fù)制下面的代碼保存到文件download_excel.php里面,也放到/dede/目錄里面。
測(cè)試:
在瀏覽器里面輸入:localhost/dedecms/dede/download_excel.php?action=allexport&aid=86`87
注意:請(qǐng)把紅色的路徑換上你的域名,上面因?yàn)槲以谧幽夸沝edecms裝的程序,如果你裝在根目錄里面,則去掉這個(gè)dedecms。
86`87:表示把文章id為86和87的文章導(dǎo)出來(lái)。
導(dǎo)出功能代碼:
<?phprequire_once(dirname(__FILE__)."/config.php");if ($action == 'allexport') { //批量獲取文章id www.CUOxin.com織夢(mèng)模板網(wǎng) if( !empty($aid) && empty($qstr) ) $qstr = $aid; if($qstr=='') { ShowMsg('參數(shù)無(wú)效!',$ENV_GOBACK_URL); exit(); } $qstrs = explode('`',$qstr); $idstrs = implode(',', $qstrs); include_once (dirname(__FILE__).'/Classes/PHPExcel.php'); // Create new PHPExcel object $objPHPExcel = new PHPExcel(); $objActSheet = $objPHPExcel->getActiveSheet(); // Set document properties $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")->setLastModifiedBy("Maarten Balliauw")->setTitle("Office 2007 XLSX Test Document")->setSubject("Office 2007 XLSX Test Document")->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")->setKeywords("office 2007 openxml php")->setCategory("Test result file"); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'id') ->setCellValue('B1', '標(biāo)題') ->setCellValue('C1', '發(fā)布時(shí)間') ->setCellValue('D1', '會(huì)員id') ->setCellValue('E1', '欄目id'); $query = "Select * From `dede_archives` where id in($idstrs) "; $dsql->SetQuery($query); $dsql->Execute(); $index = 1; while ($row = $dsql->GetArray()) { $index++; $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A' .$index, $row['id']) ->setCellValue('B' .$index, $row['title']) ->setCellValue('C' .$index, date("Y-m-d",$row['senddate'])) ->setCellValue('D' .$index, $row['mid']) ->setCellValue('E' .$index, $row['typeid']); } // Rename worksheet $objPHPExcel->getActiveSheet()->setTitle('Simple'); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // Redirect output to a client’s web browser (Excel5) header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="list.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory :: createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit;}?> |
結(jié)果:
導(dǎo)出86,87
==============完整版====================
上面只是讓你測(cè)試,不用寫(xiě)做太多事,建議先把上面的測(cè)試成功了,再看下面的, 下面才是真正應(yīng)用到網(wǎng)站里面的。
上面已經(jīng)實(shí)現(xiàn)導(dǎo)出功能,但是,總不能在瀏覽器里面輸入文章id吧。
最終還是通過(guò)后臺(tái)選擇文章,然后,導(dǎo)出。
這個(gè)功能也已經(jīng)實(shí)現(xiàn)了。
在后臺(tái)添加一個(gè)導(dǎo)出文章按扭“導(dǎo)出文檔”。
在content_list.htm里面添加按扭代碼:
<a href="javascript:eportArc(0)"> 導(dǎo)出文檔 </a>
批量獲取id方法:
1. 實(shí)現(xiàn)代碼是通過(guò)在/dede/js/list.js添加一個(gè)批量獲取id函數(shù)。
代碼如下所示:
function eportArc(aid){
var qstr=getCheckboxItem();
if(aid==0) aid = getOneItem();
if(qstr=='')
{
alert('必須選擇一個(gè)或多個(gè)文檔!');
return;
}
location="download_excel.php?aid="+aid+"&action=allexport&qstr="+qstr;
}
2. 注冊(cè)這個(gè)函數(shù)到上下文菜單
即在 函數(shù) function ShowMenu(evt,obj,aid,atitle)里面注冊(cè),代碼是:
new ContextItem("導(dǎo)出的文檔",function(){ eportArc(aid); }),



















