根据 sheet 名称导入 laravel excel
Import laravel excel depending on the sheet name
我有一个包含多个 sheet 的 Excel 文件,每个 sheet 都有一个特定的名称,该名称位于产品的“名称”列中 table.
我想在导入 Excel 文件时,数据库中 sheet 的名称也被导入。
I am using Laravel Excel and PHPOficce packages.
And the import is in the laravel console commands.
class ProductImport implements ToCollection, WithHeadingRow, WithProgressBar
{
use Importable;
public function collection(Collection $rows)
{
$reader = IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load(storage_path('app/fruit/listFruit.xlsx'));
$products = $spreadsheet->getSheetNames();
//name of sheets
//$products=['banana', 'strawberry'];
foreach ($rows as $row) {
foreach ($products as $product) {
ProductImport::updateOrCreate(
[
'price' => $row['price'],
'size' => $row['size'],
'product' => $product,//nameSheet
],
);
}
}
}
}
<?php
namespace App\Console\Commands;
use App\Imports\ProductImport;
use PhpOffice;
use Illuminate\Console\Command;
class UploadProduct extends Command
{
protected $signature = 'fruit:productList';
protected $description = 'Command to inject fruit data';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->output->title('Starting import');
(new ProductImport)->withOutput($this->output)->import(storage_path('app/fruit/listFruit.xlsx'));
$this->output->success('Import successful');
}
}
What should come out
What comes out
if I reverse the foreach
Excel
I want that the variable $product every time I go through a sheet, the name of the sheet is imported in the database and that I don't get only the name of the first sheet.
我认为这样会更有说明性
正如我所说,您只需切换您的 foreach 循环:首先是产品,然后是行(这些是您的变量名称,也许这就是您在我的解释中不理解的内容)
你有什么
foreach ($rows as $row) {
foreach ($products as $product)
.....
}
}
应该是什么
foreach ($products as $product) {
foreach ($rows as $row) {
....
}
}
编辑:
这里是您需要的代码的开头(我已经用与您相同的数据创建了 xls 文件)
$reader = IOFactory::createReader($inputFileType);
$spreadsheet = $reader->load($inputFileName);
$loadedSheetNames = $spreadsheet->getSheetNames();
print '<pre>';
foreach ($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$sheet = $spreadsheet->getSheet($sheetIndex);
$sheetData = $sheet->toArray(null, true, true, true);
var_dump($loadedSheetName, $sheetData);
}
die;
现在,我们将遍历每个 sheet 并获取其内容
$sheetData 是一个包含 sheet(产品)数据的数组,因此您仍然需要添加一些内容才能通过它。我让你做这部分
我知道你要开始了,但我还是要说解决方案就在 link 我给你
我有一个包含多个 sheet 的 Excel 文件,每个 sheet 都有一个特定的名称,该名称位于产品的“名称”列中 table. 我想在导入 Excel 文件时,数据库中 sheet 的名称也被导入。
I am using Laravel Excel and PHPOficce packages. And the import is in the laravel console commands.
class ProductImport implements ToCollection, WithHeadingRow, WithProgressBar
{
use Importable;
public function collection(Collection $rows)
{
$reader = IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load(storage_path('app/fruit/listFruit.xlsx'));
$products = $spreadsheet->getSheetNames();
//name of sheets
//$products=['banana', 'strawberry'];
foreach ($rows as $row) {
foreach ($products as $product) {
ProductImport::updateOrCreate(
[
'price' => $row['price'],
'size' => $row['size'],
'product' => $product,//nameSheet
],
);
}
}
}
}
<?php
namespace App\Console\Commands;
use App\Imports\ProductImport;
use PhpOffice;
use Illuminate\Console\Command;
class UploadProduct extends Command
{
protected $signature = 'fruit:productList';
protected $description = 'Command to inject fruit data';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->output->title('Starting import');
(new ProductImport)->withOutput($this->output)->import(storage_path('app/fruit/listFruit.xlsx'));
$this->output->success('Import successful');
}
}
What should come out
What comes out
if I reverse the foreach
Excel
I want that the variable $product every time I go through a sheet, the name of the sheet is imported in the database and that I don't get only the name of the first sheet.
我认为这样会更有说明性
正如我所说,您只需切换您的 foreach 循环:首先是产品,然后是行(这些是您的变量名称,也许这就是您在我的解释中不理解的内容)
你有什么
foreach ($rows as $row) {
foreach ($products as $product)
.....
}
}
应该是什么
foreach ($products as $product) {
foreach ($rows as $row) {
....
}
}
编辑:
这里是您需要的代码的开头(我已经用与您相同的数据创建了 xls 文件)
$reader = IOFactory::createReader($inputFileType);
$spreadsheet = $reader->load($inputFileName);
$loadedSheetNames = $spreadsheet->getSheetNames();
print '<pre>';
foreach ($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$sheet = $spreadsheet->getSheet($sheetIndex);
$sheetData = $sheet->toArray(null, true, true, true);
var_dump($loadedSheetName, $sheetData);
}
die;
现在,我们将遍历每个 sheet 并获取其内容
$sheetData 是一个包含 sheet(产品)数据的数组,因此您仍然需要添加一些内容才能通过它。我让你做这部分
我知道你要开始了,但我还是要说解决方案就在 link 我给你