我只是想知道是否可以通过已实现的import方法与我的控制器进行通信。 我试过将数据数组返回到控制器,但没有通过。 尚未提及此事...
我知道我可以使用toCollection并将整个导入返回到控制器中的一个集合,但担心会影响性能。
将导入的数据放入导入对象的class属性中,并为其创建一个吸气剂,以便在控制器中导入完成后可以调用该吸气剂。
我仍然必须缺少一些东西,让我更具体一些。
所以这是我在控制器中的导入调用:
$import = Excel::import(new SalesImport, 'imports/salesdata.xlsx');
class SalesImport implements ToCollection
{
public function collection(Collection $rows)
{
.... lots of other stuff ....
$this->data = $data;
}
对我来说很明显,这些数据并没有使其返回到控制器中的$ import对象,但是我想我需要在这里实现ToCollection以外的东西吗?
class SalesImport implements ToCollection
{
public $data;
public function collection(Collection $rows)
{
.... lots of other stuff ....
$this->data = $data;
}
$import = new SalesImport;
Excel::import($import, 'imports/salesdata.xlsx');
dd($import->data);
class SalesImport implements ToCollection { public $data; public function collection(Collection $rows) { .... lots of other stuff .... $this->data = $data; }
$import = new SalesImport; Excel::import($import, 'imports/salesdata.xlsx'); dd($import->data);
惊人的!
class SalesImport implements ToCollection { public $data; public function collection(Collection $rows) { .... lots of other stuff .... $this->data = $data; }
$import = new SalesImport; Excel::import($import, 'imports/salesdata.xlsx'); dd($import->data);
您能否在https://docs.laravel-excel.com/3.1/imports/collection.html或文档中的其他位置进行设置? 我浪费了很多时间进行搜索。
一个类似的例子已经存在于文档中一段时间了: https :
很棒的图书馆,这个例子非常适合单张纸。
关于使用WithMultipleSheets导入的任何提示? 我应该如何获得行数?
在CRUD上,我得到:调用未定义的方法MaatwebsiteExcelExcel :: getRowCount()
我正在使用一个类来调用特定的工作表导入器:
class ExcelSampleSheetsImport implements WithMultipleSheets
{
protected $params;
public function __construct($params)
{
$this->params = $params;
}
public function sheets(): array
{
return [
'Sheet to import' => new ExcelSampleImport($this->params)
];
}
}
在ExcelSampleImport类上,我已按照文档中的指定实现了计数器。
如何使用多张纸才能做到这一点?
进口班
class CommunityImport implements WithMultipleSheets
{
use WithConditionalSheets;
public function conditionalSheets(): array
{
return [
0 => new FirstSheetCommunityImport(),
1 => new SecondSheetCommunityImport(),
];
}
}
第一张课
class FirstSheetCommunityImport implements ToCollection
{
public $id;
public function collection(Collection $rows)
{
$i=0;
foreach ($rows as $row)
{
if($i==0){
$this->id = $row[0];
}
$i++;
}
}
}
页面控制器-如何从FirstSheetCommunityImport类访问ID?
public function import()
{
$import = new CommunityImport();
$import->onlySheets(0);
Excel::import($import, storage_path('app/xls/').'comunidad-import.xlsx');
}
从FirstSheetCommunityClass访问该ID对我来说是一个问题,但我使用Laravel缓存将我的收藏存储了10分钟,然后使用Cache Facade从控制器访问它
这是我对多张吸气剂的看法。 在我看来,为此使用缓存不是一个好主意。
<?php
// VendorsExtraDataImportSheet.php:
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
class VendorsExtraDataImportSheet implements ToCollection
{
public $count = 0;
public function collection(Collection $rows)
{
$this->count++;
}
}
<?php
// VendorsExtraDataImport.php
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithConditionalSheets;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
class VendorsExtraDataImport implements ToCollection, WithMultipleSheets
{
use WithConditionalSheets;
public const SHEET_TO_IMPORT = 'Example data input';
public $sheet;
public function collection(Collection $rows)
{
}
public function conditionalSheets(): array
{
if (!$this->sheet) {
$this->sheet = new VendorsExtraDataImportSheet;
}
return [
self::SHEET_TO_IMPORT => $this->sheet,
];
}
}
<?php
// ...
// PageController.php
public function import()
{
$import = new VendorsExtraDataImport();
$import->onlySheets(VendorsExtraDataImport::SHEET_TO_IMPORT);
Excel::import($import, resource_path('data/vendors.xlsx'));
dd($import->sheet->count);
}
// ...
最有用的评论