2014-07-01 36 views
0

我正在使用一些庫將PHP數組數據寫入excel文件。當我將數據寫入excel文件並回顯一些成功消息時,它可以正常工作。沒有其他數據會被添加到文件中。爲什麼額外的信息被添加到要使用PHP頭文件下載的Excel文件中?

但是,當我用頭做的同一個文件的功能目前可行的頁面上一些額外的信息下載(如標題菜單,一些標題,在頁面底部的版權線等)被添加到該文件。如何避免將這些額外的信息添加到Excel文件?以下是我的代碼:

<?php 
    require_once(CORE_PATH."/libs/excelwriter.inc.php"); 

    $objRebateReports = new RebateReports(); 

    if($_POST['btnDownload']!='') { 
    $rebate_ret = $objRebateReports->GetRebateReport($_POST); 
    $rebate_data = $objRebateReports->GetResponse(); 

    $t=time(); 

    $fileName = ADMIN_ROOT."modules/rebates/rebate_report_".$t.".xls"; 

    $excel = new ExcelWriter($fileName);  

    if($excel==false) { 
     echo $excel->error; 
     die; 
    }  

    $myArr = array('Sr. No.', 'Product','Manufacturer','User Name','Date','Status','Transaction Date'); 

    $excel->writeLine($myArr, array('text-align'=>'center', 'color'=> 'red')); 

    $id=1; 

    foreach ($rebate_data as $value) { 

     $temp_rebate_data =array(); 

     $temp_rebate_data['id'] = $id; 
     $temp_rebate_data['product'] = ""; 
     $temp_rebate_data['manufacturer'] = "";    
     $temp_rebate_data['user_name'] = $value['customer_first_name']."".$value['customer_last_name']; 
     $temp_rebate_data['date'] = $value['created_at']; 
     $temp_rebate_data['status'] = $value['request_status']; 
     $temp_rebate_data['transaction_date'] = ""; 

     $row = $temp_rebate_data; 
     $excel->writeLine($row, array()); 
     $id++; 
    } 
    $excel->close(); 

    //Following is the header information in order to download the excel file 

    header("Content-Type: application/vnd.ms-excel; charset=utf-8"); 
    header("Content-Disposition: attachment; filename=".$fileName); 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Cache-Control: private",false); 

    //Below is the success message after printing the data successfully to the file 
    //echo "Data written to file $fileName Successfully.";     
} 
?> 

回答

0

您應該爲此使用輸出緩衝。

 //start of the page 

     ob_start(); 


     //ur code 

     //headers 

     ob_clean(); 
     flush(); 
     readfile($file); 
     exit; 

我認爲這將解決您的問題

相關問題