PHP 打印表格并下载表格的 xls 文件不起作用

发布于 2024-12-17 08:06:04 字数 2344 浏览 1 评论 0原文

我有一个 .php 页面,显示了与此类似的 HTML 表格:-

<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>

该表格下方显示了一些数据。如果用户想要将表的 .xls 文件下载到他们的 PC,我允许在 URL 中传递变量。文件下载代码与此类似:-

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) . '"'; 
} 

如果变量位于 URL 中,则在检索变量后,我使用以下代码行在 php 代码中调用 savexls() 函数:-

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

我遇到的问题是,如果我声明如果 URL 中包含 ?xls=y,则 html 表格数据不会输出到屏幕,尽管 xls 文件会下载到 PC。

如果我不声明 ?xls=y 那么 .XLS 文件不会被下载,并且 HTML 表格会很好地打印在屏幕上。

为什么写入 .xls 文件的行为会阻止其他 HTML 表打印到屏幕上?对我来说,它们看起来像是完全独立的功能,但进行文件保存似乎确实阻止了第一个 HTML 打印代码停止执行其工作。

我不知道问题是什么,除了有一种模糊的感觉,我也许应该在不同的 PHP 文件中进行表格打印和 xls 文件保存,这可能会解决我的问题,但我不确定如何做到这一点。

首先我想知道为什么这样做会出现问题,其次有没有好的方法来解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你好,陌生人 2024-12-24 08:06:04

请注意,savexls() 函数

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

在响应中发送 excelsheet 标头。这使得浏览器认为返回的响应是 xls 文件的内容。

我相信你现在所做的就是最好的方法。您可以保持 php 代码不变,但在 html 中添加一个下载链接,该链接将引用同一页面(但网址中包含 xls=y)。这将允许只想查看文件的人查看它,而想要下载 xls 的人将在看到 xls 后才能下载。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文