今天帮朋友给织梦后台添加数据导出的功能,看到网上很多分享的方法还需要定义字段,感觉比较麻烦,最终找到了这种一劳永逸的方法,支持所有的表单导出而无需定义字段。演示效果如下:
修改步骤:
1、打开\dede\templets\diy_main.htm 找到“前台预览”,在下面加上:
- <a href="diy_list.php?action=excel&diyid={dede:field.diyid/}" target="_blank">导出Excel</a>
2、打开\dede\diy_list.php,修改第14行:
- $action = isset($action) && in_array($action, array('post', 'list', 'edit', 'check', 'delete','excel')) ? $action : '';
同样在这个文件中找到如下代码:
- else{
- showmsg('未定义操作', "-1");
- }
在代码上边添加:
- else if($action == 'excel')
- {
- header("Content-type:application/vnd.ms-excel");
- header("Content-Disposition:attachment;filename={$diy->name}_".date("Y-m-d").".xls");
- $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>";
- }