天天看點

織夢自定義表單導出EXCEL的方法(整理)

【1.第一種方法】

1、首先在背景找到/dede/templets/diy_main.htm,查找:

<a href="../plus/diy.php?action=list&diyid={dede:field.diyid/}" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  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>
           

2、修改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>";
}
           

==========================================================================

【2. 這種方法比較麻煩,已棄用】

1、首先在背景找到/dede/templets/diy_main.htm,查找:

<a href="../plus/diy.php?action=list&diyid={dede:field.diyid/}" target="_blank" rel="external nofollow"  target="_blank" rel="external nofollow"  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" rel="external nofollow"  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>";
}
           

繼續閱讀