我这个是一张页面2个导出按钮,2个都是按照相同的“时间”关键字。现在导出的excel文件名都是相同的。
要求实现, 为2个按钮导出的excel设置不同的文件名,并且把他们的关键字写在导出的文件名后。
比如查询条件是 “从2009-01-01到2009-02-02”文件名为jifen
那么导出的excel文件名为 “jifen-2009_01_01-2009_02_02”
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=test_data.xls_$start_jf_$end_jf");
header("Content-type: text/html; charset=utf8");
require_once("lianjie.php");
$type = @$_GET['type'];
$start_jf = $_GET['start_jf'];
$end_jf = $_GET['end_jf'];
if($type =='f') //积分
{
$sql="select order_id, person , date, record,res  from gift_order_record where `date` >=\"$start_jf\" and `date` < \"$end_jf\" ";
echo   iconv('utf-8','gb2312',"订单号")."\t";
echo   iconv('utf-8','gb2312',"操作人员")."\t";
echo   iconv('utf-8','gb2312',"时间")."\t";
echo   iconv('utf-8','gb2312',"记录")."\t";
echo   iconv('utf-8','gb2312',"备注")."\t";
echo   "\n";
$res= mysql_query($sql);
while ($row =mysql_fetch_array($res)) {
echo   iconv('utf-8','gb2312',$row['order_id'])."\t";
echo   iconv('utf-8','gb2312',$row['person'])."\t";
echo   iconv('utf-8','gb2312',$row['date'])."\t";
echo   iconv('utf-8','gb2312',$row['record'])."\t";
echo   iconv('utf-8','gb2312',$row['res'])."\t";
echo   "\n";
}
}
else if($type == 'l') //礼品
{
$sql = "select a.gift_id, b.name, count( a.gift_id )as count_id FROM gift_order AS a INNER JOIN gift AS b ON a.gift_id = b.gift_id
   where a.order_state_id in (3, 6) and a.check_date >= '$start_jf' and a.check_date < '$end_jf' group by a.gift_id";
    //echo $sql;
echo   iconv('utf-8','gb2312',"编号")."\t";
echo   iconv('utf-8','gb2312',"名称")."\t";
echo   iconv('utf-8','gb2312',"数量")."\t";
echo   "\n";
$result=mysql_query($sql);
while ($row=mysql_fetch_array($result)){
echo   iconv('utf-8','gb2312',$row['gift_id'])."\t";
echo   iconv('utf-8','gb2312',$row['name'])."\t";
echo   iconv('utf-8','gb2312',$row['count_id'])."\t";
echo   "\n";
}
}

解决方案 »

  1.   

    require_once("lianjie.php");
    //先获取开始结束时间
    $start_jf = $_GET['start_jf'];
    $end_jf = $_GET['end_jf'];
    header("Content-type:application/vnd.ms-excel");
    //根据时间设置下载文件名
    header("Content-Disposition:attachment;filename=jifen-".$start_jf."-".$end_jf.".xls");
    header("Content-type: text/html; charset=utf8");
    $type = @$_GET['type'];
    if($type =='f')        //积分
    {
        $sql="select order_id, person , date, record,res  from gift_order_record where `date` >=\"$start_jf\" and `date` < \"$end_jf\" ";
        echo   iconv('utf-8','gb2312',"订单号")."\t";
        echo   iconv('utf-8','gb2312',"操作人员")."\t";
        echo   iconv('utf-8','gb2312',"时间")."\t";
        echo   iconv('utf-8','gb2312',"记录")."\t";
        echo   iconv('utf-8','gb2312',"备注")."\t";
        echo   "\n";    
        $res= mysql_query($sql);
        while ($row =mysql_fetch_array($res)) {
            echo   iconv('utf-8','gb2312',$row['order_id'])."\t";
            echo   iconv('utf-8','gb2312',$row['person'])."\t";
            echo   iconv('utf-8','gb2312',$row['date'])."\t";
            echo   iconv('utf-8','gb2312',$row['record'])."\t";
            echo   iconv('utf-8','gb2312',$row['res'])."\t";
            echo   "\n";
        }
    }
    else if($type == 'l')        //礼品
    {
        $sql = "select a.gift_id, b.name, count( a.gift_id )as count_id FROM gift_order AS a INNER JOIN gift AS b ON a.gift_id = b.gift_id
       where a.order_state_id in (3, 6) and a.check_date >= '$start_jf' and a.check_date < '$end_jf' group by a.gift_id";
        //echo $sql;
        echo   iconv('utf-8','gb2312',"编号")."\t";
        echo   iconv('utf-8','gb2312',"名称")."\t";
        echo   iconv('utf-8','gb2312',"数量")."\t";
        echo   "\n";    
        $result=mysql_query($sql);
        while ($row=mysql_fetch_array($result)){
            echo   iconv('utf-8','gb2312',$row['gift_id'])."\t";
            echo   iconv('utf-8','gb2312',$row['name'])."\t";
            echo   iconv('utf-8','gb2312',$row['count_id'])."\t";
            echo   "\n";
        }
    }