Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么要将额外的信息添加到要使用PHP标题下载的Excel文件中?_Php_Excel_Http Headers_Download - Fatal编程技术网

为什么要将额外的信息添加到要使用PHP标题下载的Excel文件中?

为什么要将额外的信息添加到要使用PHP标题下载的Excel文件中?,php,excel,http-headers,download,Php,Excel,Http Headers,Download,我正在使用一些库将PHP数组数据写入excel文件。当我将数据写入excel文件并回显一些成功消息时,它工作正常。除了预期的数组之外,没有其他数据添加到文件中 但是,当我使用标题使相同文件功能的下载变得可行时,页面上的一些附加信息(如标题菜单、某些标题、页面底部的版权行等)会添加到文件中。如何避免将这些额外信息添加到excel文件中?以下是我的代码: <?php require_once( CORE_PATH."/libs/excelwriter.inc.php" ); $obj

我正在使用一些库将PHP数组数据写入excel文件。当我将数据写入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.";                 
 }
?>

您应该为此使用输出缓冲

        //start of the page

        ob_start(); 


        //ur code

       //headers

       ob_clean();
       flush();
       readfile($file);
        exit;
我想这会解决你的问题