[英]PHPExcel not working with xls file
I am using PHPExcel to read excel file in php,it is working fine with xlsx
but when try to read xls
file,It shows an error 我正在使用PHPExcel读取php中的excel文件,它与
xlsx
正常工作,但是当尝试读取xls
文件时,显示错误
Fatal error: Call to undefined method PHPExcel_Reader_CSV::setReadDataOnly() in /var/www/....
Here is my code 这是我的代码
$file_path='/var/www/html/site/sample.xls';
$inputFileType = PHPExcel_IOFactory::identify( $file_path);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
/** Load $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load( $file_path);
$total_sheets=$objPHPExcel->getSheetCount();
$allSheetName=$objPHPExcel->getSheetNames();
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
for ($row = 1; $row <= $highestRow;++$row)
{
for ($col = 0; $col <$highestColumnIndex;++$col)
{
$value=$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
$arraydata[$row-1][$col]=trim($value);
}
}
The file that you're loading is identified as a CSV
file, even though it has an .xls
extension... it is quite a common practise for some developers to save files formatted as csv or with html markup with a .xls extension, but that doesn't make them BIFF-format .xls files. 即使您正在加载的文件具有
.xls
扩展名,也可以将其识别为CSV
文件...对于某些开发人员来说,保存csv格式或带有.xls扩展名的html标记格式的文件是一种很常见的做法,但这并不能使它们成为BIFF格式的.xls文件。
The setReadDataOnly()
method isn't available for the CSV Reader, because a CSV file cannot contain anything other than data. setReadDataOnly()
方法不适用于CSV阅读器,因为CSV文件中不能包含任何数据。
The most recent versions of PHPExcel provide a stub for setReadDataOnly()
in the CSV Reader to prevent an error in this situation, and I'd certainly recommend that you upgrade to the latest code; PHPExcel的最新版本在CSV阅读器中为
setReadDataOnly()
提供了一个存根,以防止在这种情况下出现错误,我当然建议您升级到最新代码; but if you can't do that, then the simplest fix for you is simply to wrap the call to setReadDataOnly()
in an if
test: 但是,如果不能这样做,那么对您来说,最简单的解决方法就是
setReadDataOnly()
的调用包装在if
测试中:
$inputFileType = PHPExcel_IOFactory::identify( $file_path);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
if ($inputFileType !== 'CSV') {
$objReader->setReadDataOnly(true);
}
For binary Excel files (xls) you may have better luck using the (old) PHP-ExcelReader . 对于二进制Excel文件(xls),使用(旧) PHP-ExcelReader可能会更好。 It's the most reliable one I've found so far.
这是我到目前为止找到的最可靠的一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.