【问题标题】:how to download excel file from php page using phpexcel如何使用phpexcel从php页面下载excel文件
【发布时间】:2018-07-02 23:06:27
【问题描述】:

在我们的 cakephp 应用程序中有一个 excel 文件下载..

我刚刚为此准备了一个查看页面。

但我需要下载那个..

我该怎么做?

我想为此使用 PHPExcel 插件......

这是我使用的代码:

 /** PHPExcel_IOFactory */
        include '../Vendor/PHPExcel/Classes/PHPExcel/IOFactory.php';
        $objReader = PHPExcel_IOFactory::createReader('HTML');

        $objPHPExcel = $objReader->load('../View/Events/settlement_report_excel.html');

        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save("myExcelFile.xlsx");

但它显示../View/Events/settlement_report_excel.ctp is an Invalid HTML file.

我该如何克服这个问题...

【问题讨论】:

  • CTP是什么格式?它不是 PHPExcel 支持的标准文件格式之一
  • 我有一个名为 settlement_report_excel.ctp 的视图页面,其中有 html 表格,也有 php 代码,我需要将该表格下载为 excel .....
  • 好吧,标准 PHPExcel 中没有 CTP 阅读器,所以 PHPExcel_IOFactory::createReader('CTP'); 应该抛出异常
  • 好吧,我弄错了....
  • 我编辑了我的问题,但现在我得到了这个错误......../View/Events/attendees-list-table.html is an Invalid HTML file.

标签: cakephp phpexcel


【解决方案1】:

您可以在 CakePHP 环境中使用 simple PHP 创建所需的 csv、xls。执行以下代码后,将下载所需的 excel 文件。

header("Content-Type: application/xls");  
header("Content-Disposition: attachment; filename=names.xls"); 
header("Pragma: no-cache");
header("Expires: 0");

$schema_insert = $firstname."\t".$lastname."\t".$fullname;

echo ($schema_insert . "\r\n");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多