2017-06-23 79 views
1

我的代碼「文件格式或文件擴展名無效」出現錯誤。我使用的是PHP的Excel類PHP Excel API錯誤:文件格式或文件擴展名無效

這是我的代碼:

<?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(); 
?> 

任何幫助,將不勝感激 感謝

+0

而不是打開文件保存它,看它在文本編輯器 – rtfm

+0

我認爲,當我們使用PHPExcel_IOFactory :: createWriter($ objPHPExcel,「Excel2007中」),即「Excel2007的」,那麼,文件擴展名將爲.xlsx ...因此,您必須將「Past_Due_Report.xls」更改爲「Past_Due_Report.xlsx」 – pAsh

+0

@pAsh我已更改爲.xlsx,同樣的錯誤 – Bongsky

回答

-1

下面的代碼從我身邊的工作.. 在你的代碼創建對象第一個「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(); 
相關問題