2016-08-03 54 views
1

我想读取excel文件。但它显示我错误。我试过这个。Excel PHP - PHPExcel错误

set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/'); 

/** PHPExcel_IOFactory */ 
include 'PHPExcel/IOFactory.php'; 

// Set the Excel file name and path 
$inputFileName = 'uploads/aaa.xlsx'; // this is 2007 new format. 

// Read your Excel workbook 
try { 
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName); 
    $objReader = PHPExcel_IOFactory::createReader($inputFileType); 
    $objPHPExcel = $objReader->load($inputFileName); 
} catch(Exception $e) { 
    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage()); 
} 

但这种错误显示...

Fatal error: Class 'PHPExcel' not found in F:\xampp\htdocs\preme\PHPExcel\Reader\Excel2007.php on line 351 
+0

您是否在使用任何类型的自动加载器? –

回答

0

我自动载入 “phpoffice/phpexcel” 与作曲家。确保你的目录一致。如果你不使用作曲家,那么指向正确的目录。只是为了演示,我指向下面的文件夹

<?php 
//require_once 'vendor/autoload.php'; 
require_once 'vendor\phpoffice\phpexcel\Classes\PHPExcel\IOFactory.php'; 

$inputFileName = 'uploads/aaa.xlsx'; 

// Read your Excel workbook 
try { 
    $excelReader = PHPExcel_IOFactory::createReaderForFile($inputFileName); 
    $excelReader->setReadDataOnly(); 
    $excelReader->setLoadAllSheets(); 
    $excelObj = $excelReader->load($inputFileName); 
    //var_dump($excelObj); 
} catch(Exception $e) { 
    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage()); 
}