如何从xls文件读取数据,没有任何类型的工作表?我正在尝试这个:
require_once "admin/phpexcel/Classes/PHPExcel.php";
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objReader->setLoadAllSheets();
$objPHPExcel = $objReader->load($import_file);
echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded<br /><br />';
$loadedSheetNames = $objPHPExcel->getSheetNames();
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
echo $sheetIndex,' -> ',$loadedSheetName,'<br />';
$sheetData = $objPHPExcel->getSheet($sheetIndex)->toArray(null,true,true,true);
var_dump($sheetData);
}
我得到了这个:
0 worksheets loaded
我没有留下深刻的印象,'因为我的xls文件没有它。只有一个数据,如何阅读?
更新
require_once "admin/phpexcel/Classes/PHPExcel.php";
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($import_file);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);
仍然没有。
更新 2。
现在我在评论中写的那条线上变得很奇怪Internal Server Error
,但这很奇怪,因为文件在检查之前是否存在:
if (file_exists($import_file))
{
$this->import_file = strftime("%Y_%m_%d").'.xls';
require_once "admin/phpexcel/Classes/PHPExcel.php";
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($import_file); // Internal Server Error
}
更新 3。
我找到了一个日志:
<br />
<b>Fatal error</b>: Uncaught exception 'Exception' with message 'The filename admin/files/import/44/2013_03_31.xls is not recognised as an OLE file' in /admin/phpexcel/Classes/PHPExcel/Shared/OLERead.php:87
Stack trace:
#0 admin/phpexcel/Classes/PHPExcel/Reader/Excel5.php(1182): PHPExcel_Shared_OLERead->read('admin/files/imp...')
#1 admin/phpexcel/Classes/PHPExcel/Reader/Excel5.php(689): PHPExcel_Reader_Excel5->_loadOLE('admin/files/imp...')
#2 admin/ajax/import.php(84): PHPExcel_Reader_Excel5->load('admin/files/imp...')
#3 admin/ajax/import.php(429): ImportAjax->import()
#4 {main}
thrown in <b>admin/phpexcel/Classes/PHPExcel/Shared/OLERead.php</b> on line <b>87</b><br />
更新 4.代码,PHPExcel_IOFactory::identify();
if (file_exists($import_file))
{
require_once "admin/phpexcel/Classes/PHPExcel.php";
PHPExcel_IOFactory::identify();
}
现在我明白了:
<br />
<b>Fatal error</b>: Uncaught exception 'Exception' with message 'Could not open for reading! File does not exist.' in admin/phpexcel/Classes/PHPExcel/Reader/Excel2007.php:235
Stack trace:
#0 /admin/phpexcel/Classes/PHPExcel/IOFactory.php(280): PHPExcel_Reader_Excel2007->canRead(NULL)
#1 /admin/phpexcel/Classes/PHPExcel/IOFactory.php(205): PHPExcel_IOFactory::createReaderForFile(NULL)
#2 /admin/ajax/import.php(83): PHPExcel_IOFactory::identify()
#3 /admin/ajax/import.php(428): ImportAjax->import()
#4 {main}
thrown in <b>/admin/phpexcel/Classes/PHPExcel/Reader/Excel2007.php</b> on line <b>235</b><br />