我有一个需求,需要使用PhpSpreadsheet读取XLS文件(而不是xlsx),但是我遇到了困难。我按照文档尝试了以下代码,但是并没有成功:
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("lista.xls");
$worksheet = $spreadsheet->getActiveSheet();
echo '<table>' . PHP_EOL;
foreach ($worksheet->getRowIterator() as $row) {
echo '<tr>' . PHP_EOL;
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells,
// even if a cell value is not set.
// By default, only cells that have a value
// set will be iterated.
foreach ($cellIterator as $cell) {
echo '<td>' .
$cell->getValue() .
'</td>' . PHP_EOL;
}
echo '</tr>' . PHP_EOL;
}
echo '</table>' . PHP_EOL;
echo "<br>fin";
但是没有起作用(它可以处理xlsx文件,但无法处理xls文件!)
然后我尝试以不同的方式打开文件:
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$sheet = $reader->load("lista.xls");
但是它也不起作用...
我真的需要解决这个问题...请帮忙! PS:我已经尝试了BasicExcel和PHPExcel,但貌似也没有用。