dede自定义表单导出EXCEL表格

方法一:

 

后台找到/dede/templets/diy_main.htm

查找:

 

<a href="../plus/diy.php?action=list&diyid={dede:field.diyid/}" target="_blank"><img src='images/gtk-tmp.png' title='预览' alt='预览' />前台预览</a>


在后面加上:

 

&nbsp;|&nbsp;<a href='diy_list.php?action=excel&diyid={dede:field.diyid/}' target="_blank">导出表单Excel</a>


修改/dede/diy_list.php,在其中搜索:

 

$action = isset($action) && in_array($action, array('post', 'list', 'edit', 'check', 'delete')) ? $action : '';

修改为:


 

$action = isset($action) && in_array($action, array('post', 'list', 'edit', 'check', 'delete','excel')) ? $action : '';


再找到:


 

else{ showmsg('未定义操作', "-1");}


在它前面添加:


elseif($action == 'excel')
{   
    ob_end_clean();//清除缓冲区,避免乱码
    header("Content-type:application/vnd.ms-excel");
    header("Content-Disposition:attachment;filename={$diy->name}_".date("Y-m-d").".xls");
    print(chr(0xEF).chr(0xBB).chr(0xBF));//清除bom
    $fieldlist = $diy->getFieldList();
    echo "<table><tr>";
    foreach($fieldlist as $field=>$fielddata)
    {
        echo "<th>{$fielddata[0]}</th>";
    }
    echo "<th>状态</th>";
    echo "</tr>";
    $sql = "SELECT * FROM {$diy->table} ORDER BY id DESC";
    $dsql->SetQuery($sql);
    $dsql->Execute('t');
    while($arr = $dsql->GetArray('t'))
    {
        echo "<tr>";
        foreach($fieldlist as $key => $field)
        {
            echo "<td>".$arr[$key]."</td>";
        }
    $status = $arr['ifcheck'] == 1 ? '已审核' : '未审核';
    echo "<td>".$status."</td>";
    echo "</tr>";
    }
    echo "</table>";
}

 

 

 

方法二,较麻烦,未亲测:

1.首先在后台找到/dede/templets/diy_main.htm,查找:


<a href="../plus/diy.php?action=list&diyid={dede:field.diyid/}" target="_blank"><img src='images/gtk-tmp.png' title='预览' alt='预览' />前台预览</a>
在后面加上:
&nbsp;|&nbsp;<a href="../plus/diy.php?action=daochu&diyid={dede:field.diyid/}" target="_blank">导出为EXCEL</a>

 

2、核心内容修改 plus/diy.php


$action = isset($action) && in_array($action, array('post', 'list', 'view')) ? $action : 'post';
替换成:
$action = isset($action) && in_array($action, array('post', 'list', 'view', 'daochu')) ? $action : 'post';

 

3、再在plus/diy.php最后一行下面新加代码:

 

else if($action == 'daochu')
{
ob_end_clean();//清除缓冲区,避免乱码
header("Content-type:application/vnd.ms-excel;");
Header("Content-Disposition:attachment;filename={$diy->table}_".date("Y-m-d").".xls");
print(chr(0xEF).chr(0xBB).chr(0xBF));//清除bom
$query = "desc `{$diy->table}`";
$res = mysql_query($query);
echo "<table><tr>";
//导出表头(也就是表中拥有的字段)
while($row = mysql_fetch_array($res)){
  $t_field[] = $row['Field']; //Field中的F要大写,否则没有结果
/*   echo "<th>".$row['Field']."</th>"; */
        if($row['Field']=='id'){
            echo "<th>ID</th>";
        }elseif($row['Field']=='gsm'){
            echo "<th>公司名</th>";
        }elseif($row['Field']=='xm'){
            echo "<th>姓名</th>";
        }elseif($row['Field']=='sj'){
            echo "<th>手机号码</th>";
        }elseif($row['Field']=='yx'){
            echo "<th>电子邮箱</th>";
        }else{
            echo "<th>  </th>";
        }
}
echo "</tr>";
//导出数据
$sql = "select * from `{$diy->table}`";
$res = mysql_query($sql);
while($row = mysql_fetch_array($res)){
  echo "<tr>";
    foreach($t_field as $f_key){
        echo "<td>".$row[$f_key]."</td>";
    }
  echo "</tr>";
}
echo "</table>";
}

 

 

原文地址:https://blog.csdn.net/jklgfgdsr/article/details/79311950