我需要用Laravel框架在PHP上导入excel表(csv或xlsx),我需要知道如何才能做到这一点。
发布于 2020-05-20 10:53:36
使用https://github.com/box/spout或https://github.com/maatwebsite/Laravel-Excel
喷口速度更快,但Laravel有更多的功能.
导入示例:
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
$reader = ReaderEntityFactory::createReaderFromFile('/path/to/file.ext');
$reader->open($filePath);
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
// do stuff with the row
$cells = $row->getCells();
}
}
$reader->close();
在Laravel中,您可以直接导入模型:
(new UsersImport)->import('users.xlsx', null, \Maatwebsite\Excel\Excel::XLSX);
https://stackoverflow.com/questions/61919883
复制