[英]Read XLS in PHP using PhpSpreadsheet
I have a requirements to read XLS files (not xlsx) using PhpSpreadsheet and I having trouble. 我有使用PhpSpreadsheet读取XLS文件(不是xlsx)的要求,但遇到了麻烦。 I tried this (as the documentation say but...)
我试过了(正如文档所说,但是...)
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";
but didn't work (it worked with a xlsx file, but no with a xls file!) 但是没有用(它只能用于xlsx文件,而不能用于xls文件!)
Then I tried to open file differently: 然后我尝试以不同的方式打开文件:
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$sheet = $reader->load("lista.xls");
but also doesn't work... 但也行不通...
I really need to solve this... please help! 我真的需要解决这个问题...请帮忙! PS: I've tried BasicExcel & PHPExcel but also didn't seem to work
PS:我已经尝试过BasicExcel和PHPExcel,但似乎也没有用
I would check with your Client to see if they are using real Excel or some other spreadsheet. 我会与您的客户核对一下,看看他们是否使用的是真正的Excel或其他电子表格。
If they are using some other spreadsheet and exporting using a "Export as Excel" functionality that may explain why its not being recognised by PHPSpreadsheet as any of the possible valid excel formats. 如果他们使用其他电子表格并使用“导出为Excel”功能进行导出,则可以解释为什么PHPSpreadsheet无法将其识别为任何可能的有效excel格式。
In which case, and depending what is in the spreadsheet, it may be worth asking them to export their spreadsheet as a csv
(comma delimited values) file, as that is such a simple format it should be a valid output. 在这种情况下,并取决于电子表格中的内容,可能值得要求他们将电子表格导出为
csv
(逗号分隔值)文件,因为这种格式很简单,应该是有效的输出。 You could then read it using fgetcsv()
function calls instead of having to use PHPSpreadsheet. 然后,您可以使用
fgetcsv()
函数调用来读取它,而不必使用PHPSpreadsheet。
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$inputFileType = 'Xlsx';
$inputFileName = './mysheet.xlsx';
/** Create a new Reader of the type defined in $inputFileType **/
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Advise the Reader that we only want to load cell data **/
$reader->setReadDataOnly(true);
$worksheetData = $reader->listWorksheetInfo($inputFileName);
foreach ($worksheetData as $worksheet) {
$sheetName = $worksheet['worksheetName'];
echo "<h4>$sheetName</h4>";
/** Load $inputFileName to a Spreadsheet Object **/
$reader->setLoadSheetsOnly($sheetName);
$spreadsheet = $reader->load($inputFileName);
$worksheet = $spreadsheet->getActiveSheet();
print_r($worksheet->toArray());
}
try to remove this statement 尝试删除此语句
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("lista.xls");
and change it with this 并用它来改变
$inputFileName = "lista.xls";
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($inputFileName);
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load($inputFileName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.