【问题标题】:PHP header xls using .xlsx使用 .xlsx 的 PHP 标头 xls
【发布时间】:2020-06-28 12:31:49
【问题描述】:

我使用此代码导出 excel

<?php
 header("Content-type: application/vnd-ms-excel");
 header("Content-Disposition: attachment; filename=$title.xls");
 header("Pragma: no-cache");
 header("Expires: 0");
 ?>

但我无法将 $title.xls 更改为 .xlsx 格式

请给我解决办法

【问题讨论】:

  • 该文件是实际的 excel 文件吗?使用 PHP 设置 HTTP 标头实际上不会转换任何内容。

标签: php excel export-to-csv export-to-excel


【解决方案1】:

更改内容类型。尝试使用以下代码下载带有 $title 格式的文件

        $file = $title.".xlsx";
        header('Content-Description: File Transfer');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);

【讨论】:

  • “试试这个”并不是一个好的答案。你应该解释如何为什么这可以解决他们的问题。我推荐阅读,“How do I write a good answer?"
  • 我使用这个代码,但不起作用,你可以看到这个:link
【解决方案2】:

对于 BIFF .xls 文件

application/vnd.ms-excel

对于 Excel2007 及以上的 .xlsx 文件

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

【讨论】:

    猜你喜欢
    • 2011-03-20
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 2016-07-11
    • 1970-01-01
    相关资源
    最近更新 更多