简体   繁体   中英

PHP print table and download xls file of table not working

I've got a .php page showing a HTML table similar to this one :-

<table border="1">
    <tr bgcolor="DCDCDC">
      <th><div align="center"><b>Data1</b></div></th>
      <th><div align="center"><b>Data2</b></div></th>
      <th><div align="center"><b>Data3</b></div></th>
    </tr>

the table then has some data shown below it. I allow a variable to be passed in the URL if the user wants an .xls file of the table downloading to their PC. The file download code is similar to this :-

function savexls(){
   $data = array( array("Data1" => "Mary", "Data2" => "Johnson", "Data3" => 25), 
         array("Data1" => "Amanda", "Data2" => "Miller", "Data3" => 18), 
         array("Data1" => "James", "Data2" => "Brown", "Data3" => 31), 
         array("Data1" => "Patricia", "Data2" => "Williams", "Data3" => 7), 
         array("Data1" => "Michael", "Data2" => "Davis", "Data3" => 43), 
         array("Data1" => "Sarah", "Data2" => "Miller", "Data3" => 24), 
         array("Data1" => "Patrick", "Data2" => "Miller", "Data3" => 27) ); 

   # filename for download 
   $filename = "Dividend Data " . date("F j, Y") . ".xls"; 

   header("Content-Disposition: attachment; filename=\"$filename\""); 
   header("Content-Type: application/vnd.ms-excel"); 

   $flag = false; 
   foreach($data as $row) {
      if(!$flag) { 
         # display field/column names as first row 
         echo implode("\t", array_keys($row)) . "\r\n"; 
         $flag = true; 
      } 
      array_walk($row, 'cleanData'); 
      echo implode("\t", array_values($row)) . "\r\n"; 
   }
}

function cleanData(&$str) 
{ 
   $str = preg_replace("/\t/", "\\t", $str);
   $str = preg_replace("/\r?\n/", "\\n", $str); 
   if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; 
} 

I call the savexls() function in the php code using the following line of code after retrieving the variable if it's in the URL :-

if ($xls=="y") {
   savexls();
}

The problem I've got is that if I declare that ?xls=y in the URL, the html table data doesn't get output to the screen, although the xls file DOES get downloaded to the PC.

If I don't declare that ?xls=y then the .XLS file DOESN'T get downloaded, and the HTML table gets printed on the screen fine.

Why does the act of writing the .xls file stop the other HTML table being printed to the screen ? They look like totally separate functions to me, but doing the file-saving does seem to stop the first HTML printing code stop doing it's job.

I am stuck as to what the problem is, other than having a vague feeling that I should perhaps be doing the table-printing and the xls-file saving in different PHP files, and this may solve my problem, but I'm not sure how to do that.

First off I'd like to know why there's a problem doing it this way, and secondly, is there a good way to solve this issue?

Notice that the savexls() function sends the excelsheet headers

header("Content-Disposition: attachment; filename=\"$filename\""); 
header("Content-Type: application/vnd.ms-excel"); 

in the response. This makes the browser believe that the returned response is the content of an xls file.

I believe what you are doing now is the best way to do this. You can keep the php code as it is, but add a download link to your html, which will refer to the same page (but with xls=y in the url). This will allow people who want to only view the file to view it, and people who want to download the xls will be able to download it after they see the xls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM