织梦dedecms后台文章数据导出到excel这个功能比较实用的,因为,很多公司虽然有网站,但是,公司其它部门可能还要把公司数据导出到纸上面,以便研究公司数据之用,所以,很多的公司对这个功能就要求使用。
导出全部勾选的文章原理是:
第一步:批量获取选中的id,这个是由织梦的里面封装的js实现的。
第二步:把获取到的文章id 进行处理转换成 字符串 ,例如 1,2,3,5,6
实现代码是:
if( !empty($aid) && empty($qstr) ) $qstr = $aid;
if($qstr=='')
{
ShowMsg('参数无效!',$ENV_GOBACK_URL);
exit();
}
$qstrs = explode('`',$qstr);
$idstrs = implode(',', $qstrs);
第三步:查询要导出的数据:
sql语句是:Select * From `dede_archives` where id in($idstrs)
也就是说这里使用了 sql语言里面 in 来查询表中的某些id。
只要你把上面的三步弄明白了,那么,这个功能就实现了。
===============实现方法=======================
上面那三步只是分析如何实现的,那里面的代码不用管,下面是具体的实现代码,请跟着一步一步操作。
下载phpexcel类库,官方已经搬到这里了:https://github.com/PHPOffice/PHPExcel
1)下载后把Classes文件夹放到 /dede/目录里面。
2)复制下面的代码保存到文件download_excel.php里面,也放到/dede/目录里面。
测试:
在浏览器里面输入:localhost/dedecms/dede/download_excel.php?action=allexport&aid=86`87
注意:请把红色的路径换上你的域名,上面因为我在子目录dedecms装的程序,如果你装在根目录里面,则去掉这个dedecms。
86`87:表示把文章id为86和87的文章导出来。
导出功能代码:
02 |
require_once( dirname (__FILE__). "/config.php" ); |
03 |
if ($action == 'allexport' ) { |
04 |
//批量获取文章 id www.adminbuy.cn 织梦模版网 |
05 |
if ( !empty($aid) && empty($qstr) ) $qstr = $aid; |
09 |
ShowMsg( '参数无效!' ,$ENV_GOBACK_URL); |
12 |
$qstrs = explode( '`' ,$qstr); |
13 |
$idstrs = implode( ',' , $qstrs); |
14 |
include_once ( dirname (__FILE__). '/Classes/PHPExcel.php' ); |
15 |
// Create new PHPExcel object |
16 |
$objPHPExcel = new PHPExcel(); |
17 |
$objActSheet = $objPHPExcel->getActiveSheet(); |
18 |
// Set document properties |
19 |
$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" ); |
20 |
$objPHPExcel->setActiveSheetIndex(0) |
21 |
->setCellValue( 'A1' , 'id' ) |
22 |
->setCellValue( 'B1' , '标题' ) |
23 |
->setCellValue( 'C1' , '发布时间' ) |
24 |
->setCellValue( 'D1' , '会员id' ) |
25 |
->setCellValue( 'E1' , '栏目id' ); |
26 |
$query = "Select * From `dede_archives` where id in($idstrs) " ; |
27 |
$dsql->SetQuery($query); |
30 |
while ($row = $dsql->GetArray()) { |
32 |
$objPHPExcel->setActiveSheetIndex(0) |
33 |
->setCellValue( 'A' .$index, $row[ 'id' ]) |
34 |
->setCellValue( 'B' .$index, $row[ 'title' ]) |
35 |
->setCellValue( 'C' .$index, date ( "Y-m-d" ,$row[ 'senddate' ])) |
36 |
->setCellValue( 'D' .$index, $row[ 'mid' ]) |
37 |
->setCellValue( 'E' .$index, $row[ 'typeid' ]); |
40 |
$objPHPExcel->getActiveSheet()->setTitle( 'Simple' ); |
41 |
// Set active sheet index to the first sheet, so Excel opens this as the first sheet |
42 |
$objPHPExcel->setActiveSheetIndex(0); |
43 |
// Redirect output to a client’s web browser (Excel5) |
44 |
header( 'Content-Type: application/vnd.ms-excel' ); |
45 |
header( 'Content-Disposition: attachment;filename="list.xls"' ); |
46 |
header( 'Cache-Control: max-age=0' ); |
47 |
$objWriter = PHPExcel_IOFactory :: createWriter($objPHPExcel, 'Excel5' ); |
48 |
$objWriter->save( 'php://output' ); |
|
结果:
导出86,87
==============完整版====================
上面只是让你测试,不用写做太多事,建议先把上面的测试成功了,再看下面的, 下面才是真正应用到网站里面的。
上面已经实现导出功能,但是,总不能在浏览器里面输入文章id吧。
最终还是通过后台选择文章,然后,导出。
这个功能也已经实现了。
在后台添加一个导出文章按扭“导出文档”。
在content_list.htm里面添加按扭代码:
<a href="javascript:eportArc(0)"> 导出文档 </a>
批量获取id方法:
1. 实现代码是通过在/dede/js/list.js添加一个批量获取id函数。
代码如下所示:
function eportArc(aid){
var qstr=getCheckboxItem();
if(aid==0) aid = getOneItem();
if(qstr=='')
{
alert('必须选择一个或多个文档!');
return;
}
location="download_excel.php?aid="+aid+"&action=allexport&qstr="+qstr;
}
2. 注册这个函数到上下文菜单
即在 函数 function ShowMenu(evt,obj,aid,atitle)里面注册,代码是:
new ContextItem("导出的文档",function(){ eportArc(aid); }),
转载请标注:东东工作室——织梦dedecms后台文章数据导出到excel教程