我有使用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,但似乎也没有用
我会与您的客户核实他们是否使用真实的Excel或其他电子表格。
如果他们使用其他电子表格并使用“导出为Excel”功能导出,这可以解释为什么PHPSpreadsheet不会将其识别为任何可能的有效Excel格式。
在这种情况下,并且根据电子表格中的内容,可能值得让他们将他们的电子表格导出为csv
(逗号分隔值)文件,因为这是一种简单的格式,它应该是有效的输出。然后,您可以使用fgetcsv()
函数调用来读取它,而不必使用PHPSpreadsheet。
尝试删除此声明
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("lista.xls");
并改变它
$inputFileName = "lista.xls";
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($inputFileName);
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load($inputFileName);