DEDE自定义表单导出功能的实现详细步骤

DEDE自定义表单导出功能的实现详细步骤,这个代码是通用的!加完后  你所有的自定义表单 都可以导出!


步骤:
1.打开 /dede/templets/diy_main.htm   大概在第49行的地方加上
<a href="../plus/diy.php?action=push&diyid={dede:field.diyid/}" target="_blank">导出</a>
意思就是加个导出按钮。

2.打开 /plus/diy.php   在最下面加入以下代码
else if($action == 'push')
{
  $row = $dsql->GetOne("Select * From dede_diyforms where diyid='$diyid'");
  $ds = file(('/dede/inc/fieldtype.txt'));
foreach($ds as $d){
$dds = explode(',',trim($d));
$fieldtypes[$dds[0]] = $dds[1];
}
$fieldset = $row['info'];
$dtp = new DedeTagParse();
$dtp->SetNameSpace("field","<",">");
$dtp->LoadSource($fieldset);
$titleArray=array();
$titleName=array();
if(is_array($dtp->CTags)){
foreach($dtp->CTags as $ctag)
{
$itname = $ctag->GetAtt('itemname');
if($itname=='') echo "没指定";
$titleArray[] = $itname;
$titleName[]= $ctag->GetTagName();
}
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename=demo.xls');
header('Pragma: no-cache');
header('Expires: 0');
$title =$titleArray;
include_once DEDEINC.'/datalistcp.class.php';
    if($diy->public == 2)
        $query = "SELECT * FROM `{$diy->table}` ORDER BY id DESC";
    else
        $query = "SELECT * FROM `{$diy->table}` WHERE ifcheck=1 ORDER BY id DESC";

    $datalist = new DataListCP();
    $datalist->pageSize = 10;
    $datalist->SetParameter('action', 'list');
    $datalist->SetParameter('diyid', $diyid);
    $datalist->SetSource($query);   
$rsArray = array();
$datalist->dsql->Execute('dlist',$datalist->sourceSql);
        $i = 0;
        while($arr=$datalist->dsql->GetArray('dlist'))
        {
        $i++;  
        $object = array();
        foreach ($titleName as $va){
        $object[]=$arr[$va];
        }
            $rsArray[$i] = $object;
           
        }
$data = $rsArray;
echo iconv('utf-8', 'gbk', implode("\t", $title)), "\n";
foreach ($data as $value) {
echo iconv('utf-8', 'gbk', implode("\t", $value)), "\n";
}
exit;
}



就OK了。


思路很简单,先在列表页搞个可以触发导出的按钮,然后把需要导出的表头和内容查出来,然后以execle输出就好了。