【问题标题】:PHP Excel API Error: File Format or File Extension not validPHP Excel API 错误:文件格式或文件扩展名无效
【发布时间】:2017-06-23 03:38:00
【问题描述】:

我的代码“文件格式或文件扩展名无效”出现错误。我正在使用 PHP Excel 类

这是我的代码:

<?php
session_start();
ini_set('max_execution_time', 1200); //20 mins
ob_start();

/** Error reporting */
error_reporting(E_ALL);

 /** Include path **/
ini_set('include_path', ini_get('include_path').';../classes/');

/** PHPExcel */
include '../classes/PHPExcel.php';

/** PHPExcel_Writer_Excel2007 */
include '../classes/PHPExcel/Writer/Excel2007.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();


require_once '../classes/PHPExcel.php';


ini_set('memory_limit', '-1');
    //Untested... pulled from the manual as the way to write with PHPExcel
    //Save Excel 2007 file
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    ob_end_clean();
    //We'll be outputting an excel file
    header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    //It will be called file.xls
    header("Content-Disposition: attachment;filename=\"Past_Due_Report.xls\"");
    header("Cache-Control: max-age=0");
    $objWriter->save('php://output');
    exit();
?>

任何帮助将不胜感激 谢谢

【问题讨论】:

  • 而不是打开文件保存并在文本编辑器中查看
  • 我认为当我们使用 PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007') 即“Excel2007”时,文件扩展名将是 .xlsx... 所以你必须更改“Past_Due_Report. xls" 到 "Past_Due_Report.xlsx"
  • @pAsh 我已经改成 .xlsx,同样的错误
  • 删除 -> Past_Due_Report.xlsx 前后的“\”
  • @pAsh 已删除 '\' 仍然出现相同的错误

标签: php excel phpexcel


【解决方案1】:

我认为这个问题的解决方法和这里是一样的: Google Chrome errors while exporting XLS file using PHP

只需在 attachment;filename 之间添加一个空格,这样:

header("Content-Disposition: attachment; filename=\"Past_Due_Report.xls\"");

【讨论】:

    【解决方案2】:

    以下代码适用于我.. 在您的代码中,您创建对象第一个“PHPExcel”,然后包含该文件.. 我做了一些必要的更改。查看链接:https://github.com/PHPOffice/PHPExcel

    session_start();
    ini_set('max_execution_time', 1200); //20 mins
    ob_start();
    /** Error reporting */
    error_reporting(E_ALL);
     /** Include path **/
    ini_set('include_path', ini_get('include_path').';../classes/');
    
    /** PHPExcel */
    /*include '../classes/PHPExcel.php';*/
    
    /** PHPExcel_Writer_Excel2007 */
    /*include '../classes/PHPExcel/Writer/Excel2007.php';*/
    
    require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
    // Create new PHPExcel object
    $objPHPExcel = new PHPExcel();
    ini_set('memory_limit', '-1');
        //Untested... pulled from the manual as the way to write with PHPExcel
        //Save Excel 2007 file
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        ob_end_clean();
        //We'll be outputting an excel file
        header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //It will be called file.xls
        header("Content-Disposition: attachment;filename=\"Past_Due_Report.xls\"");
        header("Cache-Control: max-age=0");
        $objWriter->save('php://output');
        exit();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-30
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      相关资源
      最近更新 更多